Tuesday, 8 February 2011

Fun with bitwise–OR, AND, NOT, XOR and Left/ Right shift

“The funny thing about bitwise operator is, it operates only on bits on any type of data similar to how a computer hardware operates. To understand bitwise operations, we need to open up the Logical unit of a computer system. The various gates used are NOR, NAND, XOR, Shifters etc”

Metaphor

     When it comes to bits, its purely a computer or electronics phenomenon. 1 means highest signal, 0 means a lowest signal. In electronics, its a square wave with 2 symbols. In computers, if we treat that as a binary language, then its a 2 symbol digit system. To understand how this system works, we should know how does a number system work.

   In mathematics, the number system that we know has total 10 symbols, 0 – 9. After that what we have is just combination of these 10 symbols. Infinity is a new symbol btw!! :)

   But, as you can see, just 10 symbols which is why decimal system is called a base 10 decimal system. What does it mean by base 10 ? Every number in the decimal system can be represented by 10^x where x is a number from -infinity to infinity. Since this scale is very minute, we use logarithms to avoid multiplication and division.

So, log10 5 = 10^x => how much if you raise power of 10, will you get 5 when 10^0.6 is done. So, log 5 = 0.6 :D

With logarithms as we know, we can just need to add and subtract. rather than multiply/divide, as we have all in powers now.

  Very funny and obvious question. What is 10^-infinity ? Its zero amazingly :D. So, any based logarithm function with 0 as parameter will always give –infinity.

10^0 = 1. Anything power 0 is 1 but it excludes infinity!!.. Indians are the culprits who bought zero and made all this confusion!! :D

Concept

Getting the above concept. Base 10 is what human understands. Base 2 is what computer understands. With 32-bit systems, hexa decimal came in, where 16 symbols are present. 0-9 and then A-F, which means, Base 16 is also taken up by computers now. Base 8 is octet, which was used in older 8-bit systems. There are base 32 and base 64 as well used.

But the important thing here is, how will you convert between bases. Decimal system’s property is our base reference for everything as that is the master mind. :)

1’s(10^0) place, 10’s(10^1) place, 100’th(10^2) place, 1000’s(10^3) place!!.. same way, 2^0’s place, 2^1’s place, 2^2’s place.. is for binary!! :D

When you just add-up all the values of 2^0, 2^1, 2^2 for a binary, you get a decimal equivalent to it!! Note that, this is because, the binary system with 2 symbols grows in multiples of 2 in a decimal system!!..

  Bitwise is similar to a logical hardware gates.

OR or | = Adder (all symbol changes are taken)

AND or & = Condition flag checker (same symbol, take down the symbol)

NOT or ! = Reverse bit (!0 = 1)

XOR or ^ = Extract unique values ( 0 XOR 0 and 1 XOR 1 = 1.. same symbol, then 1)

Left shift or << x = Add zero’s at the right end for the amount specified. 2^x is the mapping decimal equivalent for each digit.

Right shift or >> x = Add zero’s at the left side. same 2^x is the mapping

Some fun code

Some XOR fun!!

   1:  void swap(int *a, int *b)
   2:  {
   3:      *a ^= *b; // a ^ b
   4:      *b ^= *a; // b ^ a
   5:      *a ^= *b; // a ^ b
   6:  }
   7:   
   8:  void main()
   9:  {
  10:      int a=5, b=8;
  11:      int c=10, d=10;
  12:      swap(&a,&b);
  13:      swap(&c, &d); // it works
  14:   
  15:  }

Important things

  • We will see more topics about AND, OR in coming topics :)