Download presentation
Presentation is loading. Please wait.
Published byPhillip Bradley Modified over 8 years ago
1
Two’s Complement The language of machines, part II
2
Start with a game: http://forums.cisco.com/CertCom/gam e/binary_game_page.htm http://forums.cisco.com/CertCom/gam e/binary_game_page.htm 5 minutes, highest score wins!
3
Starting with the least significant bit (left to right), add bitwise and remember to carry if needed. Double check in denary if needed. Addition (reminder) 111Carry 10100110 +00110011 11011001
4
Problem We know how to express the naturals using binary, but what about the integers? (Integers are all the positive and negative whole numbers and zero) We don’t use a minus sign – why?
5
One’s Complement We decide on the number of bits to represent our number, we will typically use a byte. The most significant bit becomes the sign bit. There is a big issue with one’s complement… 10110100 sign bit
6
Two’s Complement Two’s Complement allows us to express every number between -2^(n-1) and 2^(n- 1)-1, where n is the bit length. Zero is always just 00000…000000 -1 is always what?
7
The Method Decide on a word length Start with your absolute value in binary Next, reverse all of the bits Finally, add 1. Express these in base-2 using one byte: -42 -10 -128 -81 -93
8
Subtraction Easiest way to subtract two numbers is to convert it to an addition, how can we do this? Addition is easier for most machines than subtraction, why would this be the case?
9
From binary to hexadecimal For human programming, binary is cumbersome and error prone. We almost always deal in bytes, and so can use the property that 8 bits = 1 byte = 2 hexadecimal digits Hexadecimal (or just hex) is the base-16 number system.
10
Hex It requires 16 digits starting from zero: 00 11 …… 88 99 A10 B11 C12 D13 E14 F15
11
Converting to hex from binary Start with the least significant bit and, for every 4 bits, replace with the hex digit, e.g. 1.10011110-- start with binary 2.10011110-- ‘split’ into 4-bits 3.913-- convert to denary 4.9C-- convert to hex 5.9C-- put together
12
Denary – Binary - Hexadecimal 1.Say someone’s name then a denary number between 0 and 15 2.That person says the 4-bit binary equivalent and then someone else’s name 3.That person says the hexadecimal digit and starts again from step 1. (what’s wrong with this algorithm?)
13
Recap Binary addition One’s Complement Two’s Complement Subtraction Hexadecimal
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.