# 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 0001Representing -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.
Mayur said:
1 year ago

Binary of 1(2byte) : 0000 0000 0000 0001.

I didn't understand this step. Can someone explain it?

I didn't understand this step. Can someone explain it?

Gourav said:
2 years ago

If it takes 17<<2 what is the output? Please explain.

(1)

Naveen Narayanan said:
5 years ago

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

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

(1)

Jaya said:
6 years ago

I am not understanding this.

Akshay g said:
6 years ago

The Explanation/answer is incorrect.

Because of {int } takes 4 bytes in windows .So, the answer is ffffffff.

Because of {int } takes 4 bytes in windows .So, the answer is ffffffff.

Mahesh said:
7 years ago

Thank you @Ahmed.

Mrutyunjay Patil said:
7 years ago

Please give me the output of this question with an explanation.

#include<stdio.h>

int main()

{

printf("%x\n", -4<<2);

return 0;

}

A. fff0

B. 0

C. ffff

D. Error

#include<stdio.h>

int main()

{

printf("%x\n", -4<<2);

return 0;

}

A. fff0

B. 0

C. ffff

D. Error

Ahmed said:
7 years ago

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

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.

Mohan said:
7 years ago

What is %x?

New coder said:
8 years ago

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.

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.

Post your comments here:

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers