LESSON 01 – BITWISE OPERATIONS
LESSON
WORK SOLUTIONS
QUESTION 1A
0011 1101
AND 0110 1100
0010 1100
QUESTION 1B
0010 1000
OR 1011 1001
1011 1001
QUESTION 1C
1001
XOR 0010
1011
QUESTION 1D
1011
NOR 1001
0100
QUESTION 1E
0101
NAND 0111
1010
QUESTION 1F
0100
XNOR 1110
0101
QUESTION 1G
NOT 1001 1011
0110 0100
QUESTION #2A
1001 1101
AND 0000 0000
0000 0000
Pattern: The
value of byte A doesn’t matter as the resulting byte will always be 0s.
QUESTION #2B
1001 1101
AND 1111 1111
1001 1101
Pattern:
The resulting byte is copy of byte A.
QUESTION #2C
1001 1101
OR 0000 0000
1001 1101
Pattern:
The resulting byte is a copy of byte A.
QUESTION #2D
1001 1101
OR 1111 1111
1111 1111
Pattern:
The value of byte A doesn’t matter as the resulting byte will always be 1s.
QUESTION #2E
1001 1101
XOR 0000 0000
1001 1101
Pattern:
The resulting byte is a copy of byte A.
QUESTION #2E
1001 1101
XOR 1111 1111
0110 0010
Pattern:
The resulting byte is the inversion (or 1’s complement) of byte A.
QUESTION #3A
STEP 1
We convert
17 to binary.
0001 0001
STEP 2
We now
shift by 2 to the right. The shifted
in bits are zeros.
0000 0100
STEP 3
We now
convert back to decimal.
4
Therefore,
17 >> 2 gives 4.
VERIFICATION
Since we
shifted by 2 to the right, that is the same as dividing by 22
which is 4. And, in integer division,
17 divided by 4 gives us 4. So our
answer is correct!
QUESTION #3B
STEP 1
We convert
91 to binary.
0101 1011
STEP 2
We now
shift by 3 to the left. The shifted in
bits are zeros.
1101 1000
Note: We
did shift a 1 out. So there is overflow.
STEP 3
We now
convert back to decimal.
128+64+16+8=216
VERIFICATION
When we
shift left by 3, that is the same as multiplying by 23 which is
8. Unfortunately, because of the
overflow, this is multiplication doesn’t match our
answer.
If you
really want to check your answer, you can remove the 1-bits that are shifted
out in our original number. In this
case, it is the 64bit that is shifted out.
So, we can pretend that our original number is 91 – 64 which gives 27. Now, if we multiply 27 by 8, we get our
answer of 216.
QUESTION #4A
STEP 1
Convert 73
to binary.
0100 1001
STEP 2
To get the value of -73, we first take the 1’s complement.
1011 0110
STEP 3
We now take
the 2’s complement.
1011 0111
STEP 4
We now
shift by 2 to the right. Since it is
an arithmetic shift, we maintain the value of the leftmost bit. So, two 1s are shifted in on the left side.
1110 1101
STEP 5
We now want
to go back to decimal. We must go from
2’s complement to 1’s complement by subtracting 1.
1110 1100
STEP 6
We now reverse
1’s complement.
0001 0011
STEP 7
We now
convert to decimal
16+2+1=19
Therefore,
the -73 >> 2 gives -19.
VERIFICATION
Unfortunately,
because 2’s complement represents negative numbers by storing them off by 1,
the division rule doesn’t fully work.
In this
case, -73 divided by 22 which is 4 gives -18 even if the correct
answer is -19.
QUESTION #4B
STEP 1
We convert
21 to binary.
0001 0101
STEP 2
We take the
1’s complement.
1110 1010
STEP 3
We take the
2’s complement.
1110 1011
STEP 4
We apply
the left shift by 1.
1101 0110
STEP 5
We go from
2’s complement to 1’s complement by removing 1.
1101 0101
STEP 6
We reverse
1’s complement.
0010 1010
STEP 7
We convert
to decimal.
32+8+2=42
Therefore,
-21 << 1 gives -42.
VERIFICATION
Shifting to
the left by 1 is equivalent as multiplying by 2. And, since -21 times 2 gives -42, we know
we have the correct answer.
QUESTION #5A
1101 0110
QUESTION #5B
STEP 1
We convert
147 to binary.
1001 0011
STEP 2
We apply
the circular shift to the left by three.
1001 1100
STEP 3
We convert
back to decimal
128+16+8+4
= 156
The answer
of circular shifting 147 << 3 is 156.
VERIFICATION
There is no
convenient verification option here.
If checking
your answer was very important, you could take your original number and
separate it into the bits that will move up in the byte and the bits that
will wrap around (or move down).
1001 0011
Green – the
bits that will wrap around
Orange –
the bits that will move up
The value
of the green bits is 128. The value of
the orange bits is 16+2+1=19.
Since the
orange bits are moving to the left by three, that is like multiplying them by
23 which is 8. So the
orange bits should become 19 x 8 = 152.
Since the
green bits are moving down by 5 spots, it is like dividing them by 25
which is 32. So the green bits should
become 128 divided by 32 = 4.
In total,
the orange and green bits should total to 152 + 4 = 156 which is our answer.
QUESTION #6
STEP 1
We want to
get the binary value of -122 in Java. We
must recall that numbers are stored in 32 bit sequences.
We start by
converting 122 to binary.
0000 0000
0000 0000 0000 0000 0111 1010
STEP 2
We take 1’s
complement.
1111 1111
1111 1111 1111 1111 1000 0101
STEP 3
We take 2’s
complement
1111 1111
1111 1111 1111 1111 1000 0110
STEP 4
We now do a
logical shift to the right by 2.
Remember that in logical shifts, the shifted-in bits are always zero.
0011 1111 1111
1111 1111 1111 1110 0001
STEP 5
Interestingly,
Java sees this number as a positive number now. So can immediately convert this to decimal.
This is a
little painful. I initially listed out
all the values that I have to add up.
20+25+26+27+28+29+210+211+212+213+214+215+216+217+218+219+220+221+222+223+224+225+226+227+228+229
Not wanting to punch this into a calculator, I reformatted
and entered the following directly into Google:
2^0+2^5+2^6+2^7+2^8+2^9+2^10+2^11+2^12+2^13+2^14+2^15+2^16+2^17+2^18+2^19+2^20+
2^21+2^22+2^23+2^24+2^25+2^26+2^27+2^28+2^29
And the answer that Google spit out was
1073741793 (which
is the correct answer!)
|