0% found this document useful (0 votes)
15 views21 pages

Module 1 Class Presentation CS5002

CS5002 module1

Uploaded by

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

Module 1 Class Presentation CS5002

CS5002 module1

Uploaded by

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

CS 5002

Discrete Structures

Class Presentation for Week 1


Align MS in Computer Science

Prof. Steve Shafer [email protected]


Today In Class
• I’m going to start the Teams meeting and hit Record

• Please put away / silence your phone, and have only GoogleDoc, this Powerpoint,
and/or Teams running on your laptop
• Be sure your name card is in place (your instructor will always be fooled)

• We will create a GoogleDoc each day in class. Today’s GoogleDoc Link:


https://docs.google.com/document/d/1K7JZlDfTL-GlJ1R6H9t_ccSat1im4xnMM9Cl
1oH1_Wk/edit

• When you’re ready, give me a “thumbs-up”


• When everyone is ready, we’ll start the class session!
Meet and Greet

• Please introduce yourselves in 50 seconds:


• What you like to be called
• Your country and city of origin
• What did you do during the holiday month before this class started?
Activities You Did Before Class
• Over this week you should have done Problem Set 0.

• Then, following the Study Guide 1, you had some stuff to study
• And then you should have done your Quiz 1 yourself
• It was due at the start of class

• Please submit Problem Set 0 and Quiz 1 now if you did not already
• In the future they are always due before class starts
Review of Problem Set 0

• Discussion thread – Course Introductions

• Magic Squares

• Guess My Number
• https://www.mathsisfun.com/games/guess_number.html

• Perfect Squares
Quiz 1 In Groups
• Do you have any questions about the study materials and Quiz 1?
• How useful were the additional video links? How about the additional tutorial links?
• Now let’s arrange into groups and each group do the same Quiz 1
• Put your answers into today’s GoogleDoc
• Today’s Groups:

Group 1 Group 2 Group 3

Anindo Naren Jingjing


Jeff Ziqun Vivian
Wing Dora Jason (not the TA)
Elisa Timmons Miranda
Counting in Base 2
0=0 1=1
1=1 2 = 10
2 = 10
3 = 11 4 = 100
4 = 100 8 = 1000
5 = 101 16 = 10000
6 = 110
7 = 111 32 = 100000
8 = 1000 64 = 1000000
9 = 1001 128 = 10000000
10 = 1010
11 = ? 256 = 100000000
12 = ? multiply by 2 = add a 0 at the end
Important Powers of 2
• If you have n bits, you have 2n possible numbers (0 to 2n-1)
• Certain values of n are especially important for computers
• Particularly if n is itself a power of 2

• 1b of data is called a bit (b) – it stores 0 or 1


• 4b of data is called a nibble – it stores 0 to 15 = 1 hexadecimal digit
• 8b of data is called a Byte (B) – it stores 0 to 255 = 2 hex digits
• 32b of data is a word – it stores 0 to about 4 billion
• Actually 64b is just now becoming a more common word size
• It stores 0 to a really big number – about 16 billion billion = 16000000000000000000
A Very Useful Coincidence
• 103 = 1000 this is almost the same as 210 = 1024

3 zeros 1,000 kilo 1k = 1024 10 bits


6 zeros 1,000,000 Mega 1M = 1024 kilos 20 bits
9 zeros 1,000,000,000 Giga 1G = 1024 Megs 30 bits
12 zeros 1,000,000,000,000 Tera 1T = 1024 Gigs 40 bits

+3 zeros x1000 x1024 +10 bits

How much fits into a 64b word? 264 = 24+60 = 24 x 260 = 16 with 18 zeros
Important Uses of Hexadecimal
Numbers
• Hex is used to represent binary numbers
• Computers do a lot with “bytes” = 8 bits = 2 nibbles = 2 hex digits
• 01101011 = 0110 1011 = 0x6B note the “0x” prefix commonly
used

• Colors are represented by a Red, Green, and Blue component


• Each is typically 1B = 2 hex digits
• 0xFF0000 = red 255 green 0 blue 0 = all red = brightest purest red
• 0xFFFFFF is white, 0x000000 is black, 0xFF00FF is purple
• https://www.mathsisfun.com/hexadecimal-decimal-colors.html

• When we talk about characters in a string, we usually mean ASCII characters


• https://www.rapidtables.com/code/text/ascii-table.html
A Look At Two’s Complement Numbers
Binary Unsigned Signed Binary Unsigned Signed
0000 0 0 1000 8 -8
0001 1 1 1001 9 -7
0010 2 2 1010 10 -6
0011 3 3 1011 11 -5
0100 4 4 1100 12 -4
0101 5 5 1101 13 -3
0110 6 6 1110 14 -2
0111 7 7 1111 15 -1
What Are We Doing Here?
• Using half of the number range for negative numbers … sort of
0000 Actual bits (binary) 0111 1000 1111

0 7 8 15
Interpreting bits as unsigned number

0 7 -8 -1

Interpreting bits as signed number (two’s complement)

We give up half of the range to hold negative numbers


Why is there one more negative number than positive number?
Negating Binary Numbers In Two’s
Complement
BINARY NUMBER → flip bits → add 1 → NEGATIVE OF THAT NUMBER

Let’s try an example using 4-bit two’s complement: 6 = 0110


What is -6?
Now try negating -6. What do we get?

What happens when we add 6 and -6?

How about -6 + 2? What do we get?

Group session in GoogleDoc – Two’s Complement Arithmetic Practice


4-bit 2’s Complement Answers
Good work! You should have gotten these answers:
a) 5 + 2 = 0101 + 0010 = 0111 =7
b) (-2) + 5 = 1110 + 0101 = *0011 =3
c) (-5) + 2 = 1011 + 0010 = 1101 = -3
d) 6 + 3 = 0110 + 0011 = 1001 = -7 ???
e) (-6) + (-3) = 1010 + 1101 = *0111 = 7 ???

Overflow occurs when the value of an arithmetic result is outside the range of
numbers that can be represented (notice the sign bits!)
Carry occurs when there are not enough bits to represent the answer
An Overflow Example In The C
Language

Check this out: https://en.wikipedia.org/wiki/Integer_overflow


Adding A Number To Its Negative
• Let’s consider 6 + (-6) = 0110 + 1010 = 10000 = 16 = 24
• How about 3 + (-3) = 0011 + 1101 = 10000 = 16 = 24
• The same is true for all the numbers 0 to 7
• Each number added to its negative = 16
• This is for 4-bit 2’s complement

• In general, for n-bit 2’s complement, adding a number to its negative = 2n


• This is what n-bit 2’s complement actually means
Overflow and Sign Bits
• The first bit tells whether a number is positive (0) or negative (1)

• When you add two positive numbers whose result is too large to represent,
the result has a sign bit of 1 -- overflow!

• When you add two negative numbers whose result is too small to represent,
the result has a sign bit of 0 -- overflow!

• Can you get overflow when you add a positive and a negative number?
• You will investigate this in Problem Set 1
Activity For Today – The Game of “Nim”
Our last class activity for today … the game of Nim
• https://www.goobix.com/games/nim/
• See if you can beat the computer in 5 minutes of play
XOR Addition
XOR (“Exclusive OR”) is an operation on bits
• A OR B means “A is 1” or “B is 1” or “both are 1”
• A XOR B means “A is 1” or “B is 1” but “not both are 1”
• So it’s like OR but excludes the case that both are 1
• It also means “not equal” … A and B have different values
• When you have multiple bits, XOR means that an odd number are 1
• Mathematicians write it like this: A  B
• Computer programmers write it like this: A ^ B
We Can Use XOR To Win The Nim Game
We calculate the “NIM SUM” for the current game
• Write the number in each pile, using binary numbers
• Add them up using XOR for each column
• The result is the NIM SUM

If the piles are all empty, the last person just won and the Nim Sum is 0
Otherwise, if it’s your turn and the Nim Sum is not 0, make it 0
But if it’s your turn and the Nim Sum is already 0, you might be a loser!

You will try this out in the Problem Set


Your Homework This Week
• Problem Set 1 is based on today’s class; due in Canvas before next class

• Study Guide 2 is what you need to learn for the next class
• Quiz 2 is based on the study materials; due in Canvas before next class

You might also like