1. Data Encodings
Welcome to the first lab! In this session, we’ll log in to the workstations and familiarize ourselves with data encodings in C and C++.
1.1. Workstations
The lab room is equipped with HP Z2 Mini G1a workstations. Each has an AMD Ryzen AI Max PRO 390 system-on-a-chip (SoC) and 32 GiB of LPDDR5X-8000 memory. You can use the workstations in your lab session and in the open labs. In the lab, you’ll receive personal login credentials for the whole course. Keep them safe.
Tasks
Log in to a workstation and run the commands
whoami,hostname, andlscpu.Print the contents of the file
welcome.txtin your home directory.
1.2. C/C++ Data Types
Data encodings are essential to understanding how numerical, textual, and program data are stored. We’ll explore different data encodings by examining the bit-level representation of built-in C/C++ data types. In practice, we’ll often need to manipulate raw data at the bit level. C/C++ provides operators and functions for this.
unsigned char using std::bitset.1#include <cstdlib>
2#include <bitset>
3#include <iostream>
4
5int main() {
6 unsigned char l_data = 5;
7 std::cout << "data = " << std::bitset< 8 >( l_data ) << std::endl;
8 return EXIT_SUCCESS;
9}
The std::bitset class template is a convenient option to print the bit representation of built-in data types.
Suppose we want to print the bit representation of an unsigned char, which holds the decimal value \(5_{10}\).
An unsigned char always occupies one byte, and on the workstations, a byte has 8 bits.
The C++ program in Listing 1.2.1 therefore uses std::bitset<8> to print its bits.
Tasks
Write a C++ program that prints 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 resulting bits! Documentation on C/C++’s built-in operators is, for example, available from cppreference.
Hint
It’s essential to become comfortable invoking the compiler from the command line. In other words, avoid using an Integrated Development Environment (IDE) or other automated tools to compile or run your code while you’re getting started. However, we recommend using an editor with syntax highlighting and linting to write your software and hardware designs. VSCodium is one such option.