These are entirely different from the logical operators &&, ||, and !, which operate on whole truth values rather than individual bitsโ donโt mix them up!
Letโs see the bitwise operators in action. The helper function printBinary() below prints out the binary representation of an unsigned integer, one bit at a time, using the division/remainder approach you may have already seen:
Write a C function that prints the binary representation of an unsigned integer entered by the user. Instead of using division and remainders, you must use bitwise operators (specifically, right-shift and AND) to read and print the bits directly.
Write a C function that checks whether the \(k\)-th bit of an integer \(n\) is set (1) or not (0). The convention is to count bits from the right, starting at 0. In main(), ask the user to enter an integer as well as the bit position they want to check, call your function to evaluate the bit, and print the conclusion to the screen.
Write a C program to count the number of 1s in the binary representation of a number. Ask the user for an integer, pass it to a function countBits() that counts the set bits and returns this count, and print the result in main().