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;
}
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.
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.
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).
- %X → Prints uppercase hexadecimal (A-F instead of a-f).
- %#x → Adds "0x" prefix (0xff).
- %#X → Adds "0X" prefix (0XFF).
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.
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 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.
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.
Gokulram said:
5 years ago
What is the meaning of f in that format ff8? Please explain.
Kuro M said:
6 years ago
I understand the solution, but isn't shifting negative signed numbers to the left an undefined behavior?
(1)
Bhai said:
6 years ago
We are taking the number as signed int.
so after getting ans as : fff8 should not we convert it into signed int also?
so after getting ans as : fff8 should not we convert it into signed int also?
John said:
7 years ago
After shifting why can't it be 1111 1111 1111 1011?
Manju said:
7 years ago
Why did they shift 2 bits by left?
Can anyone tell me?
Can anyone tell me?
Veera said:
7 years ago
Why added 2? Explain here.
Lilyjaiswal said:
7 years ago
How to it can print anything modifier for integer is %d?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers