In: Computer Science
For each of the following, describe how to implement the operation of the variable "int x" using only bitwise, addition, and subtraction operators (<<, >>, +, -, &, |, ^, ~)
A) x * 2
B) x * 14
C) x * -44
D) x * 1023
Problem 2. Floating point encoding. Write out the 32 bit IEEE floating point binary representation for each of the following numbers:
A) 1.0
B) -2.0
C) -1000000.00
D) nan
E) -inf
F) 3.402823e38
A) x<<1 B) x<<4 - x<<1 C) x<<4 - x<<6 + x<<2 D) x<<10 - x
A) 1.0 in simple binary => 1.0 so, 1.0 in normal binary is 1.0 => 1. * 2^0 single precision: -------------------- sign bit is 0(+ve) exp bits are (127+0=127) => 01111111 frac bits are 00000000000000000000000 so, 1.0 in single-precision format is 0 01111111 00000000000000000000000 Answer: 0 01111111 00000000000000000000000 B) -2.0 in simple binary => 10.0 so, -2.0 in normal binary is 10.0 => 1. * 2^1 single precision: -------------------- sign bit is 1(-ve) exp bits are (127+1=128) => 10000000 frac bits are 00000000000000000000000 so, -2.0 in single-precision format is 1 10000000 00000000000000000000000 Answer: 1 10000000 00000000000000000000000 C) -1000000.0 in simple binary => 11110100001001000000.0 so, -1000000.0 in normal binary is 11110100001001000000.0 => 1.1110100001001 * 2^19 single precision: -------------------- sign bit is 1(-ve) exp bits are (127+19=146) => 10010010 frac bits are 11101000010010000000000 so, -1000000.0 in single-precision format is 1 10010010 11101000010010000000000 Answer: 1 10010010 11101000010010000000000 D) nan Answer: 01111111111111111111111111111111 E) -inf Answer: 11111111100000000000000000000000 F) 3.402823e38 3.402823e+38 in simple binary => 11111111111111111111110010110011010101101100100100100000000000000000000000000000000000000000000000000000000000000000000000000000 so, 3.402823e+38 in normal binary is 11111111111111111111110010110011010101101100100100100000000000000000000000000000000000000000000000000000000000000000000000000000 => 1.111111111111111111111 * 2^127 single precision: -------------------- sign bit is 0(+ve) exp bits are (127+127=254) => 11111110 frac bits are 11111111111111111111100 so, 3.402823e+38 in single-precision format is 0 11111110 11111111111111111111100 Answer: 0 11111110 11111111111111111111100