echo '' ;

C Examples – Bitwise Operators

C Bitwise Operators (Can only be used on integrals, no floats, and doubles)

C BitWise Logic Operator

  • & AND
  • | OR
  • ^ XOR
  • ~ ONE’S COMPLEMENT (Unary Operator)
  •  

& AND

x = 193;            /*1100 0001*/
y = x & 0xf0;        /*1111 0000*/
y = 192;            /*1100 0000*/

| OR

x = 193;            /*1100 0001*/
y = x | 0xf0;        /*1111 0000*/
y = 241;            /*1111 0001*/

^ XOR

x = 193;            /*1100 0001*/
y = x ^ 0xf0;        /*1111 0000*/
y = 62;                /*0011 0001*/

~ ONE’S COMPLEMENT (Unary Operator)

x = 193;            /*1100 0001*/
y = ~x;                /*0011 1110*/
y = 62;                /*0011 1110*

Bit-wise Shift

  • << LEFT SHIFT
    x = 193; /*1100 0001*/
    y = x << 2;
    y = 4; /*0000 0100*/
  • >> RIGHT SHIFT
    x = 193;            /*1100 0001*/
    y = x >> 2;
    y = 48;                /*0011 0000*/
  • Example : A = 0101 1000 >>1 Output : 000010000 .
  • Reference Videos:
    1. https://www.youtube.com/watch?v=d0AwjSpNXR
  1. Special Operators


Exercise in C bitwise operators

1) Which is not a bit-wise operator?

  1. &
  2. |
  3. <<
  4. &&

Ans : 4


2) Predict the output of following program.

#include <stdio.h>
int main()
{
    int a=10;
    int b=2;
    int c;
    
    c=(a & b);
    printf("c= %d",c);
    return 0;
}
  1. c= 12   
  2. c= 10
  3. c= 2
  4. c= 0

Correct answer: 3
c= 2

Bitwise AND (&) operator copies bit(s), if they are exist both of the operands. Here, binary of a is “1010” and binary of b is “0010”. Thus, result of expression (a & b) is “0010” which is equivalent to 2 in Decimal.


3) Predict the output of following program.

#include <stdio.h>

#define MOBILE  0x01
#define LAPPY   0x02

int main()
{
    unsigned char  item=0x00;

    item |=MOBILE;
    item |=LAPPY;

    printf("I have purchased ...:");
    if(item & MOBILE){
        printf("Mobile, ");
    }
    if(item & LAPPY){
        printf("Lappy");
    }

    return 1;
}
  1. I have purchased …:
  2. I have purchased …:Mobile, Lappy
  3. I have purchased …:Mobile,
  4. I have purchased …:Lappy

Correct answer: 2
I have purchased …:Mobile, Lappy

Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is “0001” and binary of Macro LAPPY (0x02) is “0010”, then result of the expression item |=MOBILE; will be “0001” and second expression item |=LAPPY; will return “0011”. Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.


4) Predict the output of following program.

#include <stdio.h>

int main()
{
    char var=0x04;

    var = var | 0x04;
    printf("%d,",var);
    var |= 0x01;
    printf("%d",var);
    
    return 0;
}
  1. 8,9
  2. 4,5   
  3. 8,8
  4. 4,4

Correct answer: 2
4,5

Value of var is 0x04 (0100), Consider the expression var = var | 0x04 The OR (|) of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.


5) Predict the output of following program.

#include <stdio.h>

int main()
{
    char flag=0x0f;

    flag &= ~0x02;
    printf("%d",flag);

    return 0;
}
  1. 13
  2. d
  3. 22
  4. 10

Correct answer: 1
13

Consider the expression flag &= ~0x02 => flag = flag & (~0x02) => flag = 0x0f & (~0x02) => flag = D => flag =13.


6) Consider the given statement:

int x = 10 ^ 2What will be the value of x?

  1. 5
  2. 6
  3. 7
  4. 8

Correct answer: 4
8

XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:

a       b       (a^b)
_______________________

0       0       0
0       1       1
1       0       1
1       1       0

Here, binary of 10 is “1010” and binary of 2 is “0010”, then the result of statement (10 ^ 2) will be “1000”, which is equivalent to 8 in decimal.


7) Predict the output of following program.

#include <stdio.h>

int main()
{
    int x=10;
    
    x &= ~2;
    printf("x= %d",x);
    
    return 0;
}
  1. x= 10
  2. x= 8
  3. x= 12
  4. x= 0

Correct answer: 2
x= 8

The statement x &= ~2; will clear second bit from the value of 10, binary of x is “1010” and the binary of 2 is “0010”, thus this statement will clear second bit and returns “1000” that is equivalent to 8 in Decimal.


8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

  1. Bitwise AND (&)   
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise NOT (~)

Correct answer: 1
Bitwise AND (&)

Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

Consider the following program:

#include <stdio.h>

int main()
{
    int count;
    
    for(count=1; count<=10; count+=1)
        if(count & 1)
            printf("%2d is ODD number\n",count);
        else
            printf("%2d is EVEN number\n",count);
    
    return 0;
}

Output

 1 is ODD number
 2 is EVEN number
 3 is ODD number
 4 is EVEN number
 5 is ODD number
 6 is EVEN number
 7 is ODD number
 8 is EVEN number
 9 is ODD number
10 is EVEN number

9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

  1. (num & (1<<3))
  2. (num & 0x08)
  3. (num & 0x03)
  4. Both (1) and (2)

Correct answer: 4
Both (1) and (2)

The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3rd bit of num is High (set) or not.

Consider this program:

#include <stdio.h>

int main()
{
    int num;
    
    printf("Enter an integer number: ");
    scanf("%d",&num);
    
    if(num & (1<<3))
        printf("3rd bit is High (Set)\n");
    else
        printf("3rd bit is Low\n");
        
    return 0;
}

Output

First run:
Enter an integer number: 15 
3rd bit is High (Set) 

Second run:
Enter an integer number: 7
3rd bit is Low

Binary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3rd bit is high and in second case 3rd bit is low. [Count from 0]


10) Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

Choose the correct words…

  1. Multiplication and Division
  2. Division and Multiplication
  3. Multiplication and Remainder
  4. Remainder and Multiplication

Correct answer: 1
Multiplication and Division

Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.

Consider this program:

#include <stdio.h>

int main()
{
    int num;
    
    printf("Enter an integer number: ");
    scanf("%d",&num);
    
    printf("Multiplication by 2 = %d\n", (num<<1));
    printf("Division by 2 = %d\n",(num>>1));
        
    return 0;
}

Output

Enter an integer number: 100
Multiplication by 2 = 200 
Division by 2 = 50


NEXT of C Bitwise Operators

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading