**Home**>

**Freshers**>

**Technical Interview Questions**>

**C**>

**Q & A**

# 10. What is meant by "bit masking"?

**Answer:**

Bitmasking refers to the technique of representing a subset of a set using bits of a number .

for eg suppose we have n numbers {1,2,3,4,5,6,7,8} in an array

then a bitmask to indicate the subset{2,3,6} of the above set will be a set of binary values like {0,1,1,0,0,1,0,0} where 1 indicates element at ith index belong to set and 0 indicated that it doesnt.

this 0,1 information can also be stored in bits of a number.

for eg consider a number mask=0;

if we want to set the ith bit of mask to 1 we can apply following operation

mask = mask|(1<*
where '|' is bitwise or and '<<' is bitwise left shift operator
and the ith bit will become 1 indicating the number is included in the set
similarly to remove a number from the set following operating can be performed
mask = mask^(1<*

*where '^' is bitwise xor operator*

for eg suppose we have n numbers {1,2,3,4,5,6,7,8} in an array

then a bitmask to indicate the subset{2,3,6} of the above set will be a set of binary values like {0,1,1,0,0,1,0,0} where 1 indicates element at ith index belong to set and 0 indicated that it doesnt.

this 0,1 information can also be stored in bits of a number.

for eg consider a number mask=0;

if we want to set the ith bit of mask to 1 we can apply following operation

mask = mask|(1<

where '|' is bitwise or and '<<' is bitwise left shift operator

and the ith bit will become 1 indicating the number is included in the set

similarly to remove a number from the set following operating can be performed

mask = mask^(1<

where '|' is bitwise or and '<<' is bitwise left shift operator

and the ith bit will become 1 indicating the number is included in the set

similarly to remove a number from the set following operating can be performed

mask = mask^(1<

*where '^' is bitwise xor operator*for eg suppose we have n numbers {1,2,3,4,5,6,7,8} in an array

then a bitmask to indicate the subset{2,3,6} of the above set will be a set of binary values like {0,1,1,0,0,1,0,0} where 1 indicates element at ith index belong to set and 0 indicated that it doesnt.

this 0,1 information can also be stored in bits of a number.

for eg consider a number mask=0;

if we want to set the ith bit of mask to 1 we can apply following operation

mask = mask|(1<

where '|' is bitwise or and '<<' is bitwise left shift operator

and the ith bit will become 1 indicating the number is included in the set

similarly to remove a number from the set following operating can be performed

mask = mask^(1<

where '|' is bitwise or and '<<' is bitwise left shift operator

and the ith bit will become 1 indicating the number is included in the set

similarly to remove a number from the set following operating can be performed

mask = mask^(1<

*where '^' is bitwise xor operator*