In the end, Option B makes the most sense. It shouldn't have to be something that the user has to understand. You lose the ability to specify a negative number using its two's complement representation, but remember that two's complement is a storage implementation detail, not a proper indication of the underlying value itself. This is (a) more consistent with decimal, (b) more compatible with the way binary values are most likely going to be used. ![]() With this option, binary numbers are represented unsigned, and negative numbers have a "-" prefix, just like they do in decimal. In the end, this is probably more confusing than helpful particularly because most binary arithmetic is going to be unsigned anyway, and people are more likely to run into mistakes by accidentally marking a number as negative because they didn't include an explicit sign bit. That also means that 0b111 is also -1, while 0b0111 is 7. You could declare that all numbers are signed, and the left-most bit is the sign bit. So if I say 0b1111, how do I know if the user wants -1 or 15? There are two options: Just like 0x means "this number is in hex". The 0b prefix just says "this number is in binary". Python needs a way to represent these numbers in binary so that there's no ambiguity of their meaning. Does 111 represent 7 or -1? Again, it could be either. In 2's complement, the sign bit extends all the way to the left, but in Python there is no pre-defined number of bits there are as many as you need.īut then you run into ambiguity: does binary 1 represent 1, or -1? Well, it could be either. So with that in mind, what binary number represents -1? Python is well-capable of interpreting literally millions (and even billions) of bits of precision, as the previous example shows. If I had more RAM, I could store larger numbers. ![]() ![]() On my computer, this actually works, but it consumes 9GB of RAM just to store the value of x. If it's a 64-bit value, then there are 64 bits to display.īut in Python, integer precision is limited only to the constraints of your hardware. If it's a 32-bit number, then a negative number has a 1 in the MSB of a set of 32. In basic theory, the actual width of the number is a function of the size of the storage. Or perhaps more generally: def bindigits(n, bits): That way you specify how far to sign extend.
0 Comments
Leave a Reply. |