Spring semester 2022/23
You are given an integer number A and an index i. Find and print the bit at position i in the number. The least significant bits have the lower indices.
Example input:
9 1
Example output:
0
You are given an integer number A in the range 0 <= A <= 4*10^18. Print the number of bits set to 1 in the binary representation of A.
Example input:
9
Example output:
2
Example input:
126
Example output:
6
Write a function that takes an unsigned integer number and prints its binary representation. Print all 32 bits, including leading zeros. Separate groups of 4 bits with apostrophes (').
Additional task: Implement a version of this function that works with signed integers.
Example input:
5
Example output:
0000'0000'0000'0000'0000'0000'0000'0101
Study the structure of the test project, make sure you can run it.
Add a new file bitwise_test.cpp to the project. Implement EXPECT_EQ tests for tasks 1-3 from the previous workshop in this file.
- Task 1, getting a bit at an index.
- Several checks for bits equal to 0 and 1. Use binary literals to define numbers for testing.
- Checks for indices 0 and 31 both for numbers where they should be 0 and where they should be 1.
- Task 2, counting bits equal to 1 in a number.
- Checks for a number with 64 ones, 0 ones, some random numbers.
- Task 3, output.
- Rewrite the function to output the number as a string instead of printing it. (If you haven’t finished this function during the last workshop, you may reverse a string instead of directly printing it in the reverse order).
- Check 1-2 random numbers.
- Check that numbers which have 1 as their first bit are outputted correctly.
- If you implemented the signed version of this task, check that it works with negative numbers, including ones which have 0 as their second bit.
Exceptions. Reading incorrect data.
You have a function that reads data and fills empty "ID", "Age", "SibSp" and "Parch" cells with 0. However, there are no checks implemented for some other columns, and if empty data appears in them, the program will not work.
-
Try loading data from the file "titanic_empty_fare.csv".
-
Fix the program so that it warns the user that the file was corrupted, similar to the existing check.
The original version of the program assumes that if a line was read correctly, it contains a correct number of columns. However, this may not be the case.
-
Try loading data from the file "titanic_no_columns.csv".
-
Add a check to the cell reading function
extractDatato see if all the cells were read correctly. Give a warning and skip the row, if there was a mistake in that row.
To skip the row, move the line that adds a passenger to the vector to a try block like this:
std::stringstream lineStream(buffer);
try
{
Passenger newPass = extractData(lineStream);
passengers.push_back(newPass);
}
catch(std::runtime_error const& ex)
{
// warn the user that a row was skipped
}Implement the following logic in your application.
When the program encounters incorrect data, it should ask the user for input. The user has 4 options.
- Skip the row as corrupt.
- Fill the row with default data, trying to "repair" it and add it to the vector of passengers.
- Choose and remember option 1, so the user isn't asked anymore.
- Choose and remember option 2, so the user isn't asked anymore.
Add option 5: "Exit the program" by throwing another exception and catching it in main().