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:
7is00111in binary, and19is10011.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:
0xFFFFhas the lowest 16 bits set to1.(x >> 5) << 5clears the rightmost 5 bits.(y >> 3) << 3clears the rightmost 3 bits.(z >> 2) << 2clears the rightmost 2 bits.displayBitsloops 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$ (00110000in binary).96 >> 1= $96 \div 2^1 = 48$ (00110000in 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:
xstarts as0x1F. Bothx<<2;andx>>2;compute shifted results that are immediately discarded, leavingxunchanged.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$ througharr_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:
0xA01Dends inD(1101in binary).mask = 1 << 3creates00001000(8).num & maskyields00001000.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
posby shifting the target bit to position 0 first and masking with1.Breakdown:
0x1Fis00011111in binary.num >> 3shifts the bits right by 3 positions, resulting in00000011.& 1isolates 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) & 1pattern inside a loop from bit 31 down to 0 to print every bit from most significant to least significant.Breakdown:
0xA0DFin binary is1010 0000 1101 1111, padded with 16 leading zeros to fill 32 bits. Note thatnumis unchanged across iterations because>>does not reassignnum.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
numstationary and walks a single1bit (mask) from the most significant bit (bit 31) down to bit 0.Breakdown:
maskis declaredunsigned intso thatmask >> 1performs a logical right shift (filling with0s from the left rather than sign-extending a1).0x1A3Bis0001 1010 0011 1011in 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
maskfixed at the most significant bit (1 << 31) and shiftsxleft by 1 bit (x <<= 1) on every iteration so each bit takes its turn in the leftmost slot.Breakdown:
0x1AE3is0001 1010 1110 0011in binary. After printing the 31st bit,xshifts left, moving the 30th bit into position 31, and so on, adding a space after every 8 bits.Output:
