# Bit Operators in C++

0
72
This entry is part 5 of 6 in the series Learn C++

Free4Dev – In digital computer programming, a bit operator operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations – from Wikipedia.

## `|` – bitwise OR

See this example to understand OR bitwise:

Ouput

Why?

A bit wise OR operates on the bit level and uses the following Boolean truth table:

When the binary value for `a` (`0101`) and the binary value for `b` (`1100`) are OR’ed together we get the binary value of `1101`:

The bit wise OR does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `|=`:

## `^` – bitwise XOR (exclusive OR)

Output

Why?

A bit wise XOR (exclusive or) operates on the bit level and uses the following Boolean truth table:

Notice that with an XOR operation `true XOR true = false` where as with operations `true AND/OR true = true`, hence the exclusive nature of the XOR operation.

Using this, when the binary value for `a` (`0101`) and the binary value for `b` (`1001`) are XOR’ed together we get the binary value of `1100`:

The bit wise XOR does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `^=`:

The bit wise XOR can be utilized in many ways and is often utilized in bit mask operations for encryption and compression.

Note: The following example is often shown as an example of a nice trick. But should not be used in production code (there are better ways `std::swap()` to achieve the same result).

You can also utilize an XOR operation to swap two variables without a temporary:

To productionalize this you need to add a check to make sure it can be used.

So though it looks like a nice trick in isolation it is not useful in real code. xor is not a base logical operation,but a combination of others: `a^c=~(a&c)&(a|c)`

also in 2015+ compilers variables may be assigned as binary:

## `&` – bitwise AND

Output

Why?

A bit wise AND operates on the bit level and uses the following Boolean truth table:

When the binary value for `a` (`0110`) and the binary value for `b` (`1010`) are AND’ed together we get the binary value of `0010`:

The bit wise AND does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `&=`:

## `<<` – left shift

Output

Why?

The left bit wise shift will shift the bits of the left hand value (`a`) the number speciﬁed on the right (1), essentially padding the least signiﬁcant bits with 0’s, so shifting the value of `5` (binary `0000 0101`) to the left 4 times (e.g. `5 << 4`) will yield the value of `80` (binary `0101 0000`). You might note that shifting a value to the left 1 time is also the same as multiplying the value by 2, example:

But it should be noted that the left shift operation will shift all bits to the left, including the sign bit, example:

Possible output: `a = 2147483647`, `b = -2`

While some compilers will yield results that seem expected, it should be noted that if you left shift a signed number so that the sign bit is aﬀected, the result is undeﬁned. It is also undeﬁned if the number of bits you wish to shift by is a negative number or is larger than the number of bits the type on the left can hold, example:

The bit wise left shift does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `<<=`:

## `>>` – right shift

Output

Why?

The right bit wise shift will shift the bits of the left hand value (a) the number speciﬁed on the right (1); it should be noted that while the operation of a right shift is standard, what happens to the bits of a right shift on a signed negative number is implementation deﬁned and thus cannot be guaranteed to be portable, example:

It is also undeﬁned if the number of bits you wish to shift by is a negative number, example:

The bit wise right shift does not change the value of the original values unless speciﬁcally assigned to using the bit wise assignment compound operator `>>=`:

You may also like:

Series NavigationPrevious: Floating Point Arithmetic in C++Next: Bit Manipulation in C++

This site uses Akismet to reduce spam. Learn how your comment data is processed.