C Play with Bits Set 1

Ques: 1 What does following segment code do?
//n is a integer variable
if(n&(n-1))
printf("Yes");
else
printf("No");
A. Prints yes if a number is prime
B. prints yes if a number is power of 2
C. prints no if a number is prime
D. prints no if number is power of 2
E. None of these
    
Description
Answer: D
if  n&(n-1)=0 then the number is power of 2.Consider the example n=8
8 in binary form is 1000  and n-1=7 in binary form is 0111.
Now when we perform 8&7 answer is 0000.

Ques: 2 Output if base address of the array is 2686664 and integer takes 4 bytes?
#include<stdio.h>
int main()
    {
        int a = 4;
        if (a=a >> 1)
           printf("%d\n", a);
    }
A. 1
B. 2  
C. 8
D. No output.
Description
Answer: B
 >> is right shift operator.it shift n number of bytes to right. Hence 4 in binary form is 0100. 
 Shifting 1 byte towards left results in 0010 which is equivalent to 2.
 Note:Shifting n bytes of a number to right is equivalent to dividing number by 2n.

Ques: 3 What will be the output of the following program?
#include<stdio.h>
int main()
    {
        int a = 4;
        if (a=a << 1)
           printf("%d\n", a);
    }
A. 4
B. 8
C. 2
D. No Ouput
Description
Answer: B
 << is left shift operator.Hence 1 byte is shifted towards left.
 Note: Shifting n bytes of number towards lefty is equivalent to multiplying the number by 2(power n).

Ques: 4 What will be the output of the following program?
#include<stdio.h>
int main(void)
{
	int x=0x1F;
        x<<2;
	printf("%X  ",x);
	x>>2;
	printf("%X\n",x);
	return 0;
}
A. 7C 1F
B. 124 31
C. Error
D. 1F 1F
Description
Answer: D
Tricky? x<<2 and x>>2 does not affect the value of x because the value
after shifting the bitsis not assigned to x.

Ques: 5 What will be the output of the following program?
#include<stdio.h>
int main(void)
{
	int k;
	k=((3<<4)^(96>>1));	
	printf("%d\n",k);
	return 0;
}
A. 48
B. 1
C. lvalue required
D. 0
Description
Answer: D

Ans zero.


Ques: 6 What will be the output of the following program?
#include<stdio.h>
void main()
    {
        int a = 6, b = -8, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d %d %d %d", a, b, c, d);
    }
A. 6 -8 1 0
B. 6 -8 1 1
C. 7 -7 0 1
D. 7 -7 1 0
Description
Answer: C
&& has higher priority. So && gets executed first (++a&&++b)||++c.
->  ++a=7
->  ++b=-7
->  7&&-7=1
-> Now 1||++c gets evaluated and which results into 1.


Ques: 7 What will be the output of the following program?
#include<stdio.h>
void main()
    {
        int a = -1, b = 5, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d %d %d %d", a, b, c, d);
    }
A. 0 6 1 1
B. 0 5 1 1 
C. 0 6 1 0
D. 0 5 1 0
Description
Answer: B
Expression is evaluated as (++a&&++b)||c
++a=0
since ++a has evaluated to zero, irrespective of what is the result of++b ,
the condition is going to be false. Hence compiler does not evaluate ++b.
Hence b is not increased.
Now 0||c++ results into true .Hence 1 is assigned to d.


Ques: 8 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a = 1, b = 5, c = -1, d;
    d = ++a && ++b || ++c;
    printf("\n%d %d %d %d", a, b, c, d);
}
A. 2 6 0 1
B. 2 6 0 0
C. 2 6 -1 0
D. 2 6 -1 1
Description
Answer: D
Since && has higher priority over || ,condition is evaluated as (++a &&++b)||++c
++a=2
++b=5
2&&5=1
now since condition at left of || operartor is true 
irrespective of result of ++c it is going to be true.
Therefore ++c is not evaluated.
Hence c=-1 d=1.

Ques: 9 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x = -2;
    x = x >> 1;
    printf("%d\n", x);
}
A. -1
B. 1
C. 2
D. -2
Description
Answer: A
    Negative numbers are stored in 2's complement form.
binary form of 2 in 4 bits is 0010.
to find 2's complement of 2 we find 1's complement and then add 1.

                  0010
1's complement    1101
		    	+1
2's complement    1110

Now shifting 1 bit to right gives 0111.Now this number is in 2's complement(a negative number).
Now take 2's complement of this number to geet the answer.
 
                  0111
1's complement    0000
add 1             0001
which is equal to 1. And put minus sign because it is a negative number.


comments powered by Disqus