Bit Wise Challenges
Bit Wise Challenges
Output:
5) Left Shift
x = x << n;
6) Right Shift
x = x >> n;
Program for the above:
Output:
Output:
11) Rotate Bits (Left)
int rotate_left = (x << n) | (x >> (sizeof(x)*8 - n));
12) Rotate Bits (Right)
int rotate_right = (x >> n) | (x << (sizeof(x)*8 - n));
Output:
13) Isolate Rightmost 1-Bit
int rightmost_set_bit = x & (-x);
14) Clear Rightmost 1-Bit
x = x & (x-1);
Program for the above:
Output:
15) Multiply by 2:
x = x << 1;
16) Divide by 2:
x = x >> 1;
Program for the above:
Output:
17) Flip all bits:
x = ~x;
18) Compute 2^x:
int result = 1 << x;
19) Check if a number is a power of 2:
Output:
20) Turn off all bits except the rightmost set bit:
x = x & (-x);
Output:
Output:
x ^= MASK;
Output:
28) Determine if two integers have the same sign:
bool sameSign = (x ^ y) >= 0;
Output:
30) Merge bits from two values according to a mask:
int result = (x & mask) | (y & ~mask);
Program for the above:
Output:
31) Detect if a number has consecutive bits set:
bool hasConsecutiveOnes = (x & (x << 1)) != 0;
Program for the above:
Output:
32) Count Set Bits
count = 0;
while(x) {
count += x & 1;
x >>= 1;
}
Program for the above:
Output:
33) Swap Bits
Output:
34) Detect if Two Integers have Opposite Signs
bool opposite = (x ^ y) < 0;
Program for the above:
Output:
35) Get Absolute Value without Branching
int mask = x >> (sizeof(int) * 8 - 1);
int abs_val = (x + mask) ^ mask;
Program for the above:
Output:
36) Check if a number is even or odd:
if (x & 1) {
// odd
} else {
// even
}
Program for the above:
Output:
37) Find Minimum or Maximum without branching:
int y;
int minimum = y ^ ((x ^ y) & -(x < y));
int maximum = x ^ ((x ^ y) & -(x < y));
Program for the above:
Output:
38) Swap two numbers without a temporary variable:
x = x ^ y;
y = x ^ y;
x = x ^ y;
Program for the above:
Output:
39) Compute modulus division by a power of 2:
Output:
40) Find average of two numbers without overflow:
int average = (x & y) + ((x ^ y) >> 1);
Program for the above:
Output:
41) Check if all bits in a number are 0 (number is zero):
bool isZero = !x;
Output:
43) Compute the next higher power of 2:
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
Output:
44) Setting Bits in a Range:
For setting bits from position p to q:
Output:
45) Testing Multiple Bits:
To check if specific bits are set/cleared:
bool areBitsSet(unsigned int x, unsigned int mask) {
return (x & mask) == mask;
}
Output:
Program for the above:
46) Conditional Bit Set/Clear:
Setting or clearing a bit based on a condition (often used in peripheral configurations):
if (condition) {
x |= (1 << BIT_POS); // Set bit if condition is true
} else {
x &= ~(1 << BIT_POS); // Clear bit if condition is false
}
Output:
Program for the above:
47) Masking Interrupts:
To disable certain interrupts by setting respective bits in interrupt mask registers:
#define INTERRUPT_MASK_ADDR 0x40021050
(volatile unsigned int)INTERRUPT_MASK_ADDR |= (1 << INTERRUPT_BIT); //
Mask interrupt
Program for the above:
Output:
48) Endianness Swap:
In situations where data endianness matters (e.g., communication between different
systems):
unsigned int swapEndianness(unsigned int x) {
return ((x >> 24) & 0xFF) |
((x << 8) & 0xFF0000) |
((x >> 8) & 0xFF00) |
((x << 24) & 0xFF000000);
}
Output:
Program for the above:
49) Bounded Increment:
Incrementing a bounded counter (common in timers and PWM modules):
x = (x + 1) & BOUND_MASK;
Program for the above:
Output:
Output:
52) Bit Fields Manipulation in Structs:
struct ControlRegister {
unsigned int mode: 3;
unsigned int enable: 1;
unsigned int flag: 1;
unsigned int reserved: 27;
};
Output:
Program for the above:
53) Parity Check:
bool hasEvenParity(unsigned int x) {
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x ^= x >> 2;
x ^= x >> 1;
return !(x & 1);
}
Program for the above:
Output:
54) Counting Bits in a Byte:
const unsigned char BitsSetTable256[256] = {...};
unsigned int countSetBits(unsigned int x) {
return BitsSetTable256[x & 0xFF] +
BitsSetTable256[(x >> 8) & 0xFF] +
BitsSetTable256[(x >> 16) & 0xFF] +
BitsSetTable256[x >> 24];
}
Program for the above:
Output:
Output:
Program for the above:
56) Low-Level Port Manipulation:
#define PORT_ADDR 0x40021000
#define PIN_MASK (1 << 5)
(volatile unsigned int)PORT_ADDR |= PIN_MASK;
Program for the above:
Output:
57) Bit Extraction:
unsigned int extractBits(unsigned int x, int p, int q) {
return (x >> p) & ((1 << (q-p+1)) - 1);
}
Program for the above:
Output:
58) Boundary Checks:
bool isWithinBoundary(unsigned int x, unsigned int boundary) {
return (x & (boundary - 1)) == x;
}
Program for the above:
Output:
59) Bit Rotation:
unsigned int rotateLeft(unsigned int x, int n) {
return (x << n) | (x >> (sizeof(x) * 8 - n));
}
Program for the above:
Output:
Output:
Program for the above:
61) Memory-Mapped Register Manipulation:
Output:
Program for the above:
62) Toggle a GPIO Pin:
#define GPIO_PIN_ADDR 0x40021015
*(volatile unsigned int*)GPIO_PIN_ADDR ^= 1; // Toggle pin
Program for the above:
Output:
63) Peripheral Enable (common in MCU configurations):
#define PERIPH_ENABLE_ADDR 0x40021020
#define PERIPH_BIT 3
*(volatile unsigned int*)PERIPH_ENABLE_ADDR |= (1 << PERIPH_BIT); // Enable
peripheral
Output:
64) Changing Bit Fields (common in MCU register configurations):
Output:
65) Reading a Specific Bit Field:
#define STATUS_REGISTER_ADDR 0x40021040
#define STATUS_MASK 0x0E // Assuming bits 1-3 are status bits
unsigned int status = (*(volatile unsigned int*)STATUS_REGISTER_ADDR &
STATUS_MASK) >> 1;
Program for the above:
Output:
Written By: Yashwanth Naidu Tikkisetty
Happy learning.
Learn together, Grow together.
T Yashwanth Naidu
https://github.com/T-Yashwanth-Naidu