Bitwise operations

i learning c++ as first program language and i cant understand the logic of
the bitwise operations pls explain to what they are and do
tnx guys
all numbers in computers are in binary. One binary digit is called "bit"
https://en.wikipedia.org/wiki/Binary_number
1310 will be 11012; 1010 will be 10102
Bitwise operator will take corresponding bits and apply corresponding logical operation as if 1 were true and 0 were false.
So 13&10 (bitwise and) will become
 1101
 &&&&
 1010
=1000
10002 = 810

13|10 (bitwise or) will become:
 1101
|1010
=1111
11112 = 1510

13^10 (bitwise xor, a^b equals true if only one operand is true) will become:
 1101
^1010
=0101
01012 = 510
Last edited on
Bitwise operations work at the bit level, i.e. the individual "digits" of a byte.

One of the functions of bitwise operators is to work with bitflags like the flags used with std::cout and std::cin.

Bitwise operators (examples are not in the C++ variable format):
-AND (&)
This returns a bitwise 1 if and only if the two bits being compared are both 1.
Useful for finding if a bitflag is set.

3 & 2
   0 0 1 1
&  0 0 1 0
-----------
   0 0 1 0
--> 3 & 2 == 2

-OR (|)
This returns a bitwise 1 if and only if at least one of the bits being compared is 1.
Good for switching on a bitflag while leaving the others untouched.

3 | 2
   0 0 1 1
|  0 0 1 0
-----------
   0 0 1 1
--> 3 | 2 == 3

-Complement (~)
This reverses the bit.

~127
~  0 1 1 1 1 1 1 1
---------------------
   1 0 0 0 0 0 0 0
--> ~127 == 128

-XOR (^)
Exclusive-or returns a bitwise 1 if and only if the two bits being compared are different.
Useful for switching a bitflag to either state while leaving the rest untouched.

3 ^ 2
   0 0 1 1
^  0 0 1 0
-----------
   0 0 0 1
--> 3 ^ 2 == 1

-Left shift (<<)
This shifts all bits one digit to the left, effectively multiplying the integer by two. The number of times the bits are shifted is determined by the second parameter.

3 << 2
   0 0 1 1
<<       2
-----------
   1 1 0 0
--> 3 << 2 == 12 == 3*2*2

-Right shift (>>)
This shifts all bits one digit to the right, effectively dividing the integer by two. The number of times the bits are shifted is determined by the second parameter.

7 >> 2
   0 1 1 1
>>       2
-----------
   0 0 0 1
--> 7 >> 2 == 1 == 7/(2*2)


A good table I've found is:

operator &
   0 & x = 0
   1 & x = x
operator |
   0 | x = x
   1 | x = 1
operator ^
   0 ^ x = x
   1 ^ x = ~x

Last edited on
thanx for the better explanation but why do i need those operators and those calculations
give me 1 example of situation i really need this
The link I posted gives a couple of examples, data encryption and data compression. If you didn't bother to read that page, I highly recommend it.
When dealing with flag bitfields:
1
2
3
4
5
6
7
8
const int8_t   start    = 00000001b; //b is custom binary literal here
const int8_t no_report  = 0000001b; 
const int8_t block_entry = 00000100b; 

int8_t bitfield = start|no_report; //setting flags
bitfield &= ~no_report; //clear no_report flag
bitfield ^= block_entry; //flips block_entry flag;
process x(bitfield); //start some process with start and block_entry flags set. 
You might already saw its usage in file streams: std::ofstream(file, ios::out|ios:ate);

Or for optimization http://www.hackersdelight.org/hdcode.htm
Algorithm for finding first zero value is used in strlen() function. And sometimes it even gets advantage of SSE
http://www.hackersdelight.org/hdcodetxt/zbytel.c.txt

Just a quick example (for "game making"):

Let us create types of monsters:
1
2
3
const int EARTH=0x1; // in binary 0001
const int WATER=0x2; // in binary 0010
const int FIRE=0x4; // in binary 0100 


Let's assume we can hit ONLY EARTH-type of monster:

1
2
if (attack_type&EARTH)
  do_attack(); // type is EARTH=do attack 



EDIT: notice:
- WATER&EARTH returns 0
-FIRE&EARTH returns 0

since none of corresponding bits of equation are 1's

EDIT 2: fixed &&->&
Last edited on
attack_type&&EARTH It should be single & for bitwise application.
Topic archived. No new replies allowed.