5 votes

What is the output of the following C program?

#include<stdio.h> void main(void){ int shifty; shifty=0570; shifty=shifty>>4; shifty=shifty<<6; printf("The value of shifty is %o \n",shifty); }

- The value of shifty is 15c0
- The value of shifty is 4300
- The value of shifty is 5700
- The value of shifty is 2700

12 votes

Best answer

Ans is D) 2700

If an integer constant starts with 0, it is considered an octal constant. So 0570 = 101 111 000 in binary as each octal digit can be directly converted to 3 bits. Now, >>4 (right shift 4) gives 010 111 and <<6 (left shift 6) gives 010 111 000 000 = 2700 in octal. If we use "%d" in printf, this would be 1024+64*7 = 1472.

If an integer constant starts with 0, it is considered an octal constant. So 0570 = 101 111 000 in binary as each octal digit can be directly converted to 3 bits. Now, >>4 (right shift 4) gives 010 111 and <<6 (left shift 6) gives 010 111 000 000 = 2700 in octal. If we use "%d" in printf, this would be 1024+64*7 = 1472.

0

see 0570 mean its in octal .

so 000 101 111 000

now >>4 means 010 111

now <<6 means 010 111 000 000 = 2700 . peace

so 000 101 111 000

now >>4 means 010 111

now <<6 means 010 111 000 000 = 2700 . peace

2

what is the meaning of PEACE,please tell me just for the sake of knowledge .every time after explaination you use this word.

6

I use this but not every time .

peace means in hindi "Shanti" , so i`m looking for her :D :P

anyway it`s like this ✌ .

1 vote

It is in octal(0570)_{8} Since it starts with octal, so take 3 bits in binary format.

(0570)_{8}=000 101 111 000

shifty>>4(left shift the bits 4 times)=100 000 010 111

shifty<<6(right shift the bits 6 times)=010 111 000 000=(2700)_{8}

So answer is D

0

ITS not shift by 4 times its by shift by 4 bits and is right shift and 2nd one is left shift by 6 bits.

0

It is in octal(0570)_{8} Since it starts with octal, so take 3 bits in binary format.

(0570)_{8}=000 101 111 000

shifty>>4(left shift the bits 4 times)=100 000 010 111

shifty<<6(right shift the bits 6 times)=010 111 000 000

**how did you that ?**

i think it should be

shifty>>4(left shift the bits 4 times)=100 000 010 111

shifty<<6(right shift the bits 6 times)=010 111 000 **1**00

0 votes

0 votes

int shifty; shifty=0570;

If an integer constant begins with **0x** or **0X**, it is hexadecimal. If it begins with the digit **0**, it is octal. Otherwise, it is assumed to be decimal.

(https://docs.microsoft.com/en-us/cpp/c-language/c-integer-constants)

shifty=shifty>>4;

>> means Right Shift means "divide by 2"

shifty=shifty<<6;

<< means Left Shift means "multiply by 2"