# C Programming - Bitwise Operators - Discussion

### Discussion :: 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;
}
``````

 [A]. ffff [B]. 0fff [C]. 0000 [D]. fff0

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.
```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.

 Nagamastan said: (Nov 12, 2010) Very nice and thanks for the explanation.

 Muruli said: (Feb 11, 2011) Can any body can explain me the use of %x?

 Naumi said: (Feb 23, 2011) It is a format specifier for the hexadecimal digits. Like %d for integer type.

 Sreedevi said: (Jun 8, 2012) Just a small change in the pgm gave me different answer. Can any1 explain the concept of that. #include int main() { unsigned int i=-1; printf("%x\n",i>>1); } Result was 7fff

 Sam said: (Aug 21, 2012) unsigned int never takes -ve value..it ranges from 0 t0 65535..

 Basha said: (Sep 5, 2012) Please tell me ! In bitwise operators what use of >>,<<,~.

 Fool Coder said: (Jul 23, 2013) >> 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.

 Nandhu said: (Sep 3, 2013) unsigned int i = -1; Here i is unsigned even if we declare negative number it will consider as positive

 Priya Ramesh said: (Feb 20, 2014) Coder will you explain the program with <<, >> using this symbols.

 Priya Ramesh said: (Feb 20, 2014) 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.

 Nandini said: (Mar 23, 2014) What does ffff mean?

 Shakti said: (Jul 19, 2014) What is o/p ? #include int main() { printf("%x\n", -1>>1); return 0; }

 Kiruba said: (Sep 3, 2014) How does 1111 1111 1111 1111 give the same when it is divided by 2(right shifting once)?

 Naresh said: (Sep 5, 2014) -1>>1 -1= for negative value use 2's complement. -1 -->0000 0000 0000 0001 1's-->1111 1111 1111 1110 2's--> 1 ------------------- 1111 1111 1111 1111 And the -1 is right shifting once. Answer : 0111 1111 1111 1111--->7FFF.

 Shankr said: (Sep 24, 2014) @Naresh. If negative number is shifted it must append with 1. So output is ffff and not 7fff.

 Bheemraj said: (Apr 24, 2015) @fool coder. >> shifts the bits in right direction, which means number is divided by 2, 4, 8, 16. Example: 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. Can you explain more about negative numbers?

 New Coder said: (Aug 27, 2015) As @Bheemraj said 24>>4 =1. I don't know how? As far as I know, it should be 24=0001 1000 >> 1000 0001=129 by right shifting 4 times. And what is the concept of multiplying by 1, 2, 4, 8 and dividing by 2, 4, 8, 6. Also I don't know how does this happens. 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. I only know to fill vacant places by 0 not by 1. If anyone knows answers please explain. Thank you.

 Mohan said: (Jul 27, 2016) What is %x?

 Ahmed said: (Nov 5, 2016) @New coder. x = 24; /* 0001 1000 */ y = x>>4 ; y = 1; /* 0000 0001*/ the two 1s shifted 4 bits so one of them became out the number and the another one which you see now. I don't understand that too! 1. Fill with 1's on the left side for a right shift for negative numbers. 2. Fill with 0's on the right side for a left shift for negative numbers. 3. Fill with 0's on the left side for a right shift for positive numbers. 4. Fill with 0's on the right side for a left shift for positive numbers.

 Mrutyunjay Patil said: (Jan 8, 2017) Please give me the output of this question with an explanation. #include int main() { printf("%x\n", -4<<2); return 0; } A. fff0 B. 0 C. ffff D. Error

 Mahesh said: (Jan 8, 2017) Thank you @Ahmed.

 Akshay G said: (Jul 10, 2017) The Explanation/answer is incorrect. Because of {int } takes 4 bytes in windows .So, the answer is ffffffff.

 Jaya said: (Aug 3, 2017) I am not understanding this.

 Naveen Narayanan said: (Oct 4, 2018) @Mrutyunjay patil. -4= 0000 0000 0000 0100. 1's ->1111 1111 1111 1011. 2's->adding 1. 1111 1111 1111 1100. By left shifting 0 as -4<<2,we get. 1111 1111 1111 0000. Since, fff0.

 Gourav said: (Jun 27, 2021) If it takes 17<<2 what is the output? Please explain.

 Mayur said: (Jun 21, 2022) Binary of 1(2byte) : 0000 0000 0000 0001. I didn't understand this step. Can someone explain it?