N&1 operator masking

The & operator provides a mask that “cancels out” bits in the first depending if they’re set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will “blank” the first set of bits, leaving you with N as 00000001.

Can you please explain why & operator performs masking?

@rjra2611 the bit wise and of 0 and 1 is 0. so if you want to know the first 4 bits of a number only then you can put 1 on the first 4 bit and then 0 on the rest and if you do bitwise & now the first 4 digits would be the original digit and rest will be zero and in a way we have formed a mask over them.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.