DEV Community

hwangs12
hwangs12

Posted on

C++ Axioms

My note for c++

1.1.1

Data type char16_t, char32_t, or wchar_t with variables assigned to an octal representation of unicode characters are converted to a decimal when used with std::cout while char in Octal representation is converted to a string literal.

Example

#include <iostream>

int main()
{
    // The following are Octal representation of 
    // a unicode character M.
    char16_t d1 = '\115';
    char32_t d2 = '\115';
    wchar_t d3 = '\115';
    char d4 = '\115'

    std::cout << d1 << " is the \x4d greatest number of all" << std::endl;
    // d1, d2, d3 are converted to decimal, hence shows 77 
    // in the output but d4 will be converted to character M. 

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

1.1.2

Hexadecimal (hex for short) representation uses A, B, C, D, E, F to represent numbers after 9. For example, 4D in hex is equivalent to 77 in decimal because D is equal to 13 and 4 * 16 + 13 = 77.

Example

#include <iostream>

int main()
{
    char16_t yo = '\x4d'; 
    // char16_t type converts to hex int with std::cout per 1.1.1
    std::cout << yo << " is the greatest number of all " << std::endl;
    // 77 is the greatest number of all
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Note: Had A B C D E F not been used, it would have been more difficult to represent decimal numbers like 77 because \x4(13) is longer in length, and without the brackets, it will convert to a much bigger number.

1.1.3

char16_t a = 'abc' then its output is 25187

ASCII code for 'abc' is '61-62-63' hex. Without the dash, '616263' in hex is 6382179 in dec. The largest integer value allowed for char16_t is 2^16. Hence, 6382179 % 2^16 = 25187

Q: Can the above be generalized? I.e. the conversion process from char16_t assigned to an integer is ascii code for each character in hex, convert to decimal, then find the remainder for the largest value permitted for that data type.

1.1.4

char32_t b = '\a' then its output is 7

Image description

source

1.1.5

char32_t b = '\ab' then its output is 1890

'\a' is 7 in ASCII code and 'b' is 98 in ASCII code in decimal which 62 in hexadecimal. Hence 762 when joined represents 7*16^2+6*16+2 = 1890

Data type char16_t, char32_t, or wchar_t outputs a remainder in decimal value of its hexadecimal representation of variables assigned to string literals given the size of each type.

Example

#include <iostream>

int main()
{

    char16_t a = 'abc'; 
    // outputs 25187
    // '616263' hex -> 6382179 in dec -> 6382179 % 2^16 = 25187

    char32_t b = '\ab'; 
    // outputs 1890
    // \ is octal -> 

    char32_t c = 'abcabc';  // 1667326563


    wchar_t d = 'abcabc';   // 1667326563
    long e = 123123123123123;  // 123123123123123

    std::cout << a << "\n" << b << "\n"  << c << "\n" << d << "\n" << e << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)