Integer representation Please write the following 4-byte integer values in binar
ID: 3855397 • Letter: I
Question
Integer representation Please write the following 4-byte integer values in binary or hexadecimal a. 2 b. 17 c. -2 d. The second largest unsigned integer. (Not the largest, one less than that.) e. The most negative signed integer. Please write a C function unsigned int rollLeft(unsigned int u, unsigned int amount) that returns the 4-byte unsigned integer u that has been rolled to the left by amount. A roll is like a shift, except that instead of forgetting the highest bit, it reappears at the lowest bit position. Examples: rollLeft(Oxl0000001, 0) = 0x10000001 rollLeft(0xl0000001, 1) = 0x20000002rollLeft(0xl0000001, 2) = 0x40000004 rollLeft(0xl0000001, 3) = 0x80000008 rollLeft(0xl0000001, 4) = 0x00000011 rollLeft(0xl0000001, 5) = 0x00000022Explanation / Answer
4bytes = 32 bits
1 byte = 8 bits..
The binary representation for given 4byte integer values:-
note: to convert the integer into binary, every time divide the integer with 2, (means decrease the number by dividing with 2)..then we will get the sequence of reminders... take them in least significant order..(first remainder is LSB in 4 byte notation)
a) 2
binary form : 10 = (2^1)*1 +(2^0)*0
00000000 00000000 00000000 00000010
b)17
binary form : 10001 = (2^4)*1+(2^3)*0+(2^2)*0+(2^1)*0+(2^0)*1
00000000 00000000 00000000 00010001
c)-2
it is signed number: so, most significant,remaing from binary format all are 1's
2 binary form : 10
now remaing all other bits are 1's
11111111 11111111 11111111 11111110
d)
the largest number in unsigned format would be:
11111111 11111111 11111111 11111111
2^32-1 = 4,294,967,295
second largest number would be = 4,294,967,295-1 = 4,294,967,294
e)
for signed 32 bit number the range would be form -2^31 to 2^31 -1
The most negative signed integer :-
-2^31 = 2,147,483,648
C function for unsigned int rollleft :-
#include<stdio.h>
#include<math.h>
unsigned int rollLeft(unsigned int u, unsigned int amount)
{
unsigned int rolled = 32-amount;//finding the number of literals that can be added at the end
rolled = pow(2,rolled);
rolled = u/rolled;
//shifting
while(amount>0)
{
u = u*2;//left shifted..
//re adding the highest bit without losing it
//adding at the end...
u = u+ (rolled%2);
rolled = rolled/2;
amount--;
}
return u;
}
int main()
{
unsigned int n,amount;
while(amount>0)
{
printf("Enter unsigned number:");
scanf("%u",&n);
printf("Enter amount to roll left:(0 to quit)");
scanf("%u",&amount);
if(amount==0)
break;
printf("After rolling Left :%u ",rollLeft(n,amount));
}
return 0;
}
/*
output:
Enter unsigned number:10
Enter amount to roll left:(0 to quit)2
After rolling Left :40
Enter unsigned number:400
Enter amount to roll left:(0 to quit)5
After rolling Left :12800
Enter unsigned number:50
Enter amount to roll left:(0 to quit)4
After rolling Left :800
Enter unsigned number:4294967002
Enter amount to roll left:(0 to quit)2
After rolling Left :4294966123
Enter unsigned number:0
Enter amount to roll left:(0 to quit)0
Process exited with return value 3221225620
Press any key to continue . . .
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.