00:00 In the previous lesson, I showed you all the different ways to be negative—numerically, that is. In this lesson, I’ll run down how Python stores integers so that in the next lesson, you can be prepared for manipulating their bits.
00:43 On a 64-bit platform, the eight-byte representation of an integer can store from -2 to the 63 all the way up to one less than positive 2 to the 63. That’s an upper limit of this very big number. To store negative values, Python’s fixed-precision integer uses two’s complement storage.
01:13 Then, what, if a nineteen-digit numbers too small for you—well, Python’s got your back. The arbitrary-precision integer has no limit and is based on a series of numbers stored in base 2 to the 30. Yup, you read that right. Not base 30. Base 2 to the 30.
02:27 New instances of the same number result in the same ID. With values between 5 and 256, new instances of the same number result in the same ID. Do that with a number bigger than 256, and you’ll get multiple instances, no singleton. The compiler will even do further optimizations if the same number shows up in the same line.
02:56 Arbitrary precision numbers are sometimes to as big nums. Doing math on big nums is expensive. Unlike a fixed-precision integer or a float, the CPU isn’t built to do this specifically. Of course, if you’re playing with numbers this big, the precision you get out of a big num might be worth the performance trade-off. What about negative numbers? Well, arbitrary-precision numbers don’t use two’s complement storage for negatives.
03:27 Doing bitwise operations on arbitrary-precision numbers is a multi-step process with even more steps if the value happens to be negative. First, it has to be converted to a series of bits in two’s complement form.
03:41 Then the bitwise operator is applied. Then the result is converted back into an arbitrary-precision integer. This back-and-forth ensures that bitwise operations do the same thing regardless of the size of the integer. Consistency is good, but of course, that is consistency with two’s complement, which I believe I have mentioned is a bit weird with bitwise operations.
04:06 Just a quick recap before getting into the actual bitwise ops. Python doesn’t have an unsigned integer. It uses two’s complement representation for negative numbers, which can have surprising effects when interacting with bitwise operations.
04:21 But for most use cases, this doesn’t really matter. Typically, if you’re doing bitwise math, you’re doing it on positive numbers that are small enough to be using the fixed-precision representation.
Become a Member to join the conversation.