Answer to Question #205229 in C for Tom Boyle

Question #205229

When you are setting a bit flag (position "p"), what is the mask that you need to use and what is the operation required? What is supposed to be the mask value for reset and what is the operation? Give one example.


1
Expert's answer
2021-06-13T10:23:50-0400

A binary OR operation is used when setting the p'th bit on the source and a mask with the p'th bit set to 1 while other bits are set to 0 and the result is stored at the source.

Hence, (1 <<p) is the value of mask.


source = 11011011

p             = 3

mask = 2>>1 = 00001000
Mask | source = 11011011

A binary AND operation is performed on the source when resetting the p'th bit and mask will have the p'th bit set to 0 and all other bits set to 1.

Thus, ~(1 << p) is the value of the mask.



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS