0% found this document useful (0 votes)
10 views3 pages

Bitwise Operators

The document explains various bitwise operators in C programming, including Bitwise AND, OR, XOR, NOT, left shift, and right shift. Each operator is illustrated with code examples and their corresponding binary and decimal results. It also discusses the implications of using these operators, particularly with respect to signed and unsigned integers.

Uploaded by

pran2007verma
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)
10 views3 pages

Bitwise Operators

The document explains various bitwise operators in C programming, including Bitwise AND, OR, XOR, NOT, left shift, and right shift. Each operator is illustrated with code examples and their corresponding binary and decimal results. It also discusses the implications of using these operators, particularly with respect to signed and unsigned integers.

Uploaded by

pran2007verma
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

Dr.

Nafees Uddin

BITWISE OPERATORS
Bitwise AND (&)

The bitwise AND operator performs a logical AND operation on each pair of corresponding bits
of its two operands. A bit in the result is set to 1 only if both corresponding bits in the operands
are 1.

#include <stdio.h>

int main() {

int a = 12; // Binary: 1100

int b = 25; // Binary: 11001

int result = a & b; // Binary: 1000, which is 8 in decimal

printf("Result of a & b: %d\n", result);

return 0;

Bitwise OR (|)

The bitwise OR operator performs a logical OR operation on each pair of corresponding bits. A
bit in the result is set to 1 if at least one of the corresponding bits in the operands is 1.

#include <stdio.h>

int main() {

int a = 12; // Binary: 1100

int b = 25; // Binary: 11001

int result = a | b; // Binary: 11101, which is 29 in decimal

printf("Result of a | b: %d\n", result);

return 0;

JIMS Engineering Management Technical Campus


Dr. Nafees Uddin

Bitwise XOR (^)

The bitwise XOR (exclusive OR) operator performs a logical XOR operation on each pair of
corresponding bits. A bit in the result is set to 1 if the corresponding bits in the operands are
di9erent.

#include <stdio.h>

int main() {

int a = 12; // Binary: 1100

int b = 25; // Binary: 11001

int result = a ^ b; // Binary: 10101, which is 21 in decimal

printf("Result of a ^ b: %d\n", result);

return 0;

Bitwise NOT (~)

The bitwise NOT operator, or complement, is a unary operator that inverts each bit of its single
operand. A 0 becomes a 1 and a 1 becomes a 0.

#include <stdio.h>

int main() {

int a = 12; // Binary (assuming 32-bit integer): ...0000 1100

int result = ~a; // Binary: ...1111 0011 (Two's complement for negative numbers)

printf("Result of ~a: %d\n", result);

return 0;

The output of this code will be -13. The reason for this is that C uses two's complement to
represent negative numbers. The bitwise NOT of a positive number will flip all bits, which in
two's complement representation results in a negative number.

JIMS Engineering Management Technical Campus


Dr. Nafees Uddin

Left Shift (<<)

The left shift operator shifts the bits of the first operand to the left by the number of places
specified by the second operand. New bits are filled with 0s. This is equivalent to multiplying the
number by 2 raised to the power of the shift amount.

#include <stdio.h>

int main() {

int a = 12; // Binary: 1100

int result = a << 2; // Binary: 110000, which is 48 in decimal

printf("Result of a << 2: %d\n", result);

return 0;

Right Shift (>>)

The right shift operator shifts the bits of the first operand to the right by the number of places
specified by the second operand. The behavior of the vacated bits on the left depends on the
data type: for unsigned integers, 0s are filled, while for signed integers, the sign bit is typically
extended (arithmetic shift). This is equivalent to dividing the number by 2 raised to the power of
the shift amount.

#include <stdio.h>

int main() {

int a = 12; // Binary: 1100

int result = a >> 2; // Binary: 11, which is 3 in decimal

printf("Result of a >> 2: %d\n", result);

return 0;

JIMS Engineering Management Technical Campus

You might also like