Bit Shifting, 2s Complement Intro to Assembly Language, Lecture notes of Design

Binary Arithmetic: Bit Shifting, 2s Complement ... Convert from binary to decimal AND ... 2s-Complement to Decimal method is the same!

Typology: Lecture notes

2022/2023

Uploaded on 02/28/2023

myfuture
myfuture 🇺🇸

4.4

(18)

258 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Arithmetic: Bit Shifting, 2s Complement
Intro to Assembly Language
CS 64: Computer Organization and Design Logic
Lecture #3
Winter 2019
Ziad Matni, Ph.D.
Dept. of Computer Science, UCSB
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Bit Shifting, 2s Complement Intro to Assembly Language and more Lecture notes Design in PDF only on Docsity!

Binary Arithmetic: Bit Shifting, 2s Complement

Intro to Assembly Language

CS 64: Computer Organization and Design Logic Lecture # Winter 2019 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Why do CPU programmers celebrate

Christmas and Halloween

on the same day?

Because Oct-31 = Dec-

Any Questions From Last Lecture?

5-Minute Pop Quiz!!!

YOU MUST SHOW YOUR WORK!!!

  1. Calculate and give your answer in hexadecimal:

~(0x3E | 0xFC)

  1. Convert from binary to decimal AND to hexadecimal. Use any technique(s) you like: a) 1001001 b) 10010010

Lecture Outline

  • Bit shift operations
  • Two’s complement
  • Addition and subtraction in binary

Bit Shift Left

  • Move all the bits N positions to the left
  • What do you do the positions now empty?
    • You put in N number of 0s
  • Example: Shift “1001” 2 positions to the left

1001 << 2 = 100100

  • Why is this useful as a form of multiplication?

Bit Shift Right

  • Move all the bits N positions to the right , subbing-in either N number of 0s or N 1s on the left
  • Takes on two different forms
  • Example: Shift “1001” 2 positions to the right 1001 >> 2 = either 0010 or 1110
  • The information carried in the last 2 bits is lost.
  • If Shift Left does multiplication, what does Shift Right do?
  • It divides, but it truncates the result

Two Forms of Shift Right

  • Subbing-in 0s makes sense
  • What about subbing-in the leftmost bit with 1?
  • It’s called “arithmetic” shift right:

1100 (arithmetic) >> 1 = 1110

  • It’s used for twos-complement purposes
    • What?

Twos Complement Method

  • This is how Twos Complement fixes this.
  • Let’s write out -6(10) in 2s-Complement binary in 4 bits :

So, –6(10) = 1010(2) according to this rule

0110 1001 1010

First take the unsigned (abs) value (i.e. 6) and convert to binary: Then negate it (i.e. do a “NOT” function on it): Now add 1:

Let’s do it Backwards… By doing it THE SAME EXACT WAY!

  • 2s-Complement to Decimal method is the same!
  • Take 1010 from our previous example
  • Negate it and it becomes 0101
  • Now add 1 to it & it becomes 0110 , which is 6(10)

Another View of 2s Complement

NOTE : Opposite numbers show up as symmetrically opposite each other in the circle.

NOTE AGAIN : When we talk of 2s complement, we must also mention the number of bits involved

Ranges

  • The range represented by number

of bits differs between positive and negative binary numbers

  • Given N bits, the range represented is:

0 to

and

+2N^ – 1 for positive numbers –2N-1^ to +2N-1^ – 1 for 2’s Complement negative numbers

Addition in Binary

  • Same mathematical principal applies

Q: What’s being assumed here??? A: That these are purely positive numbers

Theoretically , I can add any binary no. with N1 digits to any other binary no. with N2 digits. Practically , a CPU must have a defined no. of digits that it’s working with. WHY???

Exercises

Implementing an 8-bit adder:

  • What is (0x52) + (0x4B)?
    • Ans: 0x9D, output carry bit = 0
  • What is (0xCA) + (0x67)?
    • Ans: 0x31, output carry bit = 1