# Bitwise Operators in C

This is an important topic for interviews in the embedded domain. In this tutorial, we will see  Bitwise Operators in C and its Interview questions.

## Bitwise Operators

Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
<< Shift left
>> Shift right

### Bitwise AND operator (&)

The output of bitwise `AND` is 1 if the corresponding bits of both operands are 1. If either bit of an operand is 0, the result of the corresponding bit is evaluated to 0.

Let us consider the example, the bitwise `AND` operation of two integers 36 and 13.

36 = 00100100 (In Binary)
13 = 00001101  (In Binary)

Bit Operation of 36 and 13

00100100
00001101                          (&)
________________
00000100 = 4 (In decimal)

### Bitwise OR operator (|)

The output of bitwise `OR` is 1 if at least one corresponding bit of two operands is 1. In C Programming, the bitwise OR operator is denoted by `|`.

Let us consider the example, the bitwise `OR` operation of two integers 36 and 13.

36 = 00100100 (In Binary)
13 = 00001101  (In Binary)

Bit Operation of 36 and 13

00100100
00001101                          (|)
________________
00101101 = 45 (In decimal)

### Bitwise XOR (exclusive OR) operator (^)

The result of the bitwise `XOR` operator is 1 if the corresponding bits of two operands are opposite. It is denoted by `^`.

36 = 00100100 (In Binary)
13 = 00001101  (In Binary)

Bit Operation of 36 and 13

00100100
00001101                          (^)
________________
00101001 = 41 (In decimal)

### Bitwise complement operator (~)

Bitwise complement operator is a unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by `~`.

### Right Shift Operator

The Right shift operator shifts all bits towards the right by a certain number of specified bits. It is denoted by `>>`.

212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)

### Left Shift Operator

The Left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted by `<<`.

212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

That is all about the Bitwise operator. Now, we will see the common questions asked in the interview.

## Bitwise Operators in C Interview Questions

1. How to set a particular bit in a variable or Register?

To set any bit in a variable, Use the `OR` operator.

`Variable = Variable | (1<<x)   (or)   Variable |= (1<<x)`
Here, ‘`x`‘ indicates the position of the bit
Example: Set the 4th bit in given register A.

A = A | (1<<4)         (or)        A |= (1<<4)

2. How to clear a particular bit in a number?

To clear any bit in a variable, Use (`AND` ) and (`NEG` (`~`)) operators.

`Variable = Variable & ~(1<<x)    (or)    Variable &= ~(1<<x)`

`x`‘ indicates the position of the bit

Example: clear the 5th bit in given register B.

`B = B & ~(1<<5)     (or)      B &= ~(1<<5) `

3. How to toggle or flip a particular bit in a number?

To toggle any bit in a variable, Use (^ ) exclusive OR operator.

`Variable = Variable ^ (1<<x)    (or)     Variable ^= (1<<x)   `

`x`‘ indicates the position of the bit

Example: Toggle the 2nd bit in given register B.

`B = B ^ (1<<2)       (or)        B ^= (1<<2)`

4.How to check particular bit is set or not in a number?

To check any bit in a variable, Use (`&` ) And Operator.

`Variable & (1<<x)`

`x`‘ indicates the position of the bit

Example: Check the 3rd bit is set or not.

```if((variable & (1<<3)) == 0) {
"bit is not set"
} else {
"bit is set"
}```

5. How to represent the above all things in MACRO for Real time code?

```#define SET_BIT(Variable, bit_position)    Variable |= (1<< bit_position)

#define CLEAR_BIT(Variable, bit_position)    Variable &= ~(1<< bit_position)

#define TOGGLE_BIT(Variable, bit_position)    Variable ^= (1<< bit_position)

#define CHECK_BIT_IS_SET_OR_NOT(Variable, bit_position)    Variable & (1<< bit_position)```

6. Write MACRO to Swap the bytes in 16bit Integer Variable.

`#define ByteSwap16(Value) ((Value & 0x00FF) << 8) | ((Value & 0xFF00) >> 8)`

7. Write MACRO to Swap the bytes in 32bit Integer Variable.

```#define ByteSwap32(Value) ((Value & 0x000000FF) << 24) |
((Value & 0x0000FF00U) <<  8) |
((Value & 0x00FF0000U) >>  8) |
((Value & 0xFF000000U) >> 24)```

8. Write MACRO to Swap the bytes in 64bit Integer Variable.

```#define ByteSwap64(Value)   ((Value & 0x00000000000000FFU) << 56) |
((Value & 0x000000000000FF00U) << 40) |
((Value & 0x0000000000FF0000U) << 24) |
((Value & 0x00000000FF000000U) <<  8) |
((Value & 0x000000FF00000000U) >>  8) |
((Value & 0x0000FF0000000000U) >> 24) |
((Value & 0x00FF000000000000U) >> 40) |
((Value & 0xFF00000000000000U) >> 56)```

You can also read the below tutorials. 