• Rainy Season
• World
Wednesday - June 5, 2024
#Computer Science #Algorithm #Blog #C Program

# What are Bitwise operators in C and Python?

## Bitwise operators

### What is Bitwise operators

Bitwise operators in c are used to carry out operations on integer operands at the bit level. Using these operators, you may set or clear certain bits, move bits left or right, and carry out logical operations at the bit level. They change the binary representation of numbers. The C language has six bitwise operators:

### Bitwise AND (‘&'):

• combines two integers using a bitwise AND operation.
• When both operands are 1, the result has a 1 in each slot; otherwise, it has a 0.
• Example:
int a = 5;   // Binary: 0101
int b = 3;   // Binary: 0011
int result = a & b; // Binary result: 0001 (Decimal: 1)

### Bitwise OR ('|‘):

• combines two integers using a bitwise OR technique.
• Each bit location where at least one of the operands has a 1 has a 1 in the result.
• Example:
int a = 5;   // Binary: 0101
int b = 3;   // Binary: 0011
int result = a | b; // Binary result: 0111 (Decimal: 7)

### Bitwise XOR (‘^‘):

• Bitwise XOR (exclusive OR) is used to combine two numbers.
• Where precisely one of the operands, but not both, contains a 1, the result has a 1 in that bit location.
• Example:
int a = 5;   // Binary: 0101
int b = 3;   // Binary: 0011
int result = a ^ b; // Binary result: 0110 (Decimal: 6)

### Bitwise NOT ('~‘):

• on a single integer, performs a bitwise NOT (complement) operation.
• converts 0s to 1s and 1s to 0s for each bit.
• Example:
int a = 5;   // Binary: 0101
int result = ~a; // Binary result: 1010 (Decimal: -6, depends on the system's representation of negative numbers)

### Left Shift ('<<‘):

• a given number of places to the left, shifting the bits of the left operand.
• replacing empty spaces with zeros.
• Example:
int a = 5;       // Binary: 0101
int result = a << 2; // Binary result: 010100 (Decimal: 20)

### Right Shift ('>>‘):

• Shifts the bits of the left operand to the right by a specified number of positions.
• The behaviour depends on the sign of the left operand for signed integers (arithmetic right shift fills with the sign bit).
• For unsigned integers, it fills vacated positions with 0s.
• Example:
int a = -5;       // Binary: 11111111111111111111111111111011 (Two's complement)
int result = a >> 2; // Binary result: 11111111111111111111111111111110 (Decimal: -2, system-dependent)

When fine-grained control over bits is needed, bitwise operators are frequently employed in low-level programming for things like device drivers, embedded systems, and networking. They are also employed to implement certain algorithms and data structures effectively.

## Explain bitwise operators in C.

• C has the distinction of supporting special bitwise operators for manipulating data at the bit level. These operators are used for testing the bits or shifting them right or left. Bitwise operators may not be applied to float or double.

#### & (Bitwise AND):

• & is known as the bitwise AND. This operator takes two operands. It applies AND operation on each bit of value.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Left shift
>> Right shift

Example:

void main()
{
int a= 10, b=12, c;
c=a&, b;
printf ("%d", c);
}
Output:
c=8

a ---  1 0 1 0
&
b ---  1 1 0 0
-----------------
c ---  1 0 0 0

#### | (Bitwise OR):

• | is known as the Bitwise OR. This operator takes two operands. It performs an OR operation on each bit of the value.

Example:

void main()
{
int a=10, b=12, c;
/ c=a/ b;
printf ("%d", c);
}
Output:
c=14

a ---  1 0 1 0
|
b ---  1 1 0 0
-----------------
c ---  1 1 1 0

#### ^ (Bitwise exclusion OR):

• ^ is known as the Bitwise exclusive OR. This operator takes operads. It performs exclusive OR operation on each bit of value.

Example:

void main()
{
int a=10, b=12, c;
c=a / b;
printf ("%d", c);
}
Output
c = 6

a ---  1 0 1 0
^
b ---  1 1 0 0
-----------------
c ---  0 1 1 0

### Sizeof Operator

• In our program, at any instance, we may want to find the bytes occupied by the operand.
• To do this, C provides the operator called sizeof, ” sizeof is the operator which is used to return the number of bytes occupied by the operand.”
• The operand may be a variable or constant of a data type.
• Example:
int a, arr[10];
float f, ff[10];
printf("%d %d %d %d", sizeof(arr), sizeof(f), sizeof(ff), sizeof(chart):
Output:
2 20 4 40 1

## Bitwise operators in Python

Bitwise operators are used in Python to change specific integer bits. The following bitwise operations are supported by Python:

### Bitwise AND ('&‘):

• performs a bitwise AND operation between two integers’ corresponding bits.
• When both operands are 1, the result has a 1 in each slot; otherwise, it has a 0.
• Example:
a = 5    # Binary: 0101
b = 3    # Binary: 0011
result = a & b  # Binary result: 0001 (Decimal: 1)

### Bitwise OR ('|'):

• between the equivalent bits of two numbers, performing a bitwise OR.
• Each bit location where at least one of the operands has a 1 has a 1 in the result.
• Example:
a = 5    # Binary: 0101
b = 3    # Binary: 0011
result = a | b  # Binary result: 0111 (Decimal: 7)

### Bitwise XOR ('^‘):

• XORs (exclusive OR) are the appropriate bits of two integers in a bitwise fashion.
• Where precisely one of the operands, but not both, contains a 1, the result has a 1 in that bit location.
• Example:
a = 5    # Binary: 0101
b = 3    # Binary: 0011
result = a ^ b  # Binary result: 0110 (Decimal: 6)

### Bitwise NOT ('~‘):

• on a single integer, performs a bitwise NOT (complement) operation.
• converts 0s to 1s and 1s to 0s for each bit.
• Example:
a = 5    # Binary: 0101
result = ~a  # Binary result: 1010 (Decimal: -6, depending on the system's representation of negative numbers)

### Left Shift (‘<<'):

• a given number of places to the left, shifting the bits of the left operand.
• replacing empty spaces with zeros.
• Example:
a = 5       # Binary: 0101
result = a << 2  # Binary result: 10100 (Decimal: 20)

### Right Shift (‘>>’):

• a given number of places to the right, shifting the bits of the left operand.
• The behaviour of signed integers relies on the sign (the sign bit is filled via arithmetic right shift).
• It replaces empty spots for unsigned integers with 0s.
• Example:
a = -5      # Binary: 11111111111111111111111111111011 (Two's complement)
result = a >> 2  # Binary result: 11111111111111111111111111111110 (Decimal: -2, system-dependent)

When it comes to low-level bit manipulation, the implementation of specific algorithms, and working with hardware interfaces, Python’s bitwise operators are frequently employed. They are utilized in Python, but not as frequently as in certain other languages, such as C or C++.