Given a positive integer N, the task is to perform the following sequence of operations on the binary representation of N in C.

  • Finding a bit: Find the Kth bit in binary representation of N.
  • Setting a bit: If Kth bit is 0, then set it to 1. Otherwise, leave it unchanged.
  • Clearing a bit: If Kth bit is 1, then clear it to 0. Otherwise, leave it unchanged.
  • Toggling a bit: If Kth bit is 1, then change it to 0 and vice-versa.
  • Modifying a bit: Replace the Kth bit with a given bit.

#bit magic #mathematical #technical scripter #bit algorithms

Find, Set, Clear, Toggle and Modify bits in C
1.45 GEEK