C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 2)
2.
Assuming, integer is 2 byte, What will be the output of the program?
#include<stdio.h>

int main()
{
    printf("%x\n", -2<<2);
    return 0;
}
ffff
0  
fff8
Error
Answer: Option
Explanation:
The integer value 2 is represented as 00000000 00000010 in binary system.

Negative numbers are represented in 2's complement method.

1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0).

2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value).

Therefore, in binary we represent -2 as: 11111111 11111110.

After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system.
Discussion:
33 comments Page 1 of 4.

Nitesh said:   5 years ago
For 2 byte Means, 2 * 8 = 16 bits.

NOTE: When we have negative value at that time we have to find 1's and 2's complement of the number.

0000 0000 0000 0010 // 2
1111 1111 1111 1101 // 1's complement
+ 1 // 2's complement.
---------------------
1111 1111 1111 1110 // -2

-2 << 2

1111 1111 1111 1110 << 0000 0000 0000 0010

1111 1111 1111 1100 ---Step 1
1111 1111 1111 1000 ---Step 2 // Final Answer.

HexaDecimal form Of Final Answer: f f f 8.

Nitesh Alashe said:   10 years ago
Firstly 2: 0000 0000 0000 0010.

Make complement of 2 we get -2: 1111 1111 1111 1101 (All 0's to 1 and 1's to 0).

Then add 1 in that, then it becomes +1: 1111 1111 1111 1110.

As there is << operator which is pointing towards right to left.

So add 2 zero's to right it becomes 1111 1111 1111 1000 which is FFF8.

So option C.

Narayan said:   9 years ago
@ all who haven't got this-.

In left shifting we will shift by the numbers given on the right side of operator.

Example -: -2<<2.

So here we will left shift the binary value of 2 by 2 digits and add 2 zeros to the right hand side, since 2 is present on the right hand side of operator, so we add 2 zeros to the right.

Noshikifuka G said:   8 years ago
A=10, B=11,C=12.D=13,E=14,F=15 in hexa decimal.
11111111 11111000---> from left first 1111 is equal to 15 i.e f.
Next 1111 is equal to 15 i.e f then next 1111 is f so totally three f is present.
remaining part is 1000 that is equal to 8 so the ans is fff8.

Shilpa said:   1 decade ago
2: 0000 0000 0000 0010
-2: 1111 1111 1111 1101 (complimentary)
+ 1
_____________________________
1111 1111 1111 1110

Shifting 2bits to left

We get 1111 1111 1111 1000

In hex its represented as FFF8

S_14 said:   1 decade ago
@Svetta, @Swati :

Explaining left shift by 2 bits:

Append two zeros on the right hand side, then write whole no before them (excluding the 2 bits at left hand side).

Why we are shifting the two bits by left?

Arun said:   7 years ago
00000000 00000010-- the binary value of 2.

In that why how many zeros are put before 1?

We normally do the value of 2 is 0010 we use only 8 why would we use & how many zeros?

Nitesh said:   5 years ago
@Gokulram.

It's a hexadecimal form of Binary.
like,
0000 = 0,
0001 = 1,
0011 = 3.................
1001 = 9,
1010 = A
1011 = B .till 15 means 1111 = F. Thats it.

Sameer said:   1 decade ago
I run this program in dev-c and GCC compiler but answer is something different.

Like FFFFFFF8.

Why this happen. Any one help me if you know that.

Thrinesh said:   2 months ago
@All.

- %X → Prints uppercase hexadecimal (A-F instead of a-f).
- %#x → Adds "0x" prefix (0xff).
- %#X → Adds "0X" prefix (0XFF).


Post your comments here:

Your comments will be displayed after verification.