# Bit Manipulation Tricks You Must Know

## Bit Manipulation

Bit manipulation is the process of applying logical operations on a sequence of bits to achieve a required result. It is an act of algorithmically manipulating bits or other pieces of data that are shorter than a word.

We use bit manipulation to change a bit or a group of bits, invert bits, set bits, clear bits, shift bits etc…

We use basic bitwise operators like **AND (&), OR(|), NOT(~), XOR(^), Left Shift(<<), Right Shift(>>)** etc.. to manipulate bits.

I’ll show you how to perform some basic bit manipulation tricks, which will help you to manipulate bits in whichever way you want to get required results.

All the binary numbers in the examples that I’ll use will show you will be in unsigned binary form.

## 1. Set bit

Setting a bit means turning a bit in a specified index to ** 1. **So necessarily it is turning a bit in

**th**

*k**index to*

**if it is**

*1***, or leaving it unchanged if it is already**

*0***.**

*1*Let’s look at the code to do that and understand how it is done.

Here,

Number = ** 10** =

**( In binary )**

*00001010*Mask is

**right-shifted by**

*1***(Index) positions =**

*2***( In binary )**

*00000100*We perform

*bitwise OR*on the

**and**

*number***, so it will set**

*mask***at the position of the number, where we have**

*1***in the mask.**

*1*## 2. Clear Bit

Clearing a bit means turning a bit in a specified index to ** 0. **So necessarily it is turning a bit in

**th**

*k**index to*

**if it is**

*0***, or leaving it unchanged if it is already**

*1*

*0.*Let’s look at the code to do that and understand how it is done.

Here,

Number = ** 10** =

**( In binary )**

*00001010*Mask is

**right-shifted by**

*1***(Index) positions and**

*3***=**

*Inverted*

*11110111*We perform

*bitwise AND*on the

**and**

*number***, so it will clear bit/set**

*mask***at the position of the number, where we have**

*0***in the mask.**

*0*## 3. Flip Bit

Flipping a bit means turning a bit in a specified index to the opposite/inverse of that bit. So necessarily it is turning a bit in ** k**th

*index to*

**if it is**

*0***, and turning to**

*1***if it is**

*1*

*0.*Let’s look at the code to do that and understand how it is done.

Here,

Number = ** 10** =

**( In binary )**

*00001010*Mask is

**right-shifted by**

*1***(Index) positions =**

*3***( In binary )**

*00001000*We perform

*bitwise XOR*on the

**and**

*number***, so it will flip the bit at that position ( sets**

*mask***to**

*0***or**

*1***to**

*1***).**

*0*## 4. Check Bit

It is used to check if a bit in a specified index is ** 1. **So necessarily it is checking if a bit in

**th**

*k**index is*

**If the bit in**

*1.***th index is**

*k***it returns**

*1***and if it is**

*1,***it returns**

*0*

*0.*Let’s look at the code to do that and understand how it is done.

Here,

Number = ** 10** =

**( In binary )**

*00001010*Check is

**right-shifted by**

*number***(Index) positions =**

*3***The number to be checked is now brought to rightmost position**

*00000001*

We perform

*bitwise AND*on

**and**

*check***, so it will return**

*1***if the checked bit is**

*1***and returns**

*1,***if the checked bit is**

*0*

*0.*## 5. Is Odd

Now let’s see how we can check whether a number is odd or even using bit tricks.

In an odd number the rightmost bit will always be ** 1**, and in an even number the rightmost bit will always be

**So we can check whether the rightmost bit is**

*0.***or**

*0***and return whether it is odd or even.**

*1*Let’s look at the code to do that and understand how it is done.

Here,

Number = ** 11** =

**( In binary )**

*00001011*We perform

*bitwise AND*on the

**and**

*number***, so it will return**

*1***if it is**

*1***and returns**

*odd,***if it is**

*0***.**

*even*## Conclusion

We looked into some common tricks and functions to perform basic manipulations on bitsets. I encourage anyone who is interested on this to further learn about bits and gain more knowledge on advanced topics.

We familiarized with :

*Setting Bits**Clearing Bits**Flipping Bits**Checking If a bit is Set**Checking if a number is Odd*

There are a lot more to learn in bit manipulations, which can be very helpful while doing programming tasks include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization.

If you found this useful, be sure to like and share. Do you find bit manipulation wonderful ? Let me know in the comments!