Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Remember Me

Are you a
Computer / IT professional?
Join Tek-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Just copy and paste the

#### Feedback

"...The enviroment is simple, natural and efficient. The members are competent, educated and professionals..."

#### Geography

Where in the world do Tek-Tips members come from?

# C++: Microsoft FAQ

 Forum Search FAQs Links Jobs Whitepapers MVPs

## Programming

 Binary for Beginners faq207-1582 Posted: 5 Mar 02 I have noticed a lot of questions lately regarding binary operations.  The first step to understanding binary, in my opinion, is how to think in binary.  The ability to look at a number in binary representation and know what its' value is.  Take 100110 for instance.  A quick look at it and you may say it is one hundred thousand, one hundred and ten.  Retraining the way you look at it is the first step.  One method of reading binary is to remember the following sequence (1,2,4,8,16,32,64,128...) which is a very easy sequence to remember.  All it consists of is multiplying the current number by 2 to get the next number.  With the bits, however, we have to retrain our eyes to read from right to left rather then the left to right we are used to.  The above sequence will also be reversed and here is how to apply it.Notice the bold character in the binary string.  The rest of the sentence will describe that single bit100110 represents the number 1100110 represents the number 2100110 represents the number 4100110 represents the number 8100110 represents the number 16100110 represents the number 32Now we can take the bit value and multiply it with its corresponding number in the sequence.  For this we will use the formula ((Binary Value A) * (Sequence Value A)) + ((Binary Value B) * (Sequence Value B)).  Expanding on this we could represent any number of bits as(bv1*sv1) + (bv2*sv2) + (bv3*sv3)… + (bvn-1* svn-1) + (bvn * svn)Applying this to the above we get(0*1) + (1*2) + (1*4) + (0*8) + (0*16) + (1*32) OR (1*2) + (1*4) + (1*32) simplified.  2+4+32 = 38 = 100110 in binary.Now, onto the C/C++ aspect of this FAQ.Binary Operations:Left Shift:     <>Bitwise And:     &Bitwise OR:    |Bitwise Not:    ~Bitwise Xor:    ^Truth Tables:NOT               X  |  Q----------0  |  11  |  0AND                NANDX  Y  |  Q        X  Y  |  Q----------        ----------0  0  |  0        0  0  |  10  1  |  0        0  1  |  11  0  |  0        1  0  |  11  1  |  1        1  1  |  0OR                NORX  Y  |  Q        X  Y  |  Q----------        ----------0  0  |  0        0  0  |  10  1  |  1        0  1  |  01  0  |  1        1  0  |  01  1  |  1        1  1  |  0XOR                XNORX  Y  |  Q        X  Y  |  Q----------        ----------0  0  |  0        0  0  |  10  1  |  1        0  1  |  01  0  |  1        1  0  |  01  1  |  0        1  1  |  1For the rest of this FAQ, the generic binary value variable will be known as bv and will be equal to 38 or 100110 in binary as shown above.  A second binary value will be used in some examples and will be obv for other binary value.  obv will be equal to 23 or 10111 in binary.  For simplicity purposes, we will only be using 8 bits representation but the methods shown here can be applied to shorts (16 bits), ints (32 bits on most systems), longs (32 bits) and 64 bit values as well if your machine supports them.  However, you must realize that these binary operations are only supported by integer types (i.e. (signed / unsigned) char, short, int, long and other types supported by your current compilier).Definition of NOT (~):NOT is the easiest of the binary operations to visualize.  All that happens when you NOT a value is all ones in the binary representation of the number become zeros and all zeros become ones.Example of NOT (~)~bv = ~(00100110) = 11011001 = (1*1) + (1*8) + (1*16) + (1*64) + (1*128) = 217Definition of AND (&):AND compares the respective bits of the values and if and only if (iff) both bits are a one, a one is set in the specific bit being compared in the value being computed.Example of AND (&)bv & obv = 100110 & 10111Not very easy to see when side by side... Lets look at it in full 8 bit representation with one value above the other0010011000010111Now it is a bit easier to see...  With the bit comparisons we get0010011000010111--------00000110 = (1*2) + (1*4) = 6Definition of NAND (NOT AND):Once the definition of AND is understood, this is very easy to understand.  All this means is you compute the AND value and once you have that value you apply the NOT operation to it.Example of NAND:~(bv & obv) = ~(100110 & 10111) = ~(00000110) = 11111001 = 249Definition of OR (|):This compares each bit of the two values and if either bit is a one, a one is set in the corresponding bit of the value being computed.Example of OR (|)bv | obv = (00100110 | 00010111) and once again this will be displayed as:0010011000010111--------00110111 = 1+2+4+16+32 = 52Definition of NORThis is like NAND in a way except the AND is replaced with an ORExample of NOR~(bv | obv) = ~(100110 | 10111) = ~(00110111) = (11001000) = 8+64+128 = 200Definition of XOR(^)This is know as the exclusive OR.  What it means is iff one of the two bits being compared is a one then set the corresponding bit in the result to a 1, otherwise set it to zero.Example of XOR(^)bv ^ obv = (100110 ^ 10111) 0010011000010111--------00110001 = 1+16+32 = 49Definition of XNORThis follows the examples above, simply NOT the result of the XORExample of XNOR~(bv ^ obv) = ~(00110001) = 11001110 = 2+4+8+64+128 = 206The Shift (<< >>) OperationsNow onto shifting.  Above we see << and >> as left shift and right shift respectively.  What the shift does is shift the bits to the left or right the desired amount of bits.  That definition is not too clear but the example should clarify things.bv << 1  will shift the bits left by 1 so:bv << 1 = (100110) << 1 = 1001100 = 76bv << 2 = 10011000 = 152NOW THINGS BECOME INTERESTING... remember we are working with ONLY 8 bits.bv << 3 = 00110000 = 48  Think of a cliff.  There is a line 8 feet away from the edge of the cliff.  Think of the 1's in bv as lemmings standing on the cliff.  Think of the LEFT shift operation as a move command to the lemmings to move left x number of feet where x is the value on the right of the shift opperation.00100110 represents a Lemming that is 3 feet away from the edge, 6 feet away from the edge and 7 feet away from the edge.  When we command them to move 3 feet to the left, one of them jumps off the edge, never to be seen again.  The other two have moved 3 feet to the left leaving them at their final position of 3 feet away and 4 feet away from the edge.  The right shift operation behaves exactly like the above except the cliff edge is on the right instead of the left.  bv >> 1 = 100110 >> 1 = 10011 = 19bv >> 2 = 1001 = 9I don't know if the pattern was clear but for every left shift of a value when NO LEMMINGS jump, we are actually multiplying by 2 and for every right shift we are dividing by two (Even when lemmings jump off the cliff)Well, I hope this clarified how binary operations work.  Thanks for reading it.Matt Back to C++: Microsoft FAQ Index Back to C++: Microsoft Forum

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!