Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/01 Computer systems/Data Types/Addition and subtraction of binary integers

Addition and subtraction of binary integers

Addition and subtraction of binary integers.

1.4.1 Data Types

Data Types — Note 4 of 10

Candidates should be able to perform addition and subtraction on integer binary numbers. (These numbers could be positive or negative using two’s complement representation.)

What you need to know

Binary addition follows simple rules with carries. Subtraction is carried out by adding the two's complement of the number being subtracted, so a computer only needs circuitry for addition.

What to know

  • Addition rules: 0+0=0, 0+1=1, 1+1=10 (write 0, carry 1), 1+1+1=11 (write 1, carry 1).
  • Work from the right (least significant bit) to the left, carrying as you go.
  • Subtraction: to calculate A - B, convert B to its two's complement negative and ADD it to A. Ignore any carry out of the MSB.
  • In two's complement, overflow has occurred if two positive numbers give a negative result, or two negative numbers give a positive result.

Key terms

Overflow

The result of a calculation is too large to fit in the number of bits available, so the result is wrong.

Exam technique

  • Tip:Show your carries clearly; method marks are often available even if the final answer is wrong.
  • Mistake:Treating a carry out of the MSB as an error in two's complement subtraction. That carry is simply discarded.

Code examples

Subtraction: 45 - 20 by adding -20
text
  45     0 0 1 0 1 1 0 1
+ -20    1 1 1 0 1 1 0 0
        ---------------
       1 0 0 0 1 1 0 0 1   -> discard the carry out of the MSB
Result   0 0 0 1 1 0 0 1   = 25
← Previous noteSign and magnitude and two's complement to represent negative numbers1.4.1 Data TypesNext note →Represent positive integers in hexadecimal1.4.1 Data Types

Back to Data Types

Data Types

Note 4 of 10

  1. 1Primitive data types: integer, real/floating point, character, string and Boolean
  2. 2Represent positive integers in binary
  3. 3Sign and magnitude and two's complement to represent negative numbers
  4. 4Addition and subtraction of binary integers
  5. 5Represent positive integers in hexadecimal
  6. 6Convert positive integers between binary, hexadecimal and denary
  7. 7Representation and normalisation of floating point numbers in binary
  8. 8Floating point arithmetic: addition and subtraction
  9. 9Bitwise manipulation and masks: shifts, combining with AND, OR and XOR
  10. 10How character sets (ASCII and UNICODE) are used to represent text
Topic overview