Let’s cover the topic of Bit Manipulation as set out in Cracking the Coding Interview.
Bit Manipulation by Hand
Logical Operators
Notation Key: recall that ^ means XOR ~ means NOT
Bit Manipulation by Hand
Bit Facts and Tricks
Two’s Complement and Negative Numbers
Two’s complement is a mathematical operation on binary numbers and is an example of a Radix Complement [meaning the number which, added to the given n-digit number in radix r, results in { r^{n}}. In binary ({r=2}), this is the two’s complement.]. It is used in computing as a method of signed number representation. The two’s complement of an N-bit number is defined as its complement with respect to 2ᴺ (Wikipedia).
“Two’s complement is the way most computers represent positive or negative integers. To get the two’s complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result” (Bitesize)
Arithmetic vs Logical Right Shift
Common Bit Tasks: Getting and Setting
Bit Manipulation Interview Questions
References
A: https://www.programiz.com/swift-programming/bitwise-operators
B: https://www.youtube.com/watch?v=NLKQEOgBAnw
Basic of signed and unsigned numbers
https://www.youtube.com/watch?v=miwMEUfkqfY
Binary Addition
https://www.youtube.com/watch?v=WN8i5cwjkSE
Binary: Plusses & Minuses (Why We Use Two’s Complement)
https://www.youtube.com/watch?v=lKTsv6iVxV4
–
Bit Facts and Tricks
…
Two’s Complement and Negative Numbers
…
Arithmetic vs. Logical Right Shift
…
Common Bit Tasks: Getting and Setting
Interview Questions