Question

In: Computer Science

For each of the following, describe how to implement the operation of the variable "int x"...

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

Solutions

Expert Solution

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

Related Solutions

1. What is value of X after following operation int X=0x 45; X = X<<3 ;...
1. What is value of X after following operation int X=0x 45; X = X<<3 ; X= What is value of X after following operation int x=0x 40; X = X>>3 ; X= What is value of X after following operation int x=0X FF; X = X & 0x0F; X= What is value of X after following operation int x=0x FF; X = ~X ; X= What is value of X after following operation int X=0x FF; X = X...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
If x is a binomial random variable, compute P(x) for each of the following cases:
If x is a binomial random variable, compute P(x) for each of the following cases: (a) P(x≤5),n=7,p=0.3 P(x)= (b) P(x>6),n=9,p=0.2 P(x)= (c) P(x<6),n=8,p=0.1 P(x)= (d) P(x≥5),n=9,p=0.3   P(x)=  
If x is a binomial random variable, compute p(x) for each of the following cases: (a)...
If x is a binomial random variable, compute p(x) for each of the following cases: (a) n=3,x=2,p=0.9 p(x)= (b) n=6,x=5,p=0.5 p(x)= (c) n=3,x=3,p=0.2 p(x)= (d) n=3,x=0,p=0.7 p(x)=
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
If x is a binomial random variable, compute P(x) for each of the following cases, rounded...
If x is a binomial random variable, compute P(x) for each of the following cases, rounded to two decimal places: c)  P(x<1),n=5,p=0.1 d)  P(x≥3),n=4,p=0.5
For problems 3 and 4, consider the following functions that implement the dequeue operation for a...
For problems 3 and 4, consider the following functions that implement the dequeue operation for a priority queue that is implemented with a heap. int[] pQueue; int length; int dequeue() { int node = 1; int value = pQueue[--length]; int maxValue = pQueue[node]; int location = sift(node * 2, value); pQueue[location] = value; return maxValue; } int sift(int node, int value) { if (node <= length) { if (node < length && pQueue[node] < pQueue[node + 1]) node++; if (value...
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
Answer the following What is the difference between these two methods? Void display(int x) Int display(int...
Answer the following What is the difference between these two methods? Void display(int x) Int display(int s) Write a one line of code to generate random numbers from 35 to 55.( not a full program) Draw the UML diagram for a switch statement with 4 cases . Write a while loop to output the value of the variable x starting from 5, decreasing it by 0.5 each time, as long as x remains positive. Write a for loop to display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT