Bitmap Lab

bitmap-lab

Learning repository for bitmap operations and kernel near C programming. Focus on understanding memory and pixels with documentation first.

Visit Github

project info

Status
Learning
Mode
Terminal
Updated
Jan 2026

toolchain

Cgcc / clangmakelinux

Test // Understanding Shifting

This Program demonstrates bitwise shifting operations in C. It includes examples of left and right shifts, illustrating how to create bit masks and manipulate individual bits within an integer.

program source

utsab@linux:~/bitmap-lab/test/T01_UnderstandingShifting.c

// bit masks using left and right shifts

#include <stdio.h>

int main() {
    unsigned int x = 0b00001111;
    unsigned int y = 0b00010000;

    printf("Mask for bit 3: 0b%08b", 1 << 3);
    // left shift of binary 1 by 3 positions

    printf("Mask for bit 4: 0b%08b", y>>2);
    // right shift of y by 2 positions

    printf("Mask for bit 6: 0b%08b", x<<2);
    // left shift of x by 2 positions

    return 0;
}

void print_bits(unsigned int x) {
    for (int i = 7; i >= 0; i--)
        printf("%d", (x >> i) & 1);
}
GitHub

explanation

initial values

x is 0b00001111 (lower 4 bits set).

y is 0b00010000 (only bit 4 set).

▸ both are unsigned int so shifts are logical, not arithmetic.

mask for bit 3

▸ expression: 1 << 3.

▸ starting from 00000001, shifting left 3 times gives 00001000.

▸ this mask can later be used with |, & or ^ to set/clear/toggle bit 3.

mask for bit 4 via right shift

▸ expression: y >> 2 where y = 0b00010000.

▸ shifting right by 2 moves the single 1 from bit 4 to bit 2: 00000100.

▸ this shows that right shifts can also be used to reposition masks.

mask for bit 6 from x

▸ expression: x << 2 where x = 0b00001111.

▸ left shifting by 2 multiplies by 2^2 = 4, giving 0b00111100.

▸ bits that move past the most significant position are discarded; no wrap around.

helper: print_bits

print_bits loops from bit 7 down to 0 and prints each bit using (x >> i) & 1.

▸ in real C, there is no standard %b format specifier, so you would usually call print_bits(mask) instead of using %08b.