MCQ Single Best Answer Not Set

QAssunming, an integer is 2 byte, What will be the output of the program?

#include<stdio.h>

int main()
{
    printf("%x\n", -1>>1);
    return 0;
}

ID: #1163 Operators and Enums in C Language 849 views
Question Info
#1163Q ID
Not SetDifficulty
Operators and Enums in C LanguageTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A ffff
  • B 0fff
  • C 0000
  • D fff0
Correct Answer: Option A

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.

Share This Question

Challenge a friend or share with your study group.