1. Getting Started
Welcome to the first lab! During this lab we’ll get accustomed to accessing and using the computers in the lab room. These computers will accompany us throughout the entire class and serve as reference machines. We’ll do this by studying the bitwise representation of built-in data types in C/C++.
Hint
Use the reference machines during the regular labs. The open labs are the ideal place to get input on custom settings.
1.1. Accessing the Pis
For completing the assignments, you will need access to the Raspberry Pis. For this you need a KSZ account which you might still have to apply for. For further information, please visit the KSZ-Page. The KSZ accounts allows you to log into the Raspberry Pis in the lab room:
You should see an Ubuntu login screen. Don’t enter your credentials here. First, press Ctrl + Alt + F2.
Now a shell interface opens. There, you can provide your username, press enter, put in your KSZ password and press enter again.
A few lines of text will pop up. You can ignore them. Press enter one more time.
Next, type the command
startx
and press enter. Now you are all set up. Have fun! 😀
After finishing your work, you need to log out of the device:
In the bottom right corner of the screen, press the red power button.
A pop-up will open. Press Logout.
Now you are back in the shell. Just type
exit
, press enter and you’re done!
Please don’t shut the Pi down!
Tasks
Log into one of the lab room’s Raspberry Pis.
Open a terminal and run the two commands
hostname
andlscpu
.Log out from the Pi.
1.2. Working with Bits
We discussed data encodings in class. These are key to understand how numerical, textual and program data are stored. In practice, we might have to manipulate raw data at the bit level. C/C++ offers a series of operators and functions to do exactly this.
The std::bitset
class template represents one convenient option to print the bit representation of built-in data types.
Let’s say we are interested in printing the bit representation of an unsigned char
which holds the value decimal value \(5_{10}\).
Knowing that an unsigned char
has the size of one byte, i.e., eight bits, we could write the following C++ program:
1#include <bitset>
2#include <iostream>
3
4int main( int argc,
5 char * argv[] ) {
6 unsigned char l_data = 5;
7
8 std::cout << "data = " << std::bitset< 8 >( l_data ) << std::endl;
9
10 return EXIT_SUCCESS;
11}
Hint
It’s essential to get comfortable with the command line and being able to invoke a compiler manually. Formulated another way: Do not use an Integrated Development Environment (IDE) or other automated tools to compile or run your code when getting started. However, we do recommend using an editor with syntax highlighting and linting to write your software and hardware designs. VS Code is one such option and pre-installed on the Pis in the lab room.
Tasks
Write C/C++ code to print the binary representations of the following variables:
unsigned char l_data1 = 1; unsigned char l_data2 = 255; unsigned char l_data3 = l_data2 + 1; unsigned char l_data4 = 0xA1; unsigned char l_data5 = 0b1001011; unsigned char l_data6 = 'H'; char l_data7 = -4; unsigned int l_data8 = 1u << 11; unsigned int l_data9 = l_data8 << 21; unsigned int l_data10 = 0xFFFFFFFF >> 5; unsigned int l_data11 = 0b1001 ^ 0b01111; unsigned int l_data12 = ~0b1001; unsigned int l_data13 = 0xF0 & 0b1010101; unsigned int l_data14 = 0b001 | 0b101; unsigned int l_data15 = 7743; int l_data16 = -7743;
For each of the variables, briefly explain why you obtained the respective bits! Documentation on C/C++’s built-in operators is, for example, available from Microsoft.