AG Anvesh G 29 Nov 2026 15 views 0 comments C Programming

C Bitwise Operators Explained: 10 Practice Programs with Output

E.1: Bitwise vs. Logical Operators


#include<stdio.h>
int main(void)
{
int x=7,y=19;
printf("%d %d ",x&y,x&&y);
printf("%d %d ",x|y,x||y);
printf("%d\n",x^y);
return 0;
}
  • Concept: Compares bitwise operators (&, |, ^) against logical operators (&&, ||).

  • Breakdown: 7 is 00111 in binary, and 19 is 10011.

    • x & y (Bitwise AND): 00111 & 10011 =

    • x && y (Logical AND): 

    • x | y (Bitwise OR): 

    • x || y (Logical OR): 

    • x ^ y (Bitwise XOR): 

  • Output: 

E.2: Clearing Rightmost Bits via Shifting


#include<stdio.h>
void displayBits(int x);
int main(void)
{
unsigned int x,y,z;
displayBits(0xFFFF);
x=y=z=0xFFFF;
x=(x>>5)<<5; displayBits(x);
y=(y>>3)<<3; displayBits(y);
z=(z>>2)<<2; displayBits(z);
return 0;
}
void displayBits(int x)
{
int i,mask;
for(i=31; i>=0; i--)
{
mask = 1<<i;
putchar((x & mask)?'1':'0'); /*Test and print ith bit*/
if(i%8==0)
putchar(' '); /*Space after 8 bits*/
}
printf("\n");
}
  • Concept: Right-shifting an unsigned integer drops the rightmost bits, and left-shifting it back by the same amount fills those vacated positions with zeros.

  • Breakdown: 0xFFFF has the lowest 16 bits set to 1.

    • (x >> 5) << 5 clears the rightmost 5 bits.

    • (y >> 3) << 3 clears the rightmost 3 bits.

    • (z >> 2) << 2 clears the rightmost 2 bits.

    • displayBits loops from bit 31 down to 0, using a shifting mask (1 << i) to inspect and print each bit with spaces every 8 bits.

  • Output:


E.3: Shift Multiplication, Division, and XOR


#include<stdio.h>
int main(void)
{
int k;
k=((3<<4)^(96>>1));
printf("%d\n",k);
return 0;
}
  • Concept: Left shifting by $n$ multiplies by $2^n$; right shifting by $n$ divides by $2^n$.

  • Breakdown:

    • 3 << 4 = $3 \times 2^4 = 48$ (00110000 in binary).

    • 96 >> 1 = $96 \div 2^1 = 48$ (00110000 in binary).

    • 48 ^ 48 (XOR of two identical numbers) always evaluates to 0.

  • Output: 

E.4: Shift Operators Without Assignment

#include<stdio.h>
int main(void)
{
int x=0x1F;
x<<2;
printf("%X ",x);
x>>2;
printf("%X\n",x);
return 0;
}
  • Concept: Bitwise shift operators (<<, >>) evaluate to a new value without mutating the original variable unless an assignment operator (<<=, >>=, or =) is used.

  • Breakdown: x starts as 0x1F. Both x<<2; and x>>2; compute shifted results that are immediately discarded, leaving x unchanged.

  • Output: 

E.5: Binary Representation Using a Precomputed Lookup Table


#include<stdio.h>
int main(void)
{
unsigned int arr_mask[]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200,
0x400, 0x800, 0x1000,0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000, 0x80000,
0x100000, 0x200000,0x400000, 0x800000, 0x1000000, 0x2000000, 0x4000000, 0x8000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000};
int i,x=0x54038F;
for(i=31; i>=0; i--)
x&arr_mask[i] ? putchar('1'): putchar('0');
return 0;
}
  • Concept: Prints the 32-bit binary representation of a number by testing each bit against a static array of single-bit masks (arr_mask[0] = $2^0$ through arr_mask[31] = $2^{31}$).

  • Breakdown: Starting from index 31 down to 0, x & arr_mask[i] isolates the $i$-th bit. If the result is non-zero, '1' is printed; otherwise '0' is printed.

  • Output: 

E.6: Extracting a Specific Bit (Mask-and-Shift Method)


#include<stdio.h>
int main(void)
{
unsigned int num=0xA01D,pos=3,bit;
unsigned int mask=1<<pos;
bit=(num&mask)>>pos;
printf("%u\n", bit);
return 0;
}
  • Concept: Isolates a bit at index pos (0-indexed from the right) by creating a mask at that position, applying &, and shifting it back to the 0th bit position.

  • Breakdown: 0xA01D ends in D (1101 in binary).

    • mask = 1 << 3 creates 00001000 (8).

    • num & mask yields 00001000.

    • Shifting right by 3 (>> pos) moves that bit into the least significant position, giving 1.

  • Output: 

E.7: Extracting a Specific Bit (Shift-and-Mask Method)


#include<stdio.h>
int main(void)
{
unsigned int num=0x1F,pos=3,bit;
bit=(num>>pos)&1;
printf("%u\n",bit);
return 0;
}
  • Concept: An alternative, more concise way to read the bit at pos by shifting the target bit to position 0 first and masking with 1.

  • Breakdown: 0x1F is 00011111 in binary.

    • num >> 3 shifts the bits right by 3 positions, resulting in 00000011.

    • & 1 isolates the rightmost bit, yielding 1.

  • Output: 

E.8: Printing 32-Bit Binary by Shifting the Number Right


#include<stdio.h>
int main(void)
{
int i,num=0xA0DF;
for(i=31; i>=0; i--)
printf("%d",(num>>i)&1);
return 0;
}
  • Concept: Applies the (num >> i) & 1 pattern inside a loop from bit 31 down to 0 to print every bit from most significant to least significant.

  • Breakdown: 0xA0DF in binary is 1010 0000 1101 1111, padded with 16 leading zeros to fill 32 bits. Note that num is unchanged across iterations because >> does not reassign num.

  • Output: 

E.9: Printing 32-Bit Binary by Shifting the Mask Right


#include<stdio.h>
int main(void)
{
int i,num=0x1A3B;
unsigned int mask=1<<31;
for(i=31; i>=0; i--)
{
(num & mask) ? printf("1") : printf("0");
mask=mask>>1;
}
return 0;
}
  • Concept: Keeps num stationary and walks a single 1 bit (mask) from the most significant bit (bit 31) down to bit 0.

  • Breakdown: mask is declared unsigned int so that mask >> 1 performs a logical right shift (filling with 0s from the left rather than sign-extending a 1). 0x1A3B is 0001 1010 0011 1011 in binary.

  • Output: 

E.10: Printing 32-Bit Binary by Shifting the Number Left


#include<stdio.h>
void func(int x);
int main(void)
{
func(0x1AE3);
}
void func(int x)
{
int i,mask;
mask=1<<31;
for(i=1; i<=32; i++)
{
putchar((x&mask)?'1':'0');
x<<=1;
if(i%8==0)
putchar(' ');
}
printf("\n");
return 0;
}

  • Concept: Keeps mask fixed at the most significant bit (1 << 31) and shifts x left by 1 bit (x <<= 1) on every iteration so each bit takes its turn in the leftmost slot.

  • Breakdown: 0x1AE3 is 0001 1010 1110 0011 in binary. After printing the 31st bit, x shifts left, moving the 30th bit into position 31, and so on, adding a space after every 8 bits.

  • Output: 

Share:

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026
Topic-wise C Programming Interview Questions

C Programming Interview Questions – Topic Wise1. IntroductionC programming is one of the most important langu...

AG Anvesh G 30 May 2026