Write a C program to check even or odd using macro. How to check whether a given number is even or odd using macro in C program. Logic to check even or odd numbers using macro.
Bitwise operator
10 cool bitwise operator hacks and tricks every programmer must know
Bitwise operators are used to manipulate data at its lowest level (bit level). Data in memory (RAM) is organized as a sequence of bytes. Each byte is a group of eight consecutive bits. We use bitwise operators whenever we need to manipulate bits directly. In this post I will show you some cool bitwise operator hacks and tricks. These hacks will boost your programming skill.
C program to rotate bits of a number
Write a C program to input a number and rotate bits of number using bitwise shift operators. How to rotate bits of a given number using bitwise operator in C programming. Logic to left or right rotate bits of a number using bitwise shift operator in C program.
Example
Input
Input number = -15 Number of rotations = 2
Output
-15 left rotated 2 times = -57 -15 right rotated 2 times = 2147483644
Bitwise operator programming exercises and solutions in C
Data in the memory (RAM) is organized as a sequence of bytes. Each byte is a group of eight consecutive bits. Bitwise operators are useful when we need to perform actions on bits of the data.
C supports six bitwise operators.
- Bitwise AND operator
&
- Bitwise OR operator
|
- Bitwise XOR operator
^
- Bitwise complement operator
~
- Bitwise left shift operator
<<
- Bitwise right shift operator
>>
This exercises focuses on mastering bitwise operators. After this exercise you will surely gain some confidence using bitwise operators.
C program to swap two numbers using bitwise operator
Write a C program to input any two numbers from user and swap values of both numbers using bitwise operator. How to swap two number using bitwise operator in C programming. Logic to swap two numbers using bitwise operator in C programming.
Example
Input
Input first number: 22 Input second number: 65
Output
First number after swapping: 65 Second number after swapping: 22
C program to convert decimal to binary number system using bitwise operator
Write a C program to input any decimal number from user and convert it to binary number system using bitwise operator. How to convert from decimal number system to binary number system using bitwise operator in C programming. Logic to convert decimal to binary using bitwise operator in C program.
Example
Input
Input any number: 22
Output
Binary number: 00000000000000000000000000010110
C program to count zeros and ones in a binary number
Write a C program to input a number from user and count total number of ones (1s) and zeros (0s) in the given number using bitwise operator. How to count zeros and ones in a binary number using bitwise operator in C programming.
Example
Input
Input any number: 22
Output
Output number of ones: 3 Output number of zeros: 29
C program to count leading zeros in a binary number
Write a C program to input any number from user and find total number of leading zeros of the given number. How to find total leading zeros of a given number (in binary representation) using bitwise operator in C programming. Logic to count total leading zeros of a given number using C programming.
Example
Input
Input any number: 22
Output
Output leading zeros: 27 (using 4 byte signed integer)
C program to count trailing zeros in a binary number
Write a C program to input any number from user and count number of trailing zeros in the given number using bitwise operator. How to find total number of trailing zeros in any given number using bitwise operator in C programming.
Example
Input
Input any number: 22
Output
Trailing zeros: 1
C program to get lowest order or first set bit of a number
Write a C program to input any number from user and find lowest order set bit of given number using bitwise operator. How to find first set bit in a given number using bitwise operator in C programming. Logic to get first set bit of a number using C program.
Example
Input
Input any number: 22
Output
First set bit: 1