Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR ( XOR )
~ One's complement
<< bitwise left shift
>> bitwise right shift
Bits of operand1 Bit of operand2 Resulting Bit
0 0 0
0 1 0
1 0 0
1 1 1
Note: 0x293B in 16 bits = 000000000000293B
a 0010 1001 0011 1011 (0x293B)
b 0001 1010 0010 1111 (0x1A2F)
----------------------------
a&b 0000 1000 0010 1011 (0x082B)
Program code ↓
#include<stdio.h>
int main()
{
int a, b;
printf("Enter values of a and b : ");
scanf("%d%d", &a, &b);
printf("a = %d\t\t", a);
printf("b = %d\t\t", b);
printf("a&b = %d\t\t", a&b);
return 0;
}
/*
if a = 8 and b = 9 then the output will be 8
1000
& 1001
-------
1000
*/
Bits of operand1 Bit of operand2 Resulting Bit
0 0 0
0 1 1
1 0 1
1 1 1
a 0010 1001 0011 1011 (0x293B)
b 0001 1010 0010 1111 (0x1A2F)
----------------------------
a&b 0011 1011 0011 1111 (0x3B3F)
Program code ↓
#include<stdio.h>
int main()
{
int a, b;
printf("Enter values of a and b : ");
scanf("%d%d", &a, &b);
printf("a = %d\t\t", a);
printf("b = %d\t\t", b);
printf("a&b = %d\t\t", a|b);
return 0;
}
/*
if a = 8 and b = 9 then the output will be 9
1000
| 1001
-------
1001
*/
Bits of operand1 Bit of operand2 Resulting Bit
0 0 0
0 1 1
1 0 1
1 1 0
a 0010 1001 0011 1011 (0x293B)
b 0001 1010 0010 1111 (0x1A2F)
----------------------------
a&b 0011 0011 0001 0100 (0x3314)
Program code ↓
#include<stdio.h>
int main()
{
int a, b;
printf("Enter values of a and b : ");
scanf("%d%d", &a, &b);
printf("a = %d\t\t", a);
printf("b = %d\t\t", b);
printf("a&b = %d\t\t", a^b);
return 0;
}
/*
if a = 8 and b = 9 then the output will be 1
1000
^ 1001
-------
0001
*/
Bits of operand Resulting Bit
0 1
1 0
a 0010 1001 0011 1011 (0x293B)
~a 1101 0110 1100 0100 (0xD6C4)
b 0001 1010 0010 1111 (0x1A2F)
~b 1110 0101 1101 0000 (0xE5D0)
Program code ↓
#include<stdio.h>
int main()
{
unsigned int a;
printf("Enter values of a : ");
scanf("%d", &a);
printf("a = %d\n", a);
printf("Signed result of ~a = %d\n", ~a);
printf("Unsigned result of ~a = %u\n", ~a);
return 0;
}
/*
if a = 1 then the output will be -2 (signed) and 4294967294 (unsigned)
*/
#include <stdio.h>
int main()
{
int a;
printf("Enter values of a : ");
scanf("%d", &a);
a = a << 2;
printf("a << 2 = %d\t\t", a);
return 0;
}
Output ↓
Enter values of a : 1
a << 2 = 4
#include <stdio.h>
int main()
{
int a;
printf("Enter values of a : ");
scanf("%d", &a);
a = a >> 2;
printf("a >> 2 = %d\t\t", a);
return 0;
}
Output ↓
Enter values of a : 9
a << 2 = 2
#include <stdio.h>
int main()
{
int a = 10, b = 7;
printf("The value of a and b before swap : a = %d & b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("The value of a and b after swap : a = %d & b = %d\n", a, b);
return 0;
}
#include <stdio.h>
void decimalToBinary(int num)
{
int binary[32];
int i = 0;
// Convert decimal to binary using bit manipulation
while (num > 0)
{
binary[i] = num & 1;
num >>= 1;
i++;
}
printf("Binary representation: ");
// Print binary representation in reverse order
for (int j = i - 1; j >= 0; j--)
{
printf("%d", binary[j]);
}
printf("\n");
}
void checkEvenOdd(int num)
{
// Check the least significant bit to determine even/odd
if ((num & 1) == 0)
{
printf("The number is even.\n");
}
else
{
printf("The number is odd.\n");
}
}
int main()
{
int decimalNum;
printf("Enter a decimal number: ");
scanf("%d", &decimalNum);
decimalToBinary(decimalNum);
checkEvenOdd(decimalNum);
return 0;
}
#include <stdio.h>
int isBitSet(int num, int bitPos)
{
int mask = 1 << bitPos;
return ((num & mask) > 0);
}
int main()
{
int num = 10; // Binary: 0000 1010
// Check if bit at position 3 is set
if (isBitSet(num, 3))
printf("Bit at position 3 is set.\n");
else
printf("Bit at position 3 is not set.\n");
return 0;
}
#include <stdio.h>
// Function to set a specific bit
int setBit(int num, int bitPos)
{
int mask = 1 << bitPos;
return num | mask;
}
int main()
{
int num = 10; // Binary: 0000 1010
// Set bit at position 2
printf("Initially number was : %d\n", num);
num = setBit(num, 2);
printf("After setting bit at position 2 : %d\n", num);
return 0;
}
Output ↓
Initially number was : 10
After setting bit at position 2 : 14
#include <stdio.h>
// Function to clear a specific bit
int clearBit(int num, int bitPos)
{
int mask = 1 << bitPos;
return num & ~mask;
}
int main()
{
int num = 10; // Binary: 0000 1010
// Clear bit at position 1
num = clearBit(num, 1);
printf("After clearing bit at position 1: %d\n", num);
return 0;
}
Output ↓
After clearing bit at position 1: 8
#include <stdio.h>
// Function to toggle a specific bit
int toggleBit(int num, int bitPos)
{
int mask = 1 << bitPos;
return num ^ mask;
}
int main()
{
int num = 10; // Binary: 0000 1010
// Toggle bit at position 0
num = toggleBit(num, 0);
printf("After toggling bit at position 0: %d\n", num);
return 0;
}
Output ↓
After toggling bit at position 0: 11