UNIHIVE

Study Hub UNIPORT CSC 180.1

CSC 180.1 · Introduction to Computer Science and BASIC Programming

Binary arithmetic and data representation

Adding and subtracting in binary, signed numbers with two's complement, and how characters are coded.

Our notes

1 min read

Binary addition rules

0+0 = 0; 0+1 = 1; 1+1 = 10 (write 0, carry 1); 1+1+1 = 11 (write 1, carry 1).

Worked example

1011 (11) + 0110 (6):
  1011
+ 0110
------
 10001
= 10001₂ = 17 ✓.

Binary subtraction

0−0 = 0; 1−0 = 1; 1−1 = 0; 0−1 = 1 borrow 1.
1101 (13) − 0101 (5) = 1000 (8) ✓.

Binary multiplication

Like decimal, with only 0 and 1: 101 (5) × 11 (3) = 101 + 1010 = 1111 (15) ✓.

Signed numbers

  1. Sign and magnitude: first bit 0 for +, 1 for −. In 8 bits, −5 = 1000 0101. Has two zeros (+0 and −0).
  2. One's complement: flip every bit of the positive number. −5 = 1111 1010.
  3. Two's complement (used by computers): one's complement + 1. −5 = 1111 1010 + 1 = 1111 1011.

Check: 0000 0101 (5) + 1111 1011 = 1 0000 0000; drop the carry out of 8 bits: 0000 0000 = 0 ✓.
Range in n bits: −2^(n−1) to 2^(n−1) − 1; for 8 bits −128 to 127.

Subtraction by two's complement

7 − 5 in 8 bits = 0000 0111 + 1111 1011 = 1 0000 0010 → drop the carry: 0000 0010 = 2 ✓.

Representing characters

  • ASCII: 7 bits (128 characters). 'A' = 65 = 100 0001; 'a' = 97; '0' = 48; space = 32.
  • Extended ASCII: 8 bits (256).
  • Unicode (UTF-8, UTF-16): covers all world scripts, including Yoruba, Igbo and Hausa letters with marks and the naira sign ₦.
  • BCD (binary-coded decimal): each decimal digit in 4 bits: 59 = 0101 1001.

Other data

Real numbers use floating point (a sign, mantissa and exponent, like scientific notation). Pictures are grids of pixels, each with colour numbers; sound is sampled amplitudes.

Exams usually ask

  • Add, subtract and multiply binary numbers.
  • Find the two's complement of a number and use it to subtract.
  • Explain ASCII and Unicode.

Common mistake

Forgetting to fix the word size before taking a complement. Two's complement only makes sense for a stated number of bits (for example 8).

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 180.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.