0% found this document useful (0 votes)
18 views32 pages

Numeration Inked

Uploaded by

kongdehou4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views32 pages

Numeration Inked

Uploaded by

kongdehou4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Numeration

COMPSCI 1020
Outline
• Binary numbers
• Operations on binary numbers
Computer Number Systems
• In computers, information is transmitted using
electricity.
• Electricity moving through a wire is represented as 0
(off) or 1 (on).
• Because of this, computers use a number system
that only uses digits 0 and 1.
• Called binary numbers
What do we use?
• Our numeration system is decimal.
• It uses the digits 0-9 to count.
• Numbers larger than 9 are represented using
multiple digits.
– The value of a multi-digit number is calculated
using powers of 10.
Binary numbers
• Only digits are 0 and 1.
– Numbers larger than 1 need multiple digits.
• Next number after 1 is 10.
• Then 11 .. Then what?
• How do we understand binary numbers in terms of
their actual value?
– Decimal numbers were represented using powers
of 10
– Binary numbers are represented using powers of
2.
Sequence of Binary Numbers
• How do sequences of numbers work?

• …88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101…

• In binary:
Binary numbers
• Sequence is 11, 100, 101, 110, 111, 1000, …
• How do we understand binary numbers in terms of
their actual value?
– Decimal numbers were represented using powers
of 10
– Binary numbers are represented using powers of
2.
Binary Number Example
• Consider 10110. What value does it correspond to?
Terminology for binary numbers
• We refer to “binary digits” as “bits” – one bit is
either 0 or 1.
• We often also talk about “bytes” (8-bit numbers).
• We won’t worry about bytes because they are used
in the context of computer memory.
– 1 TB hard drive = 1 trillion bytes of storage.
Terminology - Base
• We also can talk about the base of a number.
• Decimal is “base 10”
• Binary is “base 2”
Notation for numbers
• Since we need to talk about multiple number types,
we need some notation.
• If X is a string of 0s and 1s (representing a binary
number) then dec(X) is the numerical value of the
string.
• e.g., what is dec(10010) ?
• If n is a decimal number, then bin(n) is the string of
0s and 1s (without leading zeroes) whose value as a
binary number is n
Binary Number: conversions
• How do we convert a number from binary into
decimal?
• How do we convert a number from decimal into
binary?
Binary to Decimal
• We’ve seen this already.
• Use the representation form using exponents.
Binary to Decimal
• Another way to think about conversion: assign a
power of two to each position.
• e.g., convert 1001011001 to decimal.
Decimal to Binary
• To convert a decimal number to binary we will use an
algorithm.
• Need two operations first
Helpful Operation: integer division
• Integer division: x div y = x divided by y with no
fractional part.
– In python, operation is //
– In Java/C/C++/C#, operation is / with two integers.
Helpful Operation: remainder
• Remainder: x mod y = remainder when x is
divided by y. (what’s “left over”)
– Obtained from the grade-school division algorithm
– In python and java, use %.
Decimal to Binary
Input: positive number n (n>0)
Output: the bin(n), the binary number with value n.

1. d = n mod 2
2. n = n div 2
3. next (higher) digit of output is d
4. if n > 0 then go to step 1.
Decimal to Binary Example
• Let’s find the binary value of 119.
Operations on Binary Numbers
• How do we add two binary numbers?
• Can use our standard “grade school” algorithm.
• Just need to remember that 1+0 = 0+1=1 and 1+1=10
in binary 1 1 0 1 1
• 11011 + 1010 = ? + 1 0 1 0

• Check: dec(11011) = 27 and dec(1010) = 10


• Result =
• In Decimal =
Carry digit in binary addition
• Let’s do another addition: 1011+11 = ?

• Notice how the only carry digit can only ever be 1.


• This is important for computer hardware.
Binary Multiplication
• Binary multiplication can be done using grade school
algorithm too.
• Easy because we only need to multiply by 0 and 1.
• Then add.
Negative numbers?
• In decimal numbers we represent negative numbers
using a negative sign before the number.
• What can we do in binary numbers to represent
negative numbers?
• “Two’s complement”
• Usually consider with fixed width binary numbers
(i.e., a given size, 8 bit, 16 bit, etc.)
Two’s Complement
• We will use the most significant digit for representing
whether a number is positive or negative.
– The most significant digit is the first digit when
reading left-to-right.
• The remaining digits (not including the most
significant) will represent the number (without the
sign).

1 1 0 1 0 0 0 1 0 1
Two’s Complement - Positive
• Most significant digit is 0
• Remaining numbers represent a binary number as
we’ve seen.
• To fit in a fixed width, may have additional leading
zeroes.
• e.g., +13 as an 8-bit number.
Two’s Complement - Negative
How do we get two’s complement of a number -X ?

1. Take the binary representation of (+X) (with sign bit)

2. Flip all the bits, including the sign bit.

3. Add 1 to this number, ignoring any overflow.


Two’s Complement
• What is bin(-119) as an • Add the binary
8-bit number? representations of 119
• Hint: we already and -119.
showed that bin(119) =
1110111
Two’s Complement
• Add the binary representations of 119 and -119.
Other Numeration Systems?
• We could play the same game with other numbers to
form other numeration systems.
– E.g., octal numbers are numbers in base 8.
– 0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,21…
• Hexadecimal systems are important in computer
systems.
Hexadecimal Numbers
• Hexadecimal numbers (base 16) are important in
computer science.
– Digits are 0,1,2,…,9,a,b,c,d,e,f
– Digits 0-9 are used as normal
– Digit a means 10, digit b means 11, digit c means
12, …. digit f means 15.
• Represent numbers using powers of 16.
Hexadecimal Hands
• It’s impossible to get AI to generate an image of
hands with eight fingers.
Hexadecimal Examples
• Use dec(X) to represent the value of a hexadecimal
number in decimal.
• dec(9f)

• dec(bee)

You might also like