Many of you might be wondering what is the motive behind the page name! Well, I thought why not throw some light 🔦 on this 😄

Back to Basics: Shall we ?

A bit is a single Boolean value (0 or 1), small set(s) of which makes a bit-mask. A bit is said to be set if and only if it is '1'. For eg: in 10011, 1st, 2nd and 5th bits are set while 3rd and 4th are not.

'Mask' in bit-mask can be understood as a way to hide/show some information.

Let us understand this with an example! Let's say we have a set of 4 characters: {'a', 'b', 'c', 'd'} and we want to represent this using bits.

Here, we will create a mask of 4 bits _, _, _, _ where each position will tell us if we are referring(setting) that value or not.

Therefore, 1011 will represent acd since we set the bits to 1 for those positions. This is a very powerful technique and we can do many operations using this logic.

Some basic operations allowed are:

AND operator (&) : This operator will give a value of 1 when both the operands are 1. So, 1&1 = 1 but 1&0 = 0

OR operator (|) : This operator will give a value of 1 when either of the operands is 1. So, 1|0 = 1 but 0|0 = 0

XOR operator (^): This operator will give a value of 1 when both of the operands have different bit or value.

So 1 ^ 0 = 1, 0 ^ 1 = 1 but 0 ^ 0 = 0 and 1 ^ 1 = 0

Let's see these in action:

Question : Swap two number without using third variable

Solution:

``````a = 6 in decimal
= 0110 in binary
b = 4 in decimal
= 0100 in binary

Now we want to swap it.
a = a ^ b
= 0110 ^ 0100
= 0010
= 10 in decimal

b = a ^ b
= 0010 ^ 0100
= 0110
= 6 in decimal

a = a ^ b
= 0010 ^ 0110
= 0100
= 4 in decimal``````

Powerful, isn't it ? 💪

Well owning to this powerful property of bit masking, the page is named Bitmaskers!