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.
Wikiok said:
1 decade ago
2 : 00000000 00000010
-2 : 11111111 11111110 ( = ~2 +1 )
-2<<2: 11111111 11111000 so it is FFFF8
-2 : 11111111 11111110 ( = ~2 +1 )
-2<<2: 11111111 11111000 so it is FFFF8
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
-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
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.
Like FFFFFFF8.
Why this happen. Any one help me if you know that.
Svetta said:
1 decade ago
How is shifting done?
Swati said:
1 decade ago
How is shifting done ? please tell anyone.
Falcon said:
1 decade ago
@Sameer
In dev-c integer is 4 byte
In dev-c integer is 4 byte
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?
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?
Vamsi reddy said:
1 decade ago
Why is it, in hexadecimal form ? please explain.
Mausami said:
1 decade ago
But if there is negative then shifting means appending 1's rather than 0's.
Am I right ?
Am I right ?
Nani said:
1 decade ago
Why should we follow hexadecimal formats?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers