In: Computer Science
Assume that we are executing the following code on a 32-bit machine using two’s complement arithmetic for signed integers. Which of the following will be printed when the following code is executed (circle those printed, and show work; e.g., how the values are stored):
#include <stdio.h>
int main()
{
char x = 0xF; // x = ________
char y = -1; // y = ________
unsigned char z = 0xFF; // z = 11111111
if (x<z)
printf("performed unsigned compare, x = %u, z = %u\n", x, z);
else
printf("performed signed compare, x = %d, z = %d\n", x, z);
printf("y>>4 = %d\n", y>>4); // y>>4 = __________
printf("z>>4 = %d\n", z>>4); // z>>4 = __________
}
Hint: Recall that the %u specifier is used to display the decimal value as an “unsigned” value
#include <stdio.h>
int main()
{
//0x is used to declare hexadecimal value--
char x = 0xF; // F in haxadecimal= 1111 in binary so,x = 1111
char y = -1; // y = -1
unsigned char z = 0xFF; // z = 11111111
if (x<z) //(1111<11111111) = True, so if statement will print
printf("performed unsigned compare, x = %u, z = %u\n", x, z); // x= decimal value of x and z = decimal value of z
// 1111 = = 8+4+2+1 = 15
//11111111 = = 128+64+32+16+8+4+2+1 = 255
else // it will not be executed
printf("performed signed compare, x = %d, z = %d\n", x, z);
printf("y>>4 = %d\n", y>>4); // y>>4 = -1
printf("z>>4 = %d\n", z>>4); // z>>4= 11111111>>4
// >> means bitwise right shift
z = 0000 0000 1111 1111
z>>1 = 0000 0000 0111 1111
z>>2 = 0000 0000 0011 1111
z>>3 = 0000 0000 0001 1111
z>>4 = 0000 0000 0000 1111
1111 = 15
Hence, the output will be:-
a) performed unsigned compare, x = 15, z = 255
b) y>>4 = -1
c) z>>4 = 15