Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Computer instruction words almost always pack several different values into a single integer word. For our computer simulator, we'll need to both pack multiple fields
Computer instruction words almost always pack several different values into a single integer word. For our computer simulator, we'll need to both pack multiple fields (operation code and operands) into individual words, and to extract them from words. This week's project is to build a BitField class that provides a tool for inserting and extracting an individual field. fill in FIXME WORD_SIZE = 32 class BitField(object): """A BitField object handles insertion and extraction of one field within an integer. """ def __init__(self, from_bit: int, to_bit: int) -> None: """Tool for inserting and extracting bits from_bit ... to_bit, where 0 is the low-order bit and 31 is the high-order bit of an unsigned 32-bit integer. For example, the low-order 4 bits could be represented by from_bit=0, to_bit=3. """ assert 0 int: """Construct a mask for the first width bits""" assert width >= 0 mask = 0 for bit in range(width): mask = (mask int: """Insert value of field into word. For example, if word is xaa00aa00 and field_val is x0000000f and the field is bits 4..7 then insert gives xaa00aaf0 """ #FIXME return 0 def extract(self, word: int) -> int: """Extract the bitfield and return it in the low-order bits. For example, if we are extracting the high-order five bits, the result will be an integer between 0 and 31. """ #FIXME return 0 def extract_signed(self, word: int) -> int: """Extract the bitfield and return it in the low order bits, sign-extended. """ unsigned = self.extract(word) # Sign extend if negative if unsigned & self.sign_bit: return 0 - (self.comp - unsigned) else: return unsignedPacking and extracting bit fields in Python integers A BitField object is a tool for inserting a field into an integer or extracting a field from an integer. For example, suppose we wanted to keep some values in the first four bits (bits 0..3) and other values in the second four bits (bits 4.7) of integers. We would define two BitField objects: 10x4 = BitField (0,3) md4 = BitField(4,7) Then we could pack two small numbers, each between 0 and 15 inclusive, into one integer: packed = 0 packed = low4. insert(7, packed = md4. insert (8, packed) packed) At this point the value of packed is 135, but let's not think about it in decimal. Think in hex, where each hexadecimal digit represents 4 binary bits. Now the value is clear: hex (packed) 0x87 There is our 7, in the low digit, and our 8, in the high digit. We can also use the BitField object to extract the individual fields: >low4.extract(packed) 7 mid4.extract(packed)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started