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.


.

Saturday, 18 June 2022

Chapter 24 // Exercise 12 - 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 24 // Exercise 12

Implement
Matrix<double> operator*(Matrix<double,2>&, Matrix<double>&);
and
Matrix<double, N> operator+(Matrix<double, N>&, Matrix<double, N>&)
If you need to, look up the mathematical definitions in a textbook.


So the first one is already supplied on page 913. Here he refers to it as a vector, but a 1 dimensional matrix is a vector.

For the second I discovered you can't use Bjarne's code to initialise a 3d matrix with a 3d array as it will cause a heap corruption. So I created a little function to use with the inbuilt apply to quickly populate the 3d matrix.

Being able to add a matrix of any incoming dimension was a bit of a head scratcher until I came across this post:

std::transform takes in an iterator to the beginning and end of your container, then uses a function object on each element. Matrix doesn't have iterators but it does supply a pointer to the beginning of the data. So I supplied transform with the raw data pointers, the size of the matrix and a new matrix to put the data into to. This way we don't have to care how many dimensions there actually are.

And with that chapter 24 is done! For once I didn't take 3 months to finish a chapter. Really looking forward to the embedded systems one (which is next) and the final chapter on C programming.

Friday, 17 June 2022

Chapter 24 // Exercise 11 - 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 24 // Exercise 11

Write a swap_columns() to match swap_rows() from section 24.5.3. Obviously, to do that you have to read and understand some of the existing Matrix library code. Don't worry too much about efficiency; it is not possible to get swap_columns() to run as fast as swap_rows().


I started off by modifying the swapRow function I did for vectors in exercise 7. Once that was working, I simply switched the subscripting round. Then copied that into a member function for 2d matrixes.

Thursday, 16 June 2022

Chapter 24 // 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 24 // Exercise 10

How random is your default_random_engine? Write a program that takes two integers n and d as inputs and calls randint(n) d times, recording the result. Output the number of draws for each of [0:n] and "eyeball" how similar the counts are. Try with low values for n and with low values for d to see if drawing only a few random numbers causes obvious biases.


I used some of the code on pg 915/916 but didn't add the normal distribution bit. Originally, the std_lib_facilities file did not seed the default random engine so it wasn't random at all. I changed that a while back and now it uses the pc time as a seed so it's almost always different. On this run, it looked pretty random? That said I just did a 5 draws with a max of 5 over and over and it started to seem pretty suspicious after while. But for a single number I need now and then, it's fine.