0% found this document useful (0 votes)
0 views20 pages

CS211 Week6 Slides

The document outlines the schedule for Mini Exams in a CS 211 course, along with an overview of topics covered in Week 6, including bit masking, bit shifting, and binary representation in Python. It explains the conversion between binary and decimal systems, bitwise operators, and provides starter code for extracting and packing bits in an 8-bit integer. Additionally, it includes a brief note on hexadecimal representation and its relationship to binary data.

Uploaded by

andyleeper05
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)
0 views20 pages

CS211 Week6 Slides

The document outlines the schedule for Mini Exams in a CS 211 course, along with an overview of topics covered in Week 6, including bit masking, bit shifting, and binary representation in Python. It explains the conversion between binary and decimal systems, bitwise operators, and provides starter code for extracting and packing bits in an 8-bit integer. Additionally, it includes a brief note on hexadecimal representation and its relationship to binary data.

Uploaded by

andyleeper05
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/ 20

CS 211

Week 6!
Mini Exam 1:
Thurs, Jan 30 th

EXAM Mini Exam II:


SCHEDULE! Tues, Feb 18th
❄️❄️❄️
Mini Exam III:
Thurs, Feb 27th
OVERVIEW OF THIS SESSION:

• Bit masking and bit shifting quick explanation


• Bit code practice
• Extra: MVC
BITS & BYTES

• A bit is a single binary digit


• A byte is 8 binary digits
• A “word” is a sequence of bytes
• Most computer memory is “byte addressed”
• Usually 4 bytes (32 bits) or 8 bytes (64 bits)
depending on the computer (see next slide)
GETTING BINARY REPRESENTATION
IN PYTHON

• Can just call bin(whatever_integer_you_want)


• Prints out string representation
BINARY REPRESENTATION – IS IT
MAKING SENSE?

• When we write a regular • When we talk about


number on paper, we use binary in computers, now
a place value system with we use a place value
base 10 system with base 2
o Called "decimal" o For example, positive
o For example, 53 is 0b101011 is
 (1 x 2^0) + (1 x 2 ^ 1)
 3 x 10^0 + 5 x 10^1
+ (0 x 2 ^ 2) + (1 x 2 ^
3) + (0 x 2 ^ 4) + (1 x 2
^ 5) = 43
BINARY ->
DECIMAL

• To convert a binary
number to decimal,
multiply each digit by
its corresponding
power of 2, starting
from the rightmost
digit as 2^0, and then
add the results.​
DECIMAL ->
BINARY

• Divide decimal number


by 2 and write down
the integer result and
the remainder
• Repeat step 1 with the
integer result until you Binary is 0b1 1 0 1
get 0.
• The binary digits are
read from bottom to
top (from the last
remainder to the first).
TEACH BACK - ON YOUR
WHITEBOARD

• What is 0b11011 from positive binary to decimal?

• What is 37 from decimal to positive binary?


BITWISE "AND"
OPERATOR

• If the bits in the compared


positions of the bit
patterns are 1, then the
resulting bit is 1.
• If not, it is 0.
• How we can apply a mask

https://www.geeks
forgeeks.org/pyth
on-bitwise-operat
ors/
BITWISE "OR"
OPERATOR

• If the two bits in the


looked-at position are 0,
the next bit is zero.
• If not, it is 1.
MICHAL'S VISUAL
BITWISE
RIGHT SHIFT

• Shifts the bits of the


number to the right and
fills 0 on voids left
(logical shift)
• ** Fills 1 in the case of a
negative number
(arithmetic shift)
MSB = most significant bit
BITWISE LEFT LSB = least significant bit
SHIFT

• Shifts the bits of the


number to the left and
fills 0 on voids right as a
result
LET'S PRACTICE ON WHITEBOARDS

• The underscore characters _ have no effect in a numeric


literal. They are there to help you keep track of bit
positions. When we print an integer without specifying
a base, Python uses base 10 (decimal).
• x = 0b1010_1100
• y = 0b0000_1111
• z = (x >> 3) & y
• print(z)
SHIFT AND MASK

Given an 8-bit unsigned integer, extract the following:


• The upper 3 bits (bits 5..7)
• The middle 4 bits (bits 1..4)
• The lowest bit (bit 0)
• Write a Python function to extract each field using bit
masking.
STARTER CODE

• def extract_fields(num: int):


• """Extract fields from an 8-bit integer using bit masking.
• - upper: Extract bits 5..7 (3 bits)
• - middle: Extract bits 1..4 (4 bits)
• - lower: Extract bit 0 (1 bit)
• Returns: A tuple (upper, middle, lower). """
• # TODO: Extract the upper 3 bits (bits 5..7) using bit shifting and masking
• upper = ?
• # TODO: Extract the middle 4 bits (bits 1..4) using bit shifting and masking
• middle = ?
• # TODO: Extract the lowest bit (bit 0) using a mask
• lower = ?
• Return upper, middle, lower
NOW DO THE OPPOSITE... AKA
PACKING THE INTEGERS

• def pack_fields(upper: int, middle: int, lower: int) -> int:


o """Pack upper, middle, and lower fields into an 8-bit unsigned integer.
o - upper: 3 bits (stored in positions 5..7)
o - middle: 4 bits (stored in positions 1..4)
o - lower: 1 bit (stored in position 0)
o Returns: An 8-bit integer with the packed values. """

• num = 0 # Initialize the packed integer


• # TODO: Shift and mask 'upper' into bits 5..7
• # TODO: Shift and mask 'middle' into bits 1..4
• # TODO: Mask 'lower' into bit 0 return num
MODEL VIEW
CONTROLLER
QUICK NOTE ON HEXADECIMAL

• Base 16 (hexadecimal) is excellent:

• Each hex digit represents 4 bits;

• 2 hex digits represent 1 byte

• Digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

You might also like