Binary arithmetic and signed integers
도입됨
21:52
숫자 체계 및 데이터
This photo. This song. This very video. Your name, your messages, every file on every device — inside the computer, all of it is nothing but ones and zeros.…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| truncating | Removing digits after a chosen position without changing the remaining digits. |
| rounding/ˈraʊndɪŋ/ | Changing a number to the nearest value with a chosen number of digits. |
| bits/bɪts/ | A binary digit, taking the value zero or one. |
| binary addition | The process of adding numbers written using the digits zero and one. |
| overflow/ˌəʊvəˈfləʊ/ | 오버플로우 |
| two's complement/tuːz ˈkɒmplɪmənt/ | 보수 |
| signed integers | Whole numbers represented in a form that allows both positive and negative values. |
| carrying | Moving a value into the next column when adding digits. |
| denary/ˈdiːnəri/ | The base-ten number system, using the digits zero through nine. |
| register/ˈredʒɪstə/ | 레지스터 |
| unsigned addition | The process of adding whole numbers represented without a sign. |
| carry out | A bit carried beyond the leftmost position available for the result. |
| most significant bit/məʊst sɪɡˈnɪfɪkənt bɪt/ | 최고위 비트 |
| register width/ˈredʒɪstə wɪtθ/ | The number of bits a register can hold at one time. |
| signed arithmetic | Calculations using numbers represented in a form that allows positive and negative values. |
| sign bit/saɪn bɪt/ | 부호 비트 |
| unsigned integers | Whole numbers represented without a sign, allowing only zero and positive values. |
| inverting/ɪnˈvɜːtɪŋ/ | Changing every zero bit to one and every one bit to zero. |
| processors/ˈprəʊsesəz/ | The component that executes instructions and performs the operations of a computer or similar system. |
| subtractor | A circuit that subtracts one binary number from another. |
| place value/pleɪs ˈvæljuː/ | The value of a digit's position within a number. |
| sign and magnitude | A way to represent signed numbers using one bit for the sign and the rest for size. |
| One's complement/wʌnz ˈkɒmplɪmənt/ | The older scheme: a negative is made by inverting every bit of the positive, with no add-1 step. |
| bit pattern | A particular sequence of zeros and ones used to represent data. |
| adder circuit | A circuit that adds binary numbers to produce a result. |
| Binary Coded Decimal/ˈbaɪnəri ˈkəʊdɪd ˈdesɪml/ | 이진 코딩된 십진수 |
| denary digit | One of the ten symbols used to write numbers in base ten. |
| pure binary | A way to represent a whole number directly using binary place values. |
| seven-segment displays | Devices that show digits using seven separately controlled sections of light. |
| nibble/ˈnɪbl/ | A group of four bits. |
The bank that lost a day
- In November 1983 the Vancouver Stock Exchange index stood at 524.811. It had opened at 1000 twenty-two months earlier, and the market had risen the whole time.
- The program recalculated the index after every trade, truncating 截断 rather than rounding 舍入 each time. Each truncation lost a fraction of a point. Three thousand trades a day did the rest.
- When it was recomputed properly the index was 1098.892: the arithmetic, not the market, had halved it.
- Arithmetic on a fixed number of bits 位 is not the arithmetic you learned at school. This lesson is binary addition 二进制加法, overflow 溢出, subtraction by two's complement 补码, signed integers 有符号整数, and BCD.

Binary addition
- Add column by column from the right, carrying 进位 into the next column, exactly as in denary 十进制.
- The rules: $0 + 0 = 0$; $0 + 1 = 1$; $1 + 1 = 10$, write 0 and carry 1; $1 + 1 + 1 = 11$, write 1 and carry 1.
0101$+$0011$=$1000, that is $5 + 3 = 8$.

Same method as denary, only two digits to carry between
Binary & signed integers
byte = Σ place values
See how an 8-bit pattern maps to a number (and how it would overflow past 255).
Add the binary numbers 0101 + 0011. Give the 4-bit result.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
$5 + 3 = 8$, which is 1000 in binary.
Overflow
- Overflow happens when the result of a calculation cannot be represented in the available register 寄存器 width. In unsigned addition 无符号加法, a carry out 进位输出 of the most significant bit indicates overflow; in signed two’s complement addition, overflow occurs when adding two numbers of the same sign produces a result of the opposite sign.
- It is a property of the register width 寄存器位宽, not of the number 255: in a 16-bit register the same sum is fine.
- In signed arithmetic 有符号运算 the tell-tale is a sign bit that flips wrongly: two positives giving a negative, or two negatives giving a positive.
Overflow in binary addition means:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Overflow occurs when the result cannot be represented in the available bits. In unsigned addition a carry out indicates overflow; in two’s complement addition, two operands of the same sign producing a result of the opposite sign indicate overflow.
Worked example: name the overflow properly
- Add the 8-bit unsigned integers 无符号整数
10110101and01101100and comment on the result. - The sum is
1 0010 0001, which needs 9 bits. The true result is 289. - Full answer: overflow has occurred, because 289 is larger than the largest value an 8-bit register can hold, 255; the carry out of the most significant bit is lost, so the stored result
0010 0001is 33, which is wrong. - The mark is for naming the register width and saying the result cannot be represented in it. "The answer was more than 255" alone does not score.

Two 8-bit unsigned integers are added and the result needs 9 bits. Which is the full-mark explanation?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Name the register width, say the result cannot be represented in it, and say what happens to the carry. The other options state a symptom without the cause.
Subtraction by two's complement
- To calculate $A - B$: form the two's complement of $B$ by inverting 按位取反 every bit and adding 1, add it to $A$, then discard any final carry-out.
- $100 - 30$ in 8 bits: two's complement of
0001 1110is1110 0001inverted, plus 1, so1110 0010. 0110 0100$+$1110 0010$=$1 0100 0110; discard the leading 1 and read0100 0110$= 70$. ✓

Subtraction becomes addition, which is why processors 处理器 need no subtractor 减法器
What is the 4-bit two's complement of 0011? (invert, then add 1)
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Invert 0011 → 1100, then add 1 → 1101 (which represents $-3$).
Put the steps of subtracting B from A by two's complement in order.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Invert, add one, add, discard. The discard is what keeps the answer in the register's width.
Two's complement signed integers
- In an $n$-bit two's complement number the most significant bit 最高有效位 is the sign bit 符号位: 0 means positive, 1 means negative. Equivalently, the top bit carries a negative place value 位权, $-2^{n-1}$.
- To read a negative number: invert every bit, add 1, then put a minus sign in front.
1011 0100inverts to0100 1011, plus 1 is0100 1100$= 76$, so the value is $-76$. Check by place value: $-128 + 32 + 16 + 4 = -76$. ✓ - For $n$ bits the range is $-2^{n-1}$ to $+2^{n-1} - 1$: 8 bits give $-128$ to $+127$, 12 bits give $-2048$ to $+2047$. The most negative value is a 1 followed by zeros; the most positive is a 0 followed by ones.

11111111 is 255 read one way and −1 read the other
Two's complement signed bits
The leftmost bit carries a 음수 place value. Flip any bit — or hit Negate (invert every bit, then add 1) — and watch the signed value change.
Read the 8-bit two's complement number 11111101 as a signed denary value.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
MSB is 1 (negative). Invert → 00000010, add 1 → 00000011 $= 3$, so the value is $-3$.
What is the largest positive value an 8-bit two's complement number can hold?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Range is $-2^{7}$ 부터 $2^{7}-1$, i.e. $-128$ 부터 $+127$. The maximum is 01111111 = 127.
Worked example: write −108 in 12 bits
- Start from $+108$ in 12 bits: $108 = 64 + 32 + 8 + 4$, so
0000 0110 1100. - Invert every bit:
1111 1001 0011. Add 1:1111 1001 0100. - Check with place values, where the top bit is worth $-2048$: $-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$. ✓
- The commonest error is sign and magnitude 原码: setting the top bit to 1 and leaving the rest. That is a different, older scheme and scores zero here.
Write −108 as a 12-bit two's complement number (spaces allowed).
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
+108 is 0000 0110 1100; invert to 1111 1001 0011 and add 1. Check: −2048 + 1024 + 512 + 256 + 128 + 16 + 4 = −108.
One's complement
- One's complement 反码 is the older scheme: a negative is made by inverting every bit of the positive, with no add-1 step. $+30$ is
0001 1110, so $-30$ is1110 0001. - Its drawback is two zeros,
0000 0000and1111 1111, which wastes a bit pattern 位模式 and complicates the arithmetic. - Two's complement has one zero and lets the same adder circuit 加法器电路 do subtraction, which is why it won.
One's complement is preferred to two's complement because it has only one representation of zero.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
The opposite: one's complement has two zeros, +0 and −0. Two's complement has one, and lets the adder do subtraction.
Binary Coded Decimal
- In Binary Coded Decimal 二进制编码十进制 (BCD) each denary digit 十进制数字 is stored as its own 4-bit pattern, using only
0000to1001. - 93 in BCD is
1001 0011, which is not the same as 93 in pure binary 纯二进制,0101 1101. Reading one as the other is a favourite exam trap. - Uses: calculators, digital clocks and seven-segment displays 七段数码管, where each digit is driven separately, and currency, where BCD avoids the rounding errors of storing $0.1$ in pure binary. The cost is wasted patterns, since
1010to1111are never used.

One digit, one nibble 半字节, one display
Write the denary digit $9$ as a 4-bit BCD pattern.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
$9$ is 1001. In BCD each denary digit gets its own nibble (0000–1001).
Match each bit pattern to what it represents.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Nothing in the bits says how to read them. The agreed representation decides the value.
Marks that slip away
- Explain overflow with the register width 寄存器位宽 the question gave, not with "it was more than 255".
- To negate, invert and add 1. Setting the top bit to 1 is sign and magnitude, a different scheme.
- Two's complement subtraction ends by discarding the final carry-out. Keeping it gives a nine-bit answer.
- BCD stores each digit separately; pure binary stores the whole number.
1001 0011is 93 in BCD and 147 in binary.
You've got it
- add column by column with carries; overflow is a result needing more bits than the register holds, and the answer names that width
- subtract by adding the two's complement 补码, invert and add 1, then discard the final carry
- signed: the MSB is the sign bit and carries $-2^{n-1}$; range $-2^{n-1}$ to $+2^{n-1}-1$; read a negative by inverting, adding 1 and negating
- one's complement inverts only and has two zeros; BCD stores each denary digit in its own nibble, for clocks, calculators and currency
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.