Question: I do not understand this requirement in this lab. Extract byte n from word x? For this example: getByte(0x12345678,1) = 0x56 I changed 0x12345678 into

I do not understand this requirement in this lab. Extract byte n from word x?

For this example: getByte(0x12345678,1) = 0x56

I changed 0x12345678 into binary, we can get 0000 0000 0001 0010 0011 0100 0101 0110 0111 1000

and change 0x56 into binary. 0000 0000 0101 0110

If I Extract byte 1 from word 0x12345678, we can get the answer by from right to left: 0.

If I Extract byte 4 from word 0x12345678, we can get the answer by from right to left: 1000

If I Extract byte 8 from word 0x12345678, we can get the answer by from right to left: 0111 1000

Why this answer is 0x56, for binary is 0000 0000 0101 0110 for n =1?

/* * getByte - Extract byte n from word x * Bytes numbered from 0 (LSB) to 3 (MSB) * Examples: getByte(0x12345678,1) = 0x56 * Legal ops: ! ~ & ^ | + << >> * Max ops: 6 * Rating: 2 */ int getByte(int x, int n) { // 0<<3 = 0, 1<<3 = 8, 2<<3 = 16, 3<<3 = 24 // each byte starts from (n<<3)th bit. int t = x >> (n << 3); // get 8 bits = 1 byte int r = t & 255; return r; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!