Section 2:Computer Memory and Number Systems
文章目录
- Section 2:Computer Memory and Number Systems
- The flipflop
- Number Systems
- The Decimal number system
- The Binary number system
- Carry 1 in binary
- Bits and Bytes
- An interesting thought
- Converting binary to decimal
- Converting decimal to binary
- The Hexadecimal number system
- Converting hexadecimal to decimal
- Converting decimal to hexadecimal
- Converting binary to hexadecimal (IMPORTANT)
- The Octal number system(this has NOTHING to do with our course)
- How a computer represents negative integers
- Method 1: Sign and Magnitude
- Method 2: Ones Complement
- Method 3: Twos Complement
- Definitions –you need to know these
- Largest integer in a computer
- Program to check the size of a variable
- maxint (IMPORTANTfor all programmers)
This section looks at how computer memory is constructed from circuits and how computers store integer values in memory. People usually work in the decimal number system but computers work in binary. Hexadecimal is used as a shorthand for writing binary.
The flipflop
The flipflop is a circuit with 2 inputs and 1 output. It is an independent circuit (it does not require continuous input) and it is a bistable circuit (it has 2 states). We could study the circuit required to construct a flipflop but we will not do that.
Number Systems
Any number system has a base and a set of digits to represent the number. The digits are arranged in columns where each column represents a different power of the base.Number systems have been used for thousands of years –they have nothing to do with computers.
The Decimal number system
Let us start off by studying the decimal number system.For thousands of years, humans have used the decimal number system for counting. It is convenient to use the decimal number system because we have ten fingers. This is why the English word ‘digit’ means a number symbol and also means a finger (or a toe). However, not all societies have used the decimal number system. For instance, in about 3000BC, the Babylonians used the number system with base 60. And that is why we have 60 seconds in a minute and 60 minutes in an hour.
Important definitions:
The Binary number system
The binary number system collects numbers into groups of 2. The baseof the binary number system is 2. The digits of the binary number system are 0 to 1. To represent any number we write down how many groups we have of each power of 2.
Carry 1 in binary
Bits and Bytes
An interesting thought
An interesting exercise is to calculate what is the maximum value that can be stored in a specified number of bits. Easy to do, just assume every flipflop is ON (i.e. assume every bit is 1).
E.g. maximum value you can store in 4 bits is: 1111 = 15 (Fun fact: 4 bits is called a nibble).
As computers increased in power, they were able to include thousands of bytes in the memory.210(1024) bytes is called a kilobyte(written as KB). It is sometimes assumed that a kilobyte is1000 bytesif precision is not required.A megabyte is 1,000,000 bytes, etc.
Converting binary to decimal
Converting decimal to binary
The Hexadecimal number system
Converting hexadecimal to decimal
Converting decimal to hexadecimal
Converting binary to hexadecimal (IMPORTANT)
It is very easy to convert binary to hexadecimal by looking at groups of 4 binary digits:
For example, convert the binary number 1001102to hexadecimal.
Put the binary into groups of four(place extra zeros at the front if needed).
Thus 100110 becomes 0010 0110
Now write the single corresponding hexadecimal digit for each group of four.
So 1001102becomes 2616
We use hexadecimal as a shorthand for writing binary.
The Octal number system(this has NOTHING to do with our course)
How a computer represents negative integers
Computers have a fixed word size. This is the number of bits that are available to store one integer value. For example, in a computer with a word size of 8, there will be 8 bits used to store one binary number. So the number 5 (in such a computer) will be stored as: 0000 0101
In a computer with a word size of 16, the number 5 will be stored as: 0000 0000 0000 0101We need to represent negative numbers and we use the fact that computers have a fixed word size.
Method 1: Sign and Magnitude
The most significant bit(at the left hand side of the number) becomes the sign bit.
The most significant bit = 0 for a positive number and 1 for a negative number.
Method 2: Ones Complement
Method 3: Twos Complement
Complement thenumber and add 1.
Definitions –you need to know these
Largest integer in a computer
Program to check the size of a variable
maxint (IMPORTANTfor all programmers)
maxintis the largest integer value that can be stored in a computer. The size of maxint will differ, depending on the variable used (e.g. long) and the make of computer BUT there is always a maxint. It is very important for programmers to know about maxint. In other words, you must know that there is a particular integer value that if you add 1 (or greater) to it, you will get a meaningless answer. Many programmers create programs without realising that they may set up a series of calculations that could produce a meaningless result(if the final result is greater than maxint).