Integers and Binary

Computers are very simple electrical machines at a fundamental level and can only have two different states.

“On” and “Off”/”True” and “False”/”1” and “0”.

Now this gets quite interesting relatively quickly. The purpose of a computer is to compute, to perform algorithms and implement mathematics to achieve a specific function.

We typically use a base-10 number system where we have ten different symbols and every tenth number expands into a new column. For computers, they only have two “symbols” to count with and every second increment expands into the next column.
[1] (Base-X System by Faceprep Knowledgebase)

Base-10: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, [...]
Base-2: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, [...]

How the increment function works within the binary.py file, is that it just builds upon this simple understanding. It starts with a basic “for loop”, which will go over each column (or each “bit”) within the number it’s going to increment, in reverse. If the current bit position, i, is a 0, this should be changed to a 1 and the script should end. If however, the bit is a 1, it should be changed to a 0 and then i will be decremented and the loop will repeat until it finds the first 0 to turn into a 1.

If however, there are no remaining bit positions which contain a 0, the integer cannot be incremented safely and an exception should be raised, however this does not happen in the library and instead an integer overflow occurs, where the value “wraps-around” from 0b11111111 (0d255) to 0b00000000 (0d0). This is a common memory-management bug and could cause breakages and unexpected behaviour if not caught.
[2] (Binary.py file provided by BCU)

Acknowledgements:
[1] (Base-X System by Faceprep Knowledgebase) https://www.faceprep.in/aptipedia/knowledgebase/base-system/

[2] (Binary.py file provided by BCU)

Leave a comment