-
Notifications
You must be signed in to change notification settings - Fork 0
/
explaination.txt
88 lines (78 loc) · 8.31 KB
/
explaination.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Authors:
Sam Bunger
Marcus Young
Bite Ye
PART A: Explanations to questions 1-15
~~~~~~~~Question 1~~~~~~~~
This method will return 1 if the inputted integer is 0, and return 0 otherwise.
The method first, takes the compliment of the bits, adds 1, and sets it equal to y.
(it turns the value of x negative in 2's compliment)
0 in binary has the unique property of remaining exactly the same when negated in 2's compliment. So 0 will remain as all 0s
Any other number will be changed.
The next function takes the bitwise OR of x and y and then takes the compliment.
In the case of 0, the result will be all 1s. In the case of any other number, the leftmost bit will be 0. This is because, if the leftmost bit of x is 0, then the leftmost bit of y is one, and the bitwise OR output would be 1, once the compliment is taken, the leftmost bit will be 0.
This remains true when x is 1 as well.
So we know after the first two lines that:
-The leftmost bit when x=0 is 1
-The leftmost bit when x=any other number is 0
The next line shifts the leftmost-bit all the way until it is the rightmost bit, and fills the rest of the space with 0s and sets it to z
The bitwise AND of Z and 1 is taken.
All the bits left of the rightmost bit will become 0, because they are all zero in the case of binary 1 and z
In the case when x is:
0 - the function will return 1, because the rightmost bit for both z and binary 1 are 1.
any other number - will return 0 since the rightmost bit of z is 0.
~~~~~~~~Question 2~~~~~~~~
This method will return the absolute value of x.
It first makes the leftmost bit of x, containing the sign, and makes it the rightmost bit, and fills the rest with 0.
If x was positive, the result of the second line will not change x.
If x was negative, it will return the same value, except change the rightmost to a 0 or 1 depending on the number.
Next, if x was positive, then z will become all 0s since taking the complement of 0 and adding one will give you 0
if x was negative, you will get all ones because when the complement of the mask is taken, everything will become 1 except for the rightmost bit,
when 1 is added, all the bits in z will then become 1.
Adding y and z will give you y if x was positive,
if it was negative it will give you the absolute value of c because adding a number of all 1s and to y will flip the bits and shift the 1s to the left 1,
giving you a positive value.
~~~~~~~~Question 3~~~~~~~~
This method returns 1 if x is greater than 0, and returns 0 otherwise.
On the first line, y becomes 1 if x is 0, and becomes 0 otherwise.
On the second line, z becomes 1 if the leftmost bit, indicating the sign of x, is 1, and 0 otherwise.
The next line takes the bitwise or of x and y. The result will be 1 if x is either 0 or negative, and will be 1 otherwise.
The last line, returns the 1 if z was 0, or 0 if z was 1. So if x was greater than 0, 1 is returns, and if its less than 1, 0 is returned.
~~~~~~~~Question 4~~~~~~~~
When n is 0, the function will return a 0.
When n > 0 and n<=32 the output is -(2^(32-n))
The first two lines determine if n is 0 or not, if it is the bitwise AND at the return statement will ensure that 0 is returned when n = 0.
If it isn't, x will be all ones, so the outcome of the return statement will completely depend on the value of y.
y is determined by right shifting -2,147,483,648 (n-1) times.
~~~~~~~~Question 5~~~~~~~~
This method determines even and odd numbers, by returning 0 for even numbers and -1 for odd numbers.
First the leftmost bit is right-shifted 31 spaces. The rightmost bit of a binary number will tell you if it is even or odd.
It will be 1 if it is odd.
Once it has been shifted to the right, it is then shifted back to the left.
When 1 is the leftmost bit and is shifted right, the spaces are filled will 1s, until after shifting 31 times, the whole 32 bit binary number is 1s
The same goes for 0, except 0s are added on to the end.
Once the number is returned, we will know if it is even or odd.
~~~~~~~~Question 6~~~~~~~~
Basically, this function shift 0x55 to the left by 8 bits and fill the empty space with another 0x55. The reuslt of this procedure is 0x5555 which is 101010101010101 in binary. Then, the fucntion do the previous precedure again but shift by 16 bits this time (the length of var we are shifting). The finally result would be 0x55555555.
In general, we times the input by 16843009.
~~~~~~~~Question 7~~~~~~~~
This function return the result of a bitwise and operation between the input and its 2s complement (by generate its 1s complemnt first by using bitwise NOT and then add 1).
~~~~~~~~Question 8~~~~~~~~
This function shift the input to right by 31 bits, basically this gives us its most significant bit, since we're duealing with 32 bits int. Then do a bitwise OR operation between its MSB and what we got after doing a logical NOT twice to the input.
This function will return 1 if the input is larger 0, return 0 if the input is 0, and return -1 if the input is smaller than 0.
~~~~~~~~Question 9~~~~~~~~
We shift 0xFF to the left by input n times 8, call it mask, and shift input c to the left by input n times 8, call it 'cshift'. Then, we do an AND operation between the invert of 'mask' and input x, call it 'z'. Finally, we return the result of a bitwise OR operation between 'z' and 'cshift'.
Basically, we take the right by input n times 8 bits of input x and append it to the input c.
~~~~~~~~Question 10~~~~~~~~
We do a logical NOT twice on input x and call it 'y'. And do a logical NOT once on the result of 2 times input x, call it 'z'. Finally, return the result of an bitwise AND operation between 'y' and 'z'.
The function will always return 0.
~~~~~~~~Question 11~~~~~~~~
Returns 1 when x is negative and y is positive, or the absolute value of x is less than y. This function will return 0 in all other cases. This method takes two numbers and bitwise shifts both of the numbers to the right by 31 bits. Since we are using a 32 bit system, this operation will check if the 31st bit of a and b is on. The function will only return 0 when (the 31st bit of x is off and the 31st bit of y is on) or (if the 31st bit isn't on for x or y, and the 31st bit of the sum of the 31st bit of y and the complement of x's 31st bit is on).
~~~~~~~~Question 12~~~~~~~~
This function checks if x >= m, and n >= x then the function returns 0, otherwise the function will return 1.This method sets a to m's negative decimal value by taking the two's complement of m by flipping all of its bits and adding one. Next, the method sets b to x's negative decimal value by taking x's twos complement just as a did. Next the method adds a or -m to x, essentially taking the difference of x - m. Then, b takes the difference of n-x. Since we are shifting a Or'd with b 31 bits, we are checking if a or b is negative, if either of them are negative, return 0, otherwise return 1.
~~~~~~~~Question 13~~~~~~~~
This function essentially returns the entire number of binary bits that are 1 or turned on in a number. First, it creates mask 2 which is the sum of 13107 + 858980352, which is 858993459. Then it uses mask1 to create mask2, taking the bitwise exclusive or of mask2, and mask2 leftshifted once. This sets mask1 to a bianary representation of alternating 1's and 0's = 1010101010101010101010101010101. Next, mask4 is the sum 3855 + 252641280 = 252645135, mask8 is the sum mask8: 255 + 16711680 = 16711935, and mask16 is the sum 255 + 65280 = 65535. X is then manipulated by all of the masks, saving the amount of 1's in x through each mask manipulation. The function returns x, the amount of 1's in it's bianary representation.
~~~~~~~~Question 14~~~~~~~~
Returns 1 if there are an odd number of true bits and 0 if there are an even number of true bits. This function begins by setting result to 0, and declares integer i. Next it sets up a for loop to iterate through x’s 32 bits, and each selected bit is xor’d with the previous result of the last bit, and anded with the hexadecimal number 0x1. Result becomes 1 if the number of true bits are odd, and 0 if the number of true bits are even.
~~~~~~~~Question 15~~~~~~~~
This function returns x modulus 2^n, which is the remainder of (x/(2^n)). This function begins by setting the integer temp to 2^n by left shifting 1 by n bits. Next, the function sets z to temp + (-1) or temp-1 . Since temp is 2^n, z is now ((2^n)-1). Finally, the function returns the bitwise and operation of z and x, returning the remainder of (x/(2^n)).