Integer Representation
Learn common ways of representing integers in computer memory.
We'll cover the following
Arithmetic expressions
Bash allows us to do calculations on integers. We can apply the following operations: addition, subtraction, multiplication, and division. Besides that, Bash provides bitwise and logical operators. We will use them often when programming.
Bash does not support floating-point arithmetic. If we need this arithmetic in our scripts, we can use the bc or dc calculator.
Integer representation
The first question we should consider is integers’ representation in the computer’s memory. This knowledge helps us understand mathematical operations in Bash.
We already know integers from mathematics. They do not have a fractional component and can be positive or negative. Programming languages with the static type system provide an integer type of variables. We should use this type whenever we need to operate on integers.
We can be very precise in programming and specify if the integer variable is positive only. The integer of this kind is called unsigned. If it is either positive or negative, the variable is called signed.
There are three common ways of representing integers in computer memory:
-
Sign-magnitude representation(SMR)
-
One’s complement
-
Two’s complement
Get hands-on with 1200+ tech skills courses.