Sunday 20 November 2022

Chapter 25 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 10

Look at the bitfield example from section 25.5.5. Write an example that initialises a PPN, then reads and prints each field value, then changes each field value (by assigning to the field) and prints the result. Repeat this exercise, but store the PPN information in a 32-bit unsigned integer and use bit-manipulation operators (section 25.5.4) to access the bits in the word.
When I first read this, my immediate thought was "wtf?" Then I read it again, looked at the example and reminded myself it's just a simple bitfield exercise. Then I got to the second half where you have to read and write to an integer and that took me an hour to wrap my head around.

It's safe to say though, I now understand how shifting and masking works. It's actually much simpler that google makes it out to be. I hope my comments on the code show that. I found drawing it out on paper really helped visualise which bits to mask/shift.


No comments:

Post a Comment