 # Bit wise Operators in C

This is the important topic for interview in embedded domain. In this tutorial we will see  Bitwise Operators in C Interview Questions .

# Bit wise 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 corresponding bit is evaluated to 0.

Let us suppose 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, bitwise OR operator is denoted by |.

Let us suppose 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                          (|)
________________
00101101 = 45 (In decimal)

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

The result of 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 compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.

## Right Shift Operator

Right shift operator shifts all bits towards right by 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

Left shift operator shifts all bits towards left by 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 interview questions asked in interview.

# Bitwise Operators in C Interview Questions

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

To set any bit in a variable, Use (OR ) operator.

`Variable = Variable | (1<<x)   (or)   Variable |= (1<<x)`
‘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 particular bit in a number?

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

`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 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.

5. How to represent the above in MACRO’s for Real time code?

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

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

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

5 1 vote
Article Rating
Subscribe
Notify of This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Inline Feedbacks NITEEN THAKARE
November 21, 2017 10:06 PM

for expression b=a+++++a
o/p a=3;b=4.
and for expression b=++a+a++
o/p a=3;b=5
how the values differ please tells me which operand have high precedence.
How associativity take place between operator ?

1
0