0% found this document useful (0 votes)
27 views55 pages

Bit Wise Challenges

The document presents a comprehensive guide on bitwise operations, detailing 65 challenges designed to enhance programming skills in this area. Each challenge includes specific operations such as Bitwise OR, AND, NOT, and various techniques for manipulating bits, including setting, clearing, and toggling bits. It also covers advanced topics like endianness, memory-mapped register manipulation, and counting bits, making it a valuable resource for embedded systems programming.

Uploaded by

suresampats
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views55 pages

Bit Wise Challenges

The document presents a comprehensive guide on bitwise operations, detailing 65 challenges designed to enhance programming skills in this area. Each challenge includes specific operations such as Bitwise OR, AND, NOT, and various techniques for manipulating bits, including setting, clearing, and toggling bits. It also covers advanced topics like endianness, memory-mapped register manipulation, and counting bits, making it a valuable resource for embedded systems programming.

Uploaded by

suresampats
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Embedded Bitwise Blueprint

65 Challenges to Perfect Your Skills


1) Bitwise OR
result = x | y;
2) Bitwise NOT
x = ~x;
3) Bitwise AND
result = x & y;
4) Bitwise XOR
result = x ^ y;
Program for the above:

Output:
5) Left Shift
x = x << n;

6) Right Shift
x = x >> n;
Program for the above:

Output:

7) Setting a Bit (Bitwise OR)


x = x | (1 << n);
8) Clearing a Bit (Bitwise AND)
x = x & ~(1 << n);
9) Toggling a Bit (Bitwise XOR)
x = x ^ (1 << n);
10) Checking a Bit
bit = (x & (1 << n)) != 0;
Program for the above:

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

Program for above:

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:

bool isPowerOf2 = x && (!(x & (x - 1)));


Program for the above:

Output:
20) Turn off all bits except the rightmost set bit:
x = x & (-x);

21) Turn on all bits to the right of rightmost set bit:


x = x | (x-1);
Program for the above:

Output:

22) Bit Masking


int masked_value = x & mask;

23) Setting Multiple Bits:

#define MASK 0x0F


x |= MASK; // Set lower 4 bits

24) Clearing Multiple Bits:


#define MASK 0xF0
x &= ~MASK; // Clear upper 4 bits
Program for the above:

Output:

25) Selective Bit Inversion:


Invert specific bits (useful in scenarios like manipulating control registers):

x ^= MASK;

26) Compute the Sign of an Integer:

int sign = (x >> 31) | !!x;

27) Conditional Negation:

int r = (x >> 31 & (y ^ z)) ^ z;


Program for the above:

Output:
28) Determine if two integers have the same sign:
bool sameSign = (x ^ y) >= 0;

29) Isolate the rightmost bit that is set to 1:


int rightmostBit = x & (-x);
Program for the above:

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

if (((x >> p) & 1) != ((x >> q) & 1)) {


x ^= (1 << p) | (1 << q);
}
Program for the above:

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:

int divisor = 4; // (which is 2^2)


int result = x & (divisor - 1);
Program for the above:

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;

42) Check if only a single bit is set in a number:


bool isSingleBitSet = x && !(x & (x-1));
Program for the above:

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

Program for the above:

Output:
44) Setting Bits in a Range:
For setting bits from position p to q:

unsigned int setRange(unsigned int x, int p, int q) {


unsigned int mask = ((1 << (q-p+1)) - 1) << p;
return x | mask;
}
Program for the above:

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:

50) Hardware Loop Unrolling for Bitwise Operations:


For operations on arrays of bits, unroll loops to make the code more predictable and often
faster:
for (int i = 0; i < ARRAY_SIZE; i+=4) {
arr[i] |= MASK;
arr[i+1] |= MASK;
arr[i+2] |= MASK;
arr[i+3] |= MASK;
}
Output:
Program for the above:
51) Power-of-Two Boundary Alignment:
Aligning addresses/data on power-of-two boundaries is often crucial for memory access
and DMA transfers:
x = (x + BOUNDARY - 1) & ~(BOUNDARY - 1);
Program for the above:

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:

55) Bit Reversal:


unsigned int reverseBits(unsigned int x) {
x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
return (x << 16) | (x >> 16);
}

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:

60) Integer Average without Overflow:


int average = (x & y) + ((x ^ y) >> 1);

Output:
Program for the above:
61) Memory-Mapped Register Manipulation:

#define REGISTER_ADDR 0x40021000


#define BIT_POS 5
*(volatile unsigned int*)REGISTER_ADDR |= (1 << BIT_POS); // Set bit
*(volatile unsigned int*)REGISTER_ADDR &= ~(1 << BIT_POS); // Clear bit

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

Program for the above:

Output:
64) Changing Bit Fields (common in MCU register configurations):

#define CONFIG_REGISTER_ADDR 0x40021030


#define FIELD_MASK 0x70 // Assuming bits 4-6 represent a field
#define FIELD_VALUE 0x50 // New value for bits 4-6
*(volatile unsigned int*)CONFIG_REGISTER_ADDR =
(*(volatile unsigned int*)CONFIG_REGISTER_ADDR & ~FIELD_MASK) |
FIELD_VALUE;

Program for the above:

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.

Follow me to receive updates regarding Embedded Systems.


Connect with me on LinkedIn: https://www.linkedin.com/in/t-yashwanth-
naidu/

T Yashwanth Naidu

https://github.com/T-Yashwanth-Naidu

You might also like