C++ 从入门到入土(English Version) Section 2:Computer Memory and Number Systems

Section 2:Computer Memory and Number Systems


  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).

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值