How things are stored in computer memory?
Computer data storage is a complex subject, but it can be broken down into three basic processes. First, data is converted to simple numbers that are easy for a computer to store. Second, the numbers are recorded by hardware inside the computer. Third, the numbers are organized, moved to temporary storage and manipulated by programs, or software.
Numbers? how?
Every piece of data in a computer is stored as a number. For example, letters are converted to numbers, and photographs are converted to a large set of numbers that indicate the color and brightness of each pixel. The numbers are then converted to binary numbers. Conventional numbers use ten digits, from 0–9, to represent all possible values. Binary numbers use two digits, 0 and 1, to represent all possible values. The numbers 0 through 8 look like this as binary numbers: 0, 1, 10, 11, 100, 101, 110, 111, 1000. Binary numbers are very long, but with binary numbers any value can be stored as a series of items which are true (1) or false (0), such as North/South, Charged/Uncharged, or Light/Dark.
So in memory all looks like this
How integers are stored in memory?
Integer data types in C are typically 1, 2, 4 or 8 bytes in length, or 8, 16, 32, or 64 bits in length, but in memory the numbers are storage
But how do we represent signed binary numbers if all we have is a bunch of one’s and zero’s. We know that binary digits, or bits only have two values, either a “1” or a “0” and conveniently for us, a sign also has only two values, being a “+” or a “–“.
Then we can use a single bit to identify the sign of a signed binary number as being positive or negative in value. So to represent a positive binary number (+n) and a negative (-n) binary number, we can use them with the addition of a sign.
For signed binary numbers the most significant bit (MSB) is used as the sign bit. If the sign bit is “0”, this means the number is positive in value. If the sign bit is “1”, then the number is negative in value. The remaining bits in the number are used to represent the magnitude of the binary number in the usual unsigned binary number format way.
Example
65
Binary equivalent of 65 is (1000001) 2 .
In our case, 65 is positive so MSB will be 0.
1’s complement of a number
One’s Complement or 1’s Complement as it is also termed, is another method which we can use to represent negative binary numbers in a signed binary number system. In one’s complement, positive numbers (also known as non-complements) remain unchanged as before with the sign-magnitude numbers.
1’s compliment of number is just inverting binary bits of an actual number.
Example:
10
Binary representation will be (1010) 2
1’s compliment of 10 is, 0101 (switching 0 to 1 and 1 to 0).
2’s complement of a number
Two’s Complement or 2’s Complement as it is also termed, is another method like the previous sign-magnitude and one’s complement form, which we can use to represent negative binary numbers in a signed binary number system. In two’s complement, the positive numbers are exactly the same as before for unsigned binary numbers. A negative number, however, is represented by a binary number, which when added to its corresponding positive equivalent results in zero.
To get 2’s complement of a number, just add 1 to 1’s complement of an actual number.
This 2’s complement form will be stored in computer.
The main advantage of two’s complement over the previous one’s complement is that there is no double-zero problem plus it is a lot easier to generate the two’s complement of a signed binary number. Therefore, arithmetic operations are relatively easier to perform when the numbers are represented in the two’s complement format.