Bitwise operator programming exercises and solutions in C

Bitwise operators are used extensively in programming whenever we need to operate on bit level of the value. Bitwise operator works on each bit of the data. There are operators are used for multiple purposes such as - setting or resetting flag values, testing the status of bit, complementing, or shifting bits either to left or right. So whenever you need to work on the lowest level of data we use bitwise operators. There are six types of bitwise operators:
  1. Bitwise AND operator &
  2. Bitwise OR operator |
  3. Bitwise XOR operator ^
  4. Bitwise complement operator ~
  5. Bitwise left shift operator <<
  6. Bitwise right shift operator >>
Below is a set of programming exercises that can be used by a beginner or an intermediate programmer to master their skills on bitwise operator.
  1. Write a C program to check Least Significant Bit (LSB) of a number is set or not.
  2. Write a C program to check Most Significant Bit (MSB) of a number is set or not.
  3. Write a C program to get nth bit of a number.
  4. Write a C program to set nth bit of a number.
  5. Write a C program to clear nth bit of a number.
  6. Write a C program to toggle nth bit of a number.
  7. Write a C program to get highest set bit of a number.
  8. Write a C program to get lowest set bit of a number.
  9. Write a C program to count trailing zeros in a binary number.
  10. Write a C program to count leading zeros in a binary number.
  11. Write a C program to flip bits of a binary number using bitwise operator.
  12. Write a C program to total number of zeros and ones in a binary number.
  13. Write a C program to convert decimal to binary number system using bitwise operator.
  14. Write a C program to swap two numbers using bitwise operator.
  15. Write a C program to check whether a number is even or odd using bitwise operator.


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment