Bit operations

© Parineeth M R

Question 28. Given an integer, a.) set a particular bit position to 1, b.) reset a particular bit position to 0 and c.) toggle a particular bit position


Let n be the integer and pos be the position of the bit

To set a bit to 1: n = n | (1 << pos)

To reset a bit to 0: n = n & ~(1 << pos)

To toggle a bit: n = n ^ (1 << pos)