Data representation (part 1)

This lesson contains approximately 26 minutes of video content.

Binary number system


Motivation for study

Positional number systems & the binary number system

Binary to decimal & decimal to binary conversions

Binary addition

Activity: Unsigned base2 value to corresponding decimal number

Graded Playground Autograder

Activity Prompt:

In this question, you will define Base2ToBase10, taking a single std::string value passed by reference to const, and returning an int. The std::string will encodes the base-2 value your implementation of Base2ToBase10 must convert to base-10. The base-2 values will be unsigned. That is, the entire representation used for positive. The representation for this problem will be 8-bits. The value will be passed in the following format:

Value (as char)0b00000011
Index (in std::string)0123456789

Your function must ensure that the std::string passed to Base2ToBase10 follows the format we specify. At indices 0 and 1, the characters '0' and 'b' respectively present. "0b" is the "prefix" to the binary value: these characters do not contribute to the base-10 value. However, "0b" must be present for the std::string to be considered as encoding valid base-2 value. Indices 2 through 9 stores the base-2 number you're to convert to base-10. The least significant bit (i.e., raised to 2^0) of the binary number is found at index 9, and the significance of bits increases in the power of two from right to left (or decreasing index value). Therefore, the characters residing at indices 2 through 9 must be either '1' or '0'. If the std::string passed to Base2ToBase10 fails to follow this format, your implementation must throw an std::invalid_argument exception.

When a valid std::string argument is passed to Base2ToBase10, you must convert the base-2 encoded value in the std::string to its base-10 equivalent. Remember, the base-2 value passed to your function is unsigned. That is, the entire 8-bit representation is used to encode a positive number. First, let's acknowledge that a std::string stores characters. When performing the base-2 to base-10 conversion, the binary digits you must consider are encoded as characters '1' or '0'. These characters must be converted to integer values. We have provided a function for you: CharIntToInt(char c). Invoking CharIntToInt('0') will return 0, and invoking CharIntToInt('1') will return the integer value 1. To raise a base a to some exponent b, you can use pow(a, b). The pow function is defined in <cmath>. Your implementation of Base2ToBase10 must return the decimal equivalent as an int.

Examples

Function invocation Behavior
Base2ToBase10("0b00000011") returns 3
Base2ToBase10("0b00000000") returns 0
Base2ToBase10("0b11111111") returns 255
Base2ToBase10("0b11112111") throws std::invalid_argument
Base2ToBase10("1b11111111") throws std::invalid_argument
Base2ToBase10("1x11111111") throws std::invalid_argument
Base2ToBase10("0b11100011111") throws std::invalid_argument
#include <iostream> #include "solution.hpp" int main() { // std::cout<< Base2ToBase10("0b11111111") << std::endl; }
#ifndef SOLUTION_HPP #define SOLUTION_HPP // Provided int CharIntToInt(char c); #endif
#include <sstream> #include "solution.hpp" // provided functions int CharIntToInt(char c) { std::stringstream ss; ss << c; int value = 0; ss >> value; return value; }