C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 1)
1.
Assunming, integer is 2 byte, What will be the output of the program?
#include<stdio.h>
int main()
{
printf("%x\n", -1>>1);
return 0;
}
Answer: Option
Explanation:
Negative numbers are treated with 2's complement method.
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement.
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement.
Binary of 1(2byte) : 0000 0000 0000 0001 Representing -1: 1s complement of 1(2byte) : 1111 1111 1111 1110 Adding 1 to 1's comp. result : 1111 1111 1111 1111 Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1) Hexadecimal : f f f f (Filled with 1s in the left side in the above step)
Note:
1. Fill with 1s in the left side for right shift for negative numbers.
2. Fill with 0s in the right side for left shift for negative numbers.
3. Fill with 0s in the left side for right shift for positive numbers.
4. Fill with 0s in the right side for left shift for positive numbers.
Discussion:
26 comments Page 1 of 3.
Nagamastan said:
1 decade ago
Very nice and thanks for the explanation.
Muruli said:
1 decade ago
Can any body can explain me the use of %x?
Naumi said:
1 decade ago
It is a format specifier for the hexadecimal digits. Like %d for integer type.
Sreedevi said:
1 decade ago
Just a small change in the pgm gave me different answer. Can any1 explain the concept of that.
#include<stdio.h>
int main()
{
unsigned int i=-1;
printf("%x\n",i>>1);
}
Result was 7fff
#include<stdio.h>
int main()
{
unsigned int i=-1;
printf("%x\n",i>>1);
}
Result was 7fff
Sam said:
1 decade ago
unsigned int never takes -ve value..it ranges from 0 t0 65535..
Basha said:
1 decade ago
Please tell me ! In bitwise operators what use of >>,<<,~.
Fool coder said:
1 decade ago
>> shifts the bits in right direction, which means number is divided by 2,4,8,16,.... e.g. 24>>4 gives output 1.
In case of left shift operator, it will multiply the no. by 1,2,4,8,.. e.g. 17<<2 will give 68.
Now at last , ~ this operator finds 1's(one's) complement of any number.
In case of left shift operator, it will multiply the no. by 1,2,4,8,.. e.g. 17<<2 will give 68.
Now at last , ~ this operator finds 1's(one's) complement of any number.
Nandhu said:
1 decade ago
unsigned int i = -1;
Here i is unsigned even if we declare negative number it will consider as positive
Here i is unsigned even if we declare negative number it will consider as positive
Priya Ramesh said:
1 decade ago
Coder will you explain the program with <<, >> using this symbols.
Priya Ramesh said:
1 decade ago
How we convert numbers into letters and letters into numbers by using hexadecimal system.
Example:1111's are indicated with f how it is possible.
Example:1111's are indicated with f how it is possible.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers