Wednesday 23 March 2011

Bitwise and, or, XOR, not, shift left, shift right

To further understand bitwise operators I tried walking through the binary1.cpp under March 13's class.

CODE:

01. #include <cstdio>
02. using namespace std;
03. int main(void){
04.  char A = 0x53;
05.  char B = 0x9A;
06.  char C;
07.  C = A & B;
08.  printf("%X\n", C);
09.  C = A | B;
10.  printf("%X\n", C);
11.  C = ~A;
12.  printf("%X\n", C);
13.  C = ~B;
14.  printf("%X\n", C);
15.  C = A + (~A+1); // C = A - A;
16.  printf("%X\n", C);
17.  C = A ^ B;
18.  printf("%X\n", C);
19.  C = C ^ B;
20.  printf("%X\n", C);
21.  printf("Shifts.......\n");
22.  C = A << 1;
23.  printf("%X\n", C);
24.  C = B << 1;
25.  printf("%X\n", C);
26.  C = A >> 1;
27.  printf("%X\n", C);
28.  C = B >> 1;
29. printf("%X\n", C);
30.  A = 100;
31.  A = A >> 1;  //  A = A / 2
32.  printf("%d\n", A);
33.  A<<=1;  // A *= 2;
34.  printf("%d\n", A);
35.  return 0;
36. }

The first bitwise operator we run into is bitwise and on line 07.
C = A & B

The hexadecimal code for A and B are the following:

A. 53 = 0101 0011
B. 9A = 1001 1010

The bitwise operator searches through the two variables and determines if the binary code for the variables are the same. For example:
We compare 5 and 9.
A. 5 = 0101
B. 9 = 1001
C = 0001

If the corresponding code are both 1's then 1 is true. If its not the code turns into a 0. So the full bitwise and binary code would be "0001 0010" which will print 12 in line 08!

The next bitewise operator we see is bitwise or. This operator goes through both the variables and checks if either or the binary combination is 1, if either of them is one it returns the 1. If its not a one it returns 0.

The second bitwise operator we run into is bitwise or on line 09.
C = A | B;

A. 53 = 0101 0011
B. 9A = 1001 1010
C = 1101 1011

This will display FFFFFFDB in line 10. It will display the F's because binary hexadecimal has 8 bits, and will fill the rest with 1's.

*RULE: if it starts with 1, it will print 6 F's.

The next bitwise operator we encounter is bitwise NOT " ~ ". This simply reverses the binary code that is given.

A. 53 = 0101 0011
B. 9A = 1001 1010

~A = 1010 1100
~B = 0110 0101

In line 17 we see the next bitwise operator which is bitwise XOR " ^ ".  This operator compares the binary code for 2 variables and does the following:
If "1 ^ 0" and "0 ^ 1" are valid you change the binary code to 1.
If "0 ^ 0" and "1 ^ 1" are invalid you change the binary code to 0.

In line 17 the following happens:

A. 53 = 0101 0011
B. 9A = 1001 1010
C = A ^ B

C = 1100 1001
This will print FFFFFFC9 in line 18.

In line 19 we have an exclusive or where C = C ^ B. If we follow the rules from before the answer would be as follows:

C = 1100 1001
B = 1001 1010
NEW C = 0101 0011

This will display 53!

After line 20 we come across shifting. The first shift we encounter is a left shift.
In line 21 we have the following code:

C = A << 1;

The current binary code for C is:

C = 0101 0011
By shifting everything to the left we get:
C = 1010 0110

Which will print : FFFFFFA6

In line 25 we have another left shift.

C = B << 1;
C = 0011 0100

This will print out: 34

When shifting to the left the last number will always be 0.

Now we have shifting to the right in line 26.

C = A >> 1;
You basically just shift everything to the right like you would do when shifting to the left
A =  0101 0011
C =  0010 1001

This will print:  29

In line 28 we have another right shift:

C = B >> 1;
B = 1001 1010
C = 1100 1101

This will print: FFFFFFCD

*RULE: When shifting to the right and the first bit is 1, the bits before it are ALL 1. So when you shift it to the right you shift 1's instead of 0's.

In line 31 you we see A = A >> 1; This is basically the same thing as saying A = A / 2;


A = 100
A = 100 / 2

Making the value of A into : 50

In line 33 we see A << =1; This is the same thing as saying A * = 2;

Making the value of A into 100.

Binary is pretty easy if you get the hang of it!