1. Data Encodings
Welcome to the first lab! In this session, we’ll connect to the course systems and familiarize ourselves with data encodings in C and C++.
1.1. Remote Access
Three course systems are accessible via the university network under the following hostnames:
elaine00.inf-ra.uni-jena.de
elaine01.inf-ra.uni-jena.de
elaine02.inf-ra.uni-jena.de
You’ll receive your personal access credentials during the lab sessions. Keep those credentials safe because you’ll need them throughout the course.
Tasks
Run the three commands
whoami
,hostname
, andlscpu
on one of the systems.Print the contents of the file
welcome.txt
in 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( int argc,
6 char * argv[] ) {
7 unsigned char l_data = 5;
8 std::cout << "data = " << std::bitset< 8 >( l_data ) << std::endl;
9 return EXIT_SUCCESS;
10}
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}\).
On the systems used in the class, an unsigned char
is one byte (8 bits):
We can write the C++ program shown in Listing 1.2.1.
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 resulting bits! Documentation on C/C++’s built-in operators is, for example, available from Microsoft.
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. Visual Studio Code (VS Code) is one such option.