News: If you feel any problem while posting a topic or Reply - Contact our server team at: +92-341-2825535

Author Topic: If the following program fragment (assume negative numbers are stored in 2's  (Read 264 times)

Offline mechanic

  • Engineer
  • Hero Engineer
  • *****
  • Posts: 2271
    • View Profile
    • Engineering Forum
Question: If the following program fragment (assume negative numbers are stored in 2's complement form)

unsigned i = 1;
int j = -4 ;
printf["%u", i + j);
prints x, then printf{"%d", 8*sizeof(int)};

outputs an integer that is same as (log in the answers are to the base two)

options are:

A. an unpredictable value
B. 8*log{x+3}
C. log (x+3)
D. none of the above

Correct answer is:

C. log (x+3)


Let sizeof(int) = 1. so -4 will be stored as 11111100. since we are adding unsigned and signed integers, the signed gets converted to unsigned, so, i + j will become 11111101. we are trying to print this as an unsigned integer. so, what is printed will be 28 - 1 - 2. So, log(x + 3) = 8 (i.e. 8*sizeof{int}).

Engineering Forum


With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
type: "mehran" without quotes: