Maybe you've stumbled across a perplexing and scary looking line of code:

```
((value << 2) & 32)
```

You don't know what this is and after some time scouring the internet for answers you come across what is known as bitwise operations. In this post, we'll go over all the bitwise operations possible in C and C++.

Bitwise operations are operations that operate on bit(s). If a bit is 1 then it is said to be "true" or "on". If it is a 0, then it is "false" or "off". Just like arithmetic operators, bitwise operators give a result when the computation is done.

## AND

For one bit values, the AND operator gives the result of "on" if both bits are in the "on" state. Otherwise, it produces the value of "off".

```
1 & 1 == 1
1 & 0 == 0
0 & 1 == 0
0 & 0 == 0
```

For bytes, it goes over each bit and applies the AND operation individually.

```
10000000 & 01111111 == 00000000
11000000 & 11100011 == 11000000
10100110 & 10000111 == 10000110
10101010 & 11011011 == 10001010
```

## OR

Denoted by `|`

in C and C++, this operation produces the state of "on" if any of the bit is in the "on" state.

```
1 | 1 == 1
1 | 0 == 1
0 | 1 == 1
0 | 0 == 0
```

Just like with the AND operator, the same thing is done with multi-bit values.

## XOR

Denoted by `^`

it is short for exclusive OR. This produces the "on" state if only ONE of the bits is true.

```
1 ^ 1 == 0
1 ^ 0 == 1
0 ^ 1 == 1
0 ^ 0 == 0
```

Operations applied to multi-bit values like bytes are done at the bit level just like with AND and OR.

## NEG

Negates the state of a bit. The symbol `~`

is used to denote this operation.

```
~1 == 0
~0 == 1
```

Same with multi-bit values.

## Bitmasks

This isn't an operation but something programmers use sometimes when dealing with bitwise operations. You can think of a bitmask as a filter to extract bits from a multi-bit value.

Say we have the byte `10100111`

and we want to get the first two bits. We can use a bitmask to accomplish this goal. A bit of 0 in bitmask means to ignore and 1 means to extract. In our case, the bitmask would like this: `000000011`

. To apply the bitmask, we use the AND operator.

```
10100111 & 00000011 == 00000011
```

## Shift operators

You've seen these before with `<<`

and `>>`

. They mean "shift to the left" and "shift to the right", respectively.

The operand on the left side is the value being shifted while the operand on the right side is the shift amount.

Unlike the other operators we have looked at, these only work on multi-bit values like bytes.

For demonstration purposes, we will have the value `11001011`

to play around with.

These operators shift each bit to the direction specified by the type of shifting operator used by a certain amount. Bits that are shifted out of the byte completely are discarded and padded with a zero on the opposite side.

```
11001011 << 1 == 10010110
11001011 >> 2 == 00110010
```

This was a quick article I decided to do after my main Using SDL2 post was released.

Edit:

Thanks to @ccmg for the corrections.

## Discussion

Can you give some examples when we need bitwise shifting? I’m writing JS and can’t figure out why would I need to use shift.

If you're writing code in higher level languages like JS or Python then the need for bitwise operations do not present themselves very often. Two examples requiring the use of bitwise operations are in these videos by Danial Shiffman:

youtube.com/watch?v=MlRlgbrAVOs

youtube.com/watch?v=meGcdIoTYgw

Some uses I can think for the shifting operators are:

Daniel Shiffman goes into more detail in this other video of his:

youtube.com/watch?v=oCBlwsY8sR4

Well, you use them when you want to know the values of bits. Tons of systems encode information on as few bits as possible to save space. Compression algorithms map character strings and numeric values to packed bit streams. Transmission protocols store flags and sometimes node addresses as some number of bits that aren't byte multiples. It's ubiquitous in embedded systems.

Shameless plug here, but I wrote an article not so long ago about the use cases of bitmasking. I used JavaScript for my examples if that helps you to figure out why you would need it. I also

highlyrecommend reading through the comments and discussion of the article. People mentioned a lot of use cases for bitmasking in general there.## Bitmasks: A very esoteric (and impractical) way of managing booleans

## Some Dood

They're useful for hash functions, and bitwise XOR can be used instead of integer addition but it's probably not actually any faster.

For example in audio processing, when manually extracting samples from byte arrays.

Couple of things i noticed

It should say:

is in the "on" state.any of the bitsActually, the value on the

of the operator is the value being shifted and the value on theleft sideis the number of times (or positions) it's going to be shifted.right sidethis article goes into more detail on how bitwise AND, OR, and XOR work in multi-bit values (specifically half-bytes) dev.to/emcain/bitwise-operations-o...