Search

воскресенье, 8 апреля 2012 г.

Study day part 2: Bits and Bites

Bitwise operator (I hope I'm spelling it correctly). They aren't that complicated, and I spend most of the break between semester helping a friend with that., so I got used to it. The only problem I have with them is that... Unless you create a function that displays a value by bits working with them is... theoretical. Best thing you can see is "a number changed" and that's it. But they work, and as far as I know very effectively.
First, Let's look at operators themselves.
Operators in binary are exactly like if statements.
AND
1. & - AND. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 & 0 = 0  //if(false && false) == false
1 & 0 = 0  //if(true && false) == false 
0 & 1 = 0  //if(false && true) == false
1 & 1 = 1  //if(true && true) ==  true
Example:
   A = "0011" //== 3
&
   B = "0101" //== 5
=
         "0001" //== 1
So:
   A & B = 1

inclusive OR 

2. | -  inclusive OR. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 | 0 = 0  //if(false || false) == false
1 | 0 = 1  //if(true || false) == true  
0 | 1 = 1  //if(false || true) == true  
1 | 1 = 1  //if(true || true) ==  true 

Example:
   A = "0011" //== 3
|
   B = "0101" //== 5
=
         "0111" //== 7
So:
   A | B = 7



exclusive OR 
3. ^ -  exclusive OR, it's a thing that you won't find in if statements, after previous ones it's not that hard. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 ^ 0 = 0  //if(false | false) == false
1 ^ 0 = 1  //if(true | false) == true  
0 ^ 1 = 1  //if(false | true) == true  
1 ^ 1 = 0  //if(true | true) ==   false  

Example:
   A = "0011" //== 3
^
   B = "0101" //== 5
=
         "0110" //== 6
So:
   A | B = 6

NOT
4.  ~ - NOT operator. Basically what is does if flips all the bits of a value.Example
3 = "0011"
~3 = "11111111111111111111111111111100"

Now the "fun part". Bit movement!

Right Shift
5. >>X - Right Shift operator.
Moves all the bits to the right X times.
A = "0101"; //3
A>>1="0010"; //2
A>>2="0001"; //1
A>>2 = 1;
Also, Right Shift once == divide by two.

Left Shift
 6. <<X - Left Shift operator.
Moves all the bits to the left X times.
WARNING!Apparently, there is a difference in what kind of value are you shifting. Unsigned or signed. If int is unsigned  it will use 0's to fill in the space, but if it's signed, it will use the last bit (if 1 - 1's, if 0 - 0's) to do that.
unsigned A = "0011"; //3
A<<1="0110"; //6
A<<2="1100"; //12
A<<2 = 1; 12
Also, Left Shift once == Multiply by two.

signed A = "0011"; //3
A<<1="0111"; //7
A<<2="1111"; //15
A<<2 = 15;


Example:
#include <cstdio>
using namespace std;

int main(){
unsigned char A = 0xA3;
unsigned char B = 0xF9;
unsigned char C;
printf("A: %X %d\n", A,A); //163 = "1010 0011"
printf("B: %X %d\n", B,B); //249 = "1111 1001"
printf("======================================\n");
C = A & B;
printf("A&B: %X %d\n", C, C); //161 = "1010 0001"
C = A | B;
printf("A|B: %X %d\n", C, C); //251 = "1111 1011"
C = A ^ B;
printf("A^B: %X %d\n", C, C); //90 = "0101 1010"
C = ~A;
printf("~A : %X %d\n", C, C); //92 = "0101 1100"
C = A << 1;
printf("A<1: %X %d\n", C, C); //70 = "0100 0110"
C = A >> 1;
printf("A>1: %X %d\n", C, C); //81 = "0101 0001"
printf ("(A >> 4) << 4:\n");
C = A >> 4;
printf("A>4: %X %d\n", C, C);//10 = "0000 1010"
C = C << 4;
printf("A<4: %X %d\n", C, C); //160 = "1010 0000"
printf ("(A >> 4) << 4 = %X %d\n", C, C);
return 0;
}

Комментариев нет:

Отправить комментарий