Monday 25 July 2022

Chapter 25 // Exercise 3 - 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 3

Initialise a 32-bit signed integer with the bit patterns and print the result: 
- all zeros, 
- all ones,
- alternating ones and zeros (starting with a leftmost zero), 
- alternating zeros and ones (starting with a leftmost one),
- the 110011001100...pattern, 
- the 001100110011...pattern, 
- the pattern of all-one bytes and all-zero bytes starting with an all-one byte, 
- the pattern of all-one bytes and all-zero bytes starting with an all-zero byte. 
Repeat that exercise with a 32-bit unsigned integer.

.

Sunday 24 July 2022

Chapter 25 // Exercise 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 // Exercise 2

Make a list of words that can be spelled with hexadecimal notation. Read 0 as o, read 1 as l, read 2 as to, etc.; for example Foo1 and Beef. Kindly eliminate vulgarities from the list before submitting it for grading.

Github: n/a

The first time I saw 0xdeadbeef at work whilst debugging, I thought I had done something terribly wrong. I was told it's often used to initialise unused memory and makes it "stand out" more when debugging.

I used an anagram solver for this one though:
And it gave me 1788 combinations...I won't list them all here but you can put: olizasgtbcdef into the textbox to see what it will generate.

Thursday 21 July 2022

Chapter 25 // Exercise 1 - 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 1

If you haven't already, do the Try this exercises in this chapter.


Pg 939 Complete the program above and print out the addresses and sizes of the objects created to see if and how "holes" appear on your machine. If you have time, you might draw memory layouts likes the ones above to better visualise what's going on.

I couldn't get this one to work, or to visualise what he was showing in the book. When a message was deleted, a new node wouldn't take it's place in that freed up memory; it would go somewhere else. Sometimes it did though.

Pg 959 - Get the bits example to work and try out a few values to develop a feel for binary and hexadecimal representations. If you get confused about the representation of negative values, just try again after reading section 25.5.3.

The bits example wouldn't compile due to a narrowing conversion error. I had to add a static cast to the bitset constructor.

Pg 963 - The following example may look innocent, but it is an infinite loop:
void infinite()
{
unsigned char mx = 160; // very large
for(signed char i = 0; i < max; ++i) cout << int(i) << '\n';
}
Run it and explain why.

A signed char can be in the range of -127 to 127. An unsigned char can be 0 - 255. The signed char therefore will never reach 160 and it will wrap round causing the infinite loop.

Pg 965 - Draw out the bit patterns on a piece of paper. Using paper, then figure out what the answer would be for si=128. Then run the program to see if your machine agrees.

This one was interesting. I was expecting the signed chars to print -127 but they did -128 instead.

Pg 973 - The key was bs; what was the text?

This one took me a while as the code in the book enciphers and I kept wondering why my text file was filled with hex afterwards; turns out I should read the code more carefully.