Showing posts with label chapter 25 drill. Show all posts
Showing posts with label chapter 25 drill. Show all posts

Monday, 20 June 2022

Chapter 25 // Drill 3, 4, 5 - 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 // Drill 3

Using hexadecimal literals, define short unsigned ints with:
  • Every bit set
  • The lowest (least significant bit) set
  • The highest (most significant bit) set
  • The lowest byte set
  • The highest byte set
  • Every second bit set (and the lowest bit 1)
  • Every second bit set (and the lowest bit 0)

Chapter 25 // Drill 4

Print each as a decimal and as a hexadecimal.

Chapter 25 // Drill 5

Do 3 and 4 using bit manipulation operations (|, &, <<) and (only) the literals 1 and 0.


I cheated with drill 3 and just wrote out the binary then converted it to hex using 

5 was annoying because you can directly type out binary literals in C++14 and assign them to things. The challenge came from only using 1 and 0. Like setting the highest byte can be done with 1 << 15 but 15 isn't 1 or 0.

I honestly had no idea how to do the last 2 using only 1 and 0, so I cheated and just used direct binary strings.


Sunday, 19 June 2022

Chapter 25 // Drill 1, 2 - 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 // Drill 1

Run this:
int v = 1; for(int i = 0; i < sizeof(v)*8; ++i) { cout << v << ' '; v << =1; }

I've never seen <<= before. But shifting to the left by 1 is the "cheapest" and "fastest" way to multiply by 2. In some very low-level engine code at work, I often see >> 1 to divide by 2 instead of / 2, however it's not 1995 anymore and Visual Studio will convert divides/multiplies that are powers of 2 to an equivalent shift for you.

The last number will wrap as it's 1 over the max limit of a signed int.

Chapter 25 // Drill 2

Run that again with v declared to be an unsigned int.


.