Tag Archives: Bitwise operator

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

Continue reading C program to rotate bits of a number

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.

  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 >>

This exercises focuses on mastering bitwise operators. After this exercise you will surely gain some confidence using bitwise operators.

Continue reading Bitwise operator programming exercises and solutions in C

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

Continue reading C program to swap two numbers using bitwise operator

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

Continue reading C program to convert decimal to binary number system using bitwise operator

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

Continue reading C program to count zeros and ones in a binary number

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)

Continue reading C program to count leading zeros in a binary number

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

Continue reading C program to get lowest order or first set bit of a number

C program to get highest order set bit of a number

Write a C program to input any number from user and find highest order set bit of given number using bitwise operator. How to find the highest order of the set bit of a given number using bitwise operator in C programming. Logic to get highest order set bit of a number in C programming.

Example

Input

Input any number: 22

Output

Highest order set bit in 22 is 4.

Continue reading C program to get highest order set bit of a number