CS 429
DISCUSSION 3 (0b011)
Upcoming
Next week:
Thursday, September 19 CI Lab Week 3 (Variables!)
(Feb 15th with 2 Slip Days)
Friday, September 20 Quiz 0x3 on this week’s content
Quiz
● Open Note
● Open Internet
● NO RUNNING CODE
● MUST BE TAKEN IN YOUR DISCUSSION SECTION
● MUST BE TAKEN IN-PERSON!
● Good Luck :)
↓
De
&
Bu
-
B
-
⑳
-
" bit
Little Endian vs. Big Endian LABD
,
big endian
Little Endian
● [LSB]...[MSB]
X %]
--
-
-
● This is generally used as a default for exams! If no endian is expressed, assume
little endian. -
● Ex. 0001 0010 0011 0100 (value) > 0011-
0100 0001 0010 (memory)
○ Note that the order of bits inside each byte stays the same! Only the order of the bytes changes.
Big Endian
X
● [MSB]...[LSB]
-
● Think of numbers that you use. 1 million is expressed by having a 1 (the most
significant number in 1 million) on the left side.
● Ex. 0001 0010 0011 0100 (value) > 0001 0010 0011 0100 (memory)
I
Multiplication & Division Via Shifting 32
Shifting can be used for efficient multiplication and division. t
● Multiplication Y
○ 4*2=8
■ 0100 << 1 = 1000
-
■ We move over 1 bit because shifting to the left multiplies the number by 21.
①
● Division 001
% 010000
Q
,
○ 4/4=1 E
■ 0100 >> 2 = 0001 01 117
+ 0
10000
t
-002
-
2
■ We move over to the right by 2 bits because it divides the number by 2 .
407 ⑦
20 +
⑳
2
2 +
Y
4 + 2 + ) =
7
if you have a , if at b =0 then
,
Subtraction => Y -
X = 2X + 1
b is additi
inverse of a
Subtraction for Unsigned Integers b
a = a + ( b)
-
= a + (vb + 1)
● Simple example: 0110 - 0110
● ~0110 + 1 = 1010 0110 Is I
+
1001 1010
0110
● 0110 + 1010 = 0000 1010
& -
Subtraction for Signed Integers - Turn 1 number negative and then add
-
*
● To get the negative version of an integer, flip 0s to 1s and 1s to 0s, and then add 1.
○ 0001 = 1
○ ~0001 + 1 = 1111 = -1
● Sometimes other numbers don’t have an additive inverse. Signed numbers
○
○
1000 = -8
~1000 + 1 = -8 = 1000
- -
a ,
-
a [ (2) Jak,
-
(a -
b) = a + (- b) = a + (b + 1) unsigned
[0 2 17
,
-
Finite set of numbers you can
represent ( -
32
5-bit numbers 16 17 33
unsigned + =
>
-
overflow ↑
- =
I
O 31
your final carry
Addition ,
if is I then
,
you have an
sunsigned carry
Rule for addition ,
s=atb if sa and sb
have
you unsigned carry
31 o unsigned
I
carry
M 5-bits
j
-
>
S
I & f wheel
16
+
1
[0 , 31]
a =
(1010)
b
Y 110/1)2
=
&
101 0
↑
16 10 I
N
100 ,
I
[
- =
- 16 15
I I
*
-
-
b =>
5 bits >
-
[-16 , 15]
POS + pos 5-bit number
neg
=
neg
t
11101
neg pos
=
- 1 O k= 5
⑪
&
S
-
3 e
↓
14 + 15
inte
-1 -
16
=
15
*
D in
-
16
5
T 5-16 157
,
4 bit >
-
1001 = -
7
Logical vs. Arithmetic Shifts 0011 77A2 =
0000
2001
2 =
0010
M
- 1001 A2 ↑M
remaining
=
I -
R O's in
bit
after least-significanthihon
#
& L
>
-
- 10's infront Y
>
-
[X-1 , Xo] Cut
resents off
I nedness
sig
X k =
logical right shift in C
109 x 103 = 109,000
2017 ,
+ 2 =
10 1 000
#
1101723 = 10 , 000
Bit Masks =
o
● Examples (Integer representations unsigned, but doesn’t matter what type)
○ Using & - Do & (and) for every bit in the same position for both values
■ 8 & 1 = 1000 & 0001 = 0000 = 0 E
---- -
--
-
■ 9 & 1 = 1001 & 0001 = 0001 = 1 -
-
[
○ Using | - Do | (inclusive or) for every bit in the same position for both values
■ 8 | 1 = 1000 | 0001 = 1001 = 9 &
■ 8 | 15 = 1000 | 1111 = 1111 = 15
○ Using ^ - Do ^ (exclusive or) for every bit in the same position for both values
■ 8 ^ 1 = 1000 ^ 0001 = 1001 = 9
● The resulting bits are only 1 if the bits are DIFFERENT. 0 if they are the same.
■ 8 ^ 15 = 1000 ^ 1111 = 0111 = 7
Bitwise Operations
01110100 01110100 01110100 01110100 01110100
>> 5 << 4 & 00111000 ^ 00111000 | 00111000
00000011 01000000 00110000 01001100 01111100
Logical and Arithmetic shifts
- Logical: shift and replace bits with 0
- Arithmetic: shift and replace bits with sign bit
- Left logical/arithmetic are the same (replace with 0)
Half-adder
Ripple-Carry Adder
Full adder
By Marble machine - Own work, CC BY-SA 4.0,
[Link]
+8
k = 4 9 unsigned
198
-
1000
↑
+ -
-10001
C bit
e Ulti
outputs bit string
inputs
(in b) + ( Cour) Go oth
a
, a , ,
bit
b(i)
"All ↓
came " alo] blot of a
bit ,
↓ ↓
T
sh &
iI
[I] S[o]
> even
-
of Is >
- Sag
> odd
#of 1's 1
-
> s =
⑧
-
I I
O
Crack) + ~b [k]n <in]
8
!
I O
I
v(a[k] 1 - b[k] 1 cin]
10I
S
[ O :
I
I
(aChJ1bEk])nCin = S
↓
↓
1 1 - 0
Bit
Carry
2 is
from aCk] ,
b(k] , <in
3 Is
w
If
bothl both is --
(a(k) 1 b(k)) v(a(k] 1
(in) v(b(k]1(in)
Subtraction
-
a -
b =
a + ~b
a
↓
Lo] vb[o]
↓ i
# lesy
car traction
↓
S(0)
CI Lab Week 3 - Variables & Hash Tables
- In Week 3, we’ll implement variables. Internally these are
backed by a hash table, similar to CS 314’s hash table.
- The only things you have to implement are:
- put() - add/update an entry to the hash table
-
- get() - retrieve an entry from the hash table
-
- infer_type()
-
- if a leaf node is a variable, our build_leaf doesn’t
-
-
automatically get the variable data. You have to use get() to retrieve
the variable and update the type and value.
- cleanup() - make sure to free space for variable name storage
-
RTYPE
please try to use gdb if you segfault
inter-type
has
[
a = S > -
put in
map
a
=3
get from has nep
err-undefined
key = hash (var name)
-
I
Xb
⑰ as
-
w
linkedList
1. redefine variable
↓ hash collision
I
itrave
Resources
Integer representations worksheet
Cinderblocks worksheet