Basic Bit Manipulation Programs in C for Freshers

Basic Bit Manipulation Programs

# Program Name Description Resources
1 Check Even/Odd Determine if number is even or odd using bits GeeksforGeeks
2 Set a Bit Set a specific bit in a number GeeksforGeeks
3 Clear a Bit Clear a specific bit in a number GeeksforGeeks
4 Toggle a Bit Toggle a specific bit in a number GeeksforGeeks
5 Check Bit Status Check if a specific bit is set or not GeeksforGeeks
6 Count Set Bits Count number of set bits in a number GeeksforGeeks
7 Count Leading Zeros Count leading zeros in binary representation GeeksforGeeks
8 Count Trailing Zeros Count trailing zeros in binary representation GeeksforGeeks
9 Find Parity Check if number has odd or even parity GeeksforGeeks
10 Swap Bits Swap bits at given positions GeeksforGeeks
11 Is Power of 2 Check if number is power of two GeeksforGeeks
12 Is Power of 4 Check if number is power of four GeeksforGeeks
13 Find Missing Number Find missing number using XOR GeeksforGeeks
14 Find Odd Occurring Number Find number occurring odd times GeeksforGeeks
15 Reverse Bits Reverse bits of a number GeeksforGeeks
16 Add Without + Add two numbers without + operator GeeksforGeeks
17 Multiply Without * Multiply two numbers without * operator GeeksforGeeks
18 Divide Without / Divide two numbers without / operator GeeksforGeeks
19 Modulo Without % Find modulo without % operator GeeksforGeeks
20 Absolute Value Without - Find absolute value without - operator GeeksforGeeks
21 Swap Two Numbers Swap two numbers without temporary variable GeeksforGeeks
22 Find Minimum Find minimum of two numbers without branching GeeksforGeeks
23 Find Maximum Find maximum of two numbers without branching GeeksforGeeks
24 Detect Opposite Signs Check if two numbers have opposite signs GeeksforGeeks
25 Turn Off Rightmost Bit Turn off the rightmost set bit GeeksforGeeks
26 Rotate Bits Rotate bits of a number GeeksforGeeks
27 Next Power of 2 Find next higher power of 2 Leetcode
28 Previous Power of 2 Find previous lower power of 2 GeeksforGeeks
29 Swap Nibbles Swap nibbles in a byte GeeksforGeeks
30 Binary to Gray Convert binary to Gray code GeeksforGeeks
31 Gray to Binary Convert Gray code to binary GeeksforGeeks
32 Find Duplicates Find duplicates using bits GeeksforGeeks
33 Find Two Non-Repeating Find two non-repeating elements GeeksforGeeks
34 Generate Power Set Generate power set using bits GeeksforGeeks
35 Hamming Distance Calculate Hamming distance GeeksforGeeks
36 Count Total Set Bits Count set bits from 1 to n GeeksforGeeks
37 Find Position of MSB Find position of most significant bit GeeksforGeeks
38 Find Position of LSB Find position of least significant bit GeeksforGeeks
39 Binary Representation Print binary representation GeeksforGeeks
40 Count Flipped Bits Count bits to flip to convert A to B GeeksforGeeks
41 Is Sparse Number Check if number is sparse GeeksforGeeks
42 Longest Consecutive 1s Find longest consecutive 1s in binary GeeksforGeeks
43 Circular Shift Perform circular shift on bits GeeksforGeeks
44 Check Bit Pattern Check for alternating bit pattern GeeksforGeeks
45 Sum of Bit Differences Sum of bit differences among all pairs GeeksforGeeks
46 Magic Number Find nth magic number GeeksforGeeks
47 Rightmost Different Bit Find position of rightmost different bit GeeksforGeeks
48 Bitwise AND Range Bitwise AND of numbers in range GeeksforGeeks
49 XOR from 1 to N Calculate XOR from 1 to N GeeksforGeeks
50 Find Extra Character Find extra character using bits GeeksforGeeks

Bitwise Operators in C

# Operator Description Example
1 & (AND) Bitwise AND operation 5 & 3 = 1
2 | (OR) Bitwise OR operation 5 | 3 = 7
3 ^ (XOR) Bitwise XOR operation 5 ^ 3 = 6
4 ~ (NOT) Bitwise complement ~5 = -6
5 << (Left Shift) Left shift bits 5 << 1 = 10
6 >> (Right Shift) Right shift bits 5 >> 1 = 2

Related Resources