Kodeclik Blog
What are integers?
Integers are among the most common data types used in programming languages. Every programming language has a way to represent integers and perform operations on integers such as addition, subtraction, multiplication, and so on.
Integers are whole numbers, i.e., not decimals or fractions. An integer can be positive, negative, or zero.
Integers are typically stored internally as bytes. A byte is a collection of 8 bits with each bit storing either a 1 or a zero.
When should I use integers?
You should use integers to store and represent anything that is a whole number, e.g., age (measured in years), counts (of entities), zip codes in the US (which are integers), or pages of a book. So when you write a program to handle any of the above entities, you will need integers to represent and reason about them.
Representing integers as bytes
For instance, the number 6 can be represented as the following byte: “00000110”. Note that each bit in the byte denotes a specific place value. In binary, each place value is a power of 2, beginning with 1 (ie 2^0) at the right and going upto 128 (i.e., 2^7) at the left. In the above representation, the two 1’s are at the place values of 2 and 4, and thus the number represented by the above byte is 2+4=6.
You can use Python’s format function to convert integers into their binary representation, like so:
print(format(6,'b'))
Here ‘b’ denotes that you would like to format the given integer, i.e., 6, as binary.
The output is:
110
(note that leading zeros are omitted).
If you would like to try a large number like 255, which is one less than a power of two, we get:
print(format(255,'b'))
with output:
11111111
If we increase the number by just one, i.e., compute the binary representation of 256:
print(format(256,'b'))
we get:
100000000
Another way to extract the binary representation in Python is to use the bin() function, like so:
print(bin(256))
This produces the same output as before:
0b100000000
Note that the output has a “0b” in front which is Python’s way of saying that what follows is a binary representation.
Representing negative integers
To represent negative integers, we must become a bit more creative. Note that the underlying bit representation can only store zeros (0s) and ones (1s). So we must somehow learn to interpret sequences of zeros and ones as sometimes denoting positive numbers, sometimes denoting negative numbers, and sometimes zero itself.
One way to do it is called the “2s complement” representation. Consider bit strings of length 3. Let us first enumerate all bit strings of length 3 systematically:
000
001
010
011
100
101
110
111
As shown above, there are 8 (i.e., 2^3) possibilities. In a 2s complement representation, bit strings with a 0 upfront denote zero and positive integers. Bit strings with a 1 in the leading bit position denote negative integers. Here is how this mapping works:
000 -> 0
001 -> 1
010 -> 2
011 -> 3
100 -> -4
101 -> -3
110 -> -2
111 -> -1
In general, if you have bit strings of length n, the numbers being represented go from 0 to 2^(n-1), followed by -2^(n-1) to -1.
Computing the 2s complement representation
There is an easy way to compute the 2s complement representation. First write down the normal representation. For instance, the representation for 3 would be “011”. Then invert this representation, i.e., flip the bits so that 1s become 0s and 0s become 1s. This is also called the 1s complement representation. For our case, we get “100”. Finally add 1 to this representation. So we will get “101”.
Other ways of representing negative integers
Python has an easier, simpler, way to represent negative integers. It simply stores the negative symbol separately! This works in Python because there is no fixed bit length for integers (i.e., it has arbitrary precision).
We can use the same format() and bin() functions covered above to identify such representations of negative integers. Here is some Python code to do that and also compare it to the representations of corresponding positive integers:
print(bin(256))
print(bin(-256))
print(format(256,'b'))
print(format(-256,'b'))
The output is:
0b100000000
-0b100000000
100000000
-100000000
In the above, Python essentially computes the representation the usual way (i.e., representing the positive integer) and pre-pends a negative sign to the representation.
Operations on Integers
We can use addition, subtraction, and multiplication operations on integers and we will obtain more integers. However, note that division of one integer by another can give rise to a non-integer. Python supports regular division (so the results are returned as floating point numbers) or integer division (where the results are truncated to integers). Here is some code to explore this:
print(25/2)
print(25//2)
Here “/” is the regular division operator and “//” is the integer division operator. The results will be:
12.5
12
as mentioned above.
There is more to learn in this space but we hope we have whetted your appetite about integers and working with integers. If you liked this blogpost, checkout our blogpost on strings.