C/C++ Bitwise operations

The most fundamental operations of the computer are:

[dot] AND[dot] OR [dot] NOT[dot] moving data

All other operations (including Exclusive OR (XOR) are built on these.

Binary Operations take two pieces of data of the same size and compare them bit-by-bit, i.e., compare the right-most (least significant) bit of the first data piece with the least significant bit of the second data piece, and so on through the most significant bit of each.

 
 
---------------------------------------------------       

[dot]AND ( & )

The AND operation, represented in C++ as  &  , checks to see if the corresponding bits are BOTH set to 1.  If they are, the answer bit is set to 1; if not, the answer bit is set to 0. For example:
  char a = '\05', b = '\03';
would give us, in binary,

a = 00000101 (5)
b = 00000011 (3)
a & b = 00000001 (1)

since the only bit that is set to 1 in both a AND b is the right-most (least significant) bit.
Both a and b remain unchanged.

[dot]OR ( | )

The OR operation, represented in C++ as  |  , checks to see if either of the corresponding bits are set to 1.  If one, the other or both are, the answer bit is set to 1; if not, the answer bit is set to 0. For example:
  char a = '\05', b = '\03';
would give us, in binary,

a = 00000101 (5)
b = 00000011 (3)
a | b = 00000111 (7)

since the three right-most (least significant) bits are set to 1 in one, the other OR both a and b .
Both a and b remain unchanged.

 

[dot]XOR (^)

The Exclusive OR (XOR) operation, represented in C++ as  ^ , checks to see if the corresponding bits are set to different values.  If one or the other but not both are set to 1, the answer bit is set to 1; if not, the answer bit is set to 0; i.e., a ^ b = (a | b) & ~(a & b) . For example:
  char a = '\05', b = '\03';
would give us, in binary,

a = 00000101 (5)
b = 00000011 (3)
a | b = 00000110 (6)

since the three right-most (least significant) bits are set to 1 in one, the other but not both a and b .
Both a and b remain unchanged.

Unary Operations look at one piece of data, evaluating it on the value of each individual bit.

[dot]NOT ( ~ )

The NOT operation, also called One's Complement, is represented in C++ as  ~  . It checks to see if each bit is set to 1 or 0.  If the bit is set to 0, the answer bit is set to 1; if the bit is set to 1, the answer bit is set to 0. For example:
  char a = '\05';
would give us, in binary,

a = 00000101 (5)
~a = 11111010 (250)

since the only bit that is set to 1 in both a AND b is the right-most (least significant) bit.
a remains unchanged.

Moving Data:

[dot]Left-Shift ( << )

The Left-Shift operation, represented in C++ as  <<  ,  moves bits in the answer a specified number places to the left, bringing zeros in on the right. For example:
  char a = '\05';
would give us, in binary,

a = 00000101 (5)
a << 2 = 00010100 (20)

giving the "101" pattern moved two places to the left, with zeros in the last two places.
a remains unchanged.

[dot]Right-Shift ( >> )

The Right-Shift operation, represented in C++ as  >>  ,  moves bits in the answer a specified number places to the right, bringing zeros in on the left. For example:
  char a = '\05';
would give us, in binary,

a = 00000101 (5)
a >> 2 = 00000001 (1)

giving the "101" pattern moved two places to the right (dropping off the last two digits), with zeros in the first two places.
a remains unchanged.


All of these operators have accompanying assignment operators: &=, |=, ~=, ^=, <<=, >>=.

[previous] [Top][Index] [syllabus]

[Edmonds Community College]


For Further information, contact pbladek@edcc.edu


copyright©2003,2005 Edmonds Community College