Number systems in binary. An introduction to number systems in binary.

saxsql 12 views 12 slides Oct 04, 2024
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Number systems in binary.


Slide Content

Denary numbers systems in binary Mr.Kemperman M’Kis - 2021 IB Computer Science

How to store denary numbers? As we have learned in our previous lesson, we need to look at how mathematical concepts in the denary system can be stored in the binary system. In math we use 10 characters to represent numbers: 0,1,2,3,4,5,6,7,8,9. After using all the characters, we ‘reuse’ the characters. The computer only uses two characters, 0 and 1. We ‘reuse’ the result. Denary contains the prefix “de” which refers to ‘ dec ’, 10 in Latin. Binary contain the prefix ‘bi’, which means 2 in Latin. E.g. Bisexual, bilingual The challenge for us becomes: how to store denary numbers in binary? We need to make a mapping between these two number system!

How to convert to binary…… Natural numbers = {1,2,3,……}. In CS we call these unsigned integers . Integer = whole numbers. Unsigned = only positive (sign is – or +). Whole numbers = {…,-2,-1,0,1,2….}. Signed integers in CS. Decimals like 3.14, 0.5, -0.25, -0.00001, 3x10 -10 Real numbers like e, sqrt(2) or pi? Irrational numbers ? What about the operations? 3+4, 0.5x-0.2, 3x4, 0/0? What about 10 Modulo 3 = 1? In CS 10 % 3 = 1

Terminology and concepts B2D = Binary to Denary conversion. Denary uses a base 10, Binary uses base 2. This is the fundamental principal used for conversion. Let’s define some terminology first: 0111010010111 is called a binary string (=sequence of 0’s and 1’s) A bit is one position in a binary string and can either 0 or 1. 0101 0010 is a binary string comprised of eight bits and is called a byte. A byte is comprised of 2 nibbles (half byte = nibble = 4 bits). A binary string has to be read from right to left. The right most bit in a binary string is called the Least Significant Bit (LSB) The left most bit in a binary string is called the Most Significant Bit (MSB)

Representing values in binary: how many different values can you store in n bits? One bit can represent two values. Every bit added double the number of representations. How many different values can you store in 3 bits? #values = 2 3 = 8 representations E.g. how many denary numbers can you represent in 3 bits? Well, 8. What is the biggest signed integer you can store? 7. What is the smallest signed integer you can store? 1. Denary 0 = Binary 0 ! If you use three bits: 10 = 000 2

Conversion B2D B2D = Binary to Denary (aka Decimal) conversion. Straight forward, as we have seen (right). If you need to answer the question: How many bits do I need to represent 78 10 ? Well, find a factor of 2 that exceeds 78 . Why exceeds? 2 4 = 2x2x2x2 = 16, 78>16, so 4 bits is not enough. 2 6 = 2x2x2x2x2x2 = 64, still too low, so 2 7 = 128! 7 bits needed to represent 78!

Conversion D2B D2B = Denary to Binary conversion Let’s take 78 10 , pronounced ‘denary seventy-eight’. We know we need 7 bits to store this. The computer doesn’t use 7 bits, but the working unit is byte. 78 10 = 64 + 8 + 4 + 2, right? Why am I using these numbers? Let’s rewrite. 78 10 = 1 x64 + x32 + x16 + 1 x8 + 1 x4 + 1 x2 + x1. Look’s familiar, doesn’t it. All denary numbers can be written as a sum of powers of 2. Once you’ve done that, you just copy the 0 and 1’s. 1001110 2 is the binary representation of denary 78.

It’s not that hard, Tweety . Keep on dividing by two until you reach 1. Write 1 if number is odd. Write 0 if number is even. 1 is the remainder after division. 0 if there is no remainder. Your answer is binary is shown from bottom to top! 157 / 2 = 78 rem 1 78 / 2 = 39 rem 0 39 / 2 = 19 rem 1 19 /2 = 9 rem 1 And so on…… Read remainders bottom to top! .

Let’s apply this flowchart to 45 denary! Dry-run this algorithm! Get X  X = 45 X > 1? Yes! Calculate X / 2 and X = 45, so 45 / 2. Find quotient (Q) and remainder (R). Well: Q = 22, R = 1, because 22x2 + 1 = 45 Next step: X becomes 22 (X = quotient = 22) Y is the result, the binary string. We add the remainder, which is 1. Y is printed (means put on the screen). We follow the flowchart…. X = 22, X>1, Yes!! Find quotient and remainder… 22 / 2 = 11, Q = 11 and R = 0 ………and we following the same ‘loop’ We stop when X=0 or X=1, because we are done.

Classwork / Homework B2D, D2B conversions No calculators allowed this time! Write down all your steps on how you get the answer. Convert the following denary numbers to binary: 55, 134, 64, 1025, 800 Convert the following binary strings to denary: 10000001, 0001001, 1001, 1111111, 1010101, 01010101. Design a flowchart in draw.io for B2D conversion. Input X is a binary string, Output Y is the denary equivalent.