Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2- (Bitwise Operators) (I) Left shifting an unsigned int by 1 bit is equivalent

ID: 3778121 • Letter: 2

Question

2- (Bitwise Operators)

(I) Left shifting an unsigned int by 1 bit is equivalent to multiplying the

value by 2. Write a function power2 that takes two integer arguments number and pow and calculates

number * 2pow

Use the shift operator to calculate the result. Print the values as integers and as bits.

(II) What is the output of this C code?

#include <stdio.h>

1. void main()

2. {

3. int a = 5, b = -7, c = 0, d;

4. d = ++a && ++b || ++c;

5. printf(" %d%d%d%d", a, b, c, d);

6. }

a) 6 -6 0 0

b) 6 -5 0 1

c) -6 -6 0 1

d) 6 -6 0 1

(III) What is the output of this C code?

1. #include <stdio.h>

2. int main()

3. {

4. int x = 2;

5. int y = -4

6. x = x << 1;

7. y = y >> 1;

8. printf("%d %d ", x, y);

9. }

a) 4 -2

b) 1 -8

c) Depends on the compiler

d) Compiler error

Explanation / Answer

ii) a b c are incremented

d = 6 && -6 || 0

= 1010 && 10110 || 0000

= 10010 || 0000

= 10010

Option d

iii) x=0010

x<<1= 0100 = 4

y =-4 = - 0100

y>>1 = -0010 = -2

Option a

i)

int power2(number,pow)

{

int new_num;

new_num=number;

new_num=new_num<<pow;

printf("new number : %d",new_num);

printf("new number-binary : %b",new_num);

return new_num;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote