operator asm equivalent description
&
AND Bitwise AND|
OR Bitwise inclusive OR^
XOR Bitwise exclusive OR~
NOT Unary complement (bit inversion)<<
SHL Shift bits left>>
SHR Shift bits right
-
Question:
count_bits.cc
- Use of
&
operand and00000001
to isolate the right most bit of a number.
- Use of
-
Question:
parity.cc
- We can use the
&
operator to determine the presence of1
s and0
s in the number. Based on that we can establish if the number of1
aka parity (odd or even)&
is good to isolate a bit
- We can use the
-
Question:
swap_bits.cc
- Swapping two distinct bits from a number X.
- In order to do that we can use the
>>
(shift) operator to isolate the two bits. Then we can create two new numbers (0000000
) and inject the isolated bits via shift (<<
). Finally, we can use the OR|
operator to merge the two new numbers and use the XOR^
operator to switch the bits from the original number.|
is useful to merge bits from two numbers^
is useful to switch a bit on and off in a number
-
Question:
reverse_bits.cc
- We can reverse a numbers bits by using the
&
operand to isolate the right most bit and re-injecting it to a new number via the<<
operand.
- We can reverse a numbers bits by using the
-
Question:
closest_int_same_weight.cc
- We calculate the weight of a binary stream by calculating the number of
1
in it. For example, for number (3
=00000011
) has a weight of2
. - The closest number to
00000011
(3
) with the same weight is00000101
(5
). - In order to do that we can create a new number (mask =
00000011
) that will carry the number to be switched. Example00000110
. The mask is initialized with11
because we want to guarantee it to change the neighbour bits. - The mask will be applied to the original number using the XOR operator
^
.
- We calculate the weight of a binary stream by calculating the number of
-
Question:
primitive_multiple.cc
- This one is not the most intuitive, but we basically can re-create a
add
function by using&
to find the carry and^
to merge two numbers. We can repeat theadd
function X times to simulate a multiplication.
- This one is not the most intuitive, but we basically can re-create a
-
Question:
primitive_divide.cc
- We can find the quotient of a division by doing the same strategy as the multiplication problem. We can do N subtractions to a number to find its quotient. Example:
n / m
=((n - m) - m) - m...
- We can find the quotient of a division by doing the same strategy as the multiplication problem. We can do N subtractions to a number to find its quotient. Example:
-
Question:
power_x_y.cc
- We can use the same strategy as the divide problem.
pow(m, n)
can be found by doingresult = ((m * m) * m) * m
aboutn
times. If the exponential is negative remember to1 / result
- We can use the same strategy as the divide problem.
-
Question:
reverse_digits.cc
- In order to reverse a number (int) we can use
%
to extract the right most digit. Additionally we can do* 10
to keep increasing your reversed number.
- In order to reverse a number (int) we can use
-
Question:
is_number_palindrome.cc
- This question is similar to
reverse_digits.cc
. We can use the same logic to reverse the number and check if it is equal to the original one.
- This question is similar to
-
Question:
uniform_random_number.cc
- We can use the
<<
operator and the randomization function to create unique numbers. On that note, we can limit the size of the number based on the number of possibilities (lower bound - great bound).
- We can use the
-
Question:
rectangle_intersection.cc
- No need to use bitwise operators to solve this problem. Basically we start checking if the rectangles intersect, after that we can calculate the size of the new intersection rectangle.