CS208 Introduction to Computer Systems Monday, 25 September 2023 + Today - More integers: recap, byte order, and printf - Bitwise operations - Character sets & character encodings + Integer recap - Base 2 (binary) 111011 - Base 10 (decimal) 32 + 16 + 8 + 0 + 2 + 1 = 59 - Base 16 (hexadecimal) 0011 1011 --> 3B - Hexadecimal: 4-bit chunks, 0x notation, %x and %X in printf + Fixed-length integers - each int is 4 bytes (with our version of gcc) - each long is 8 bytes - byte order - the problem - Jonathan Swift, Gulliver's Travels (1726), and eggs - "little-endian" vs. "big-endian" - "On Holy Wars and a Plea for Peace" -- Danny Cohen, 1980 + Bitwise operations & bitwise AND | bitwise OR ^ bitwise XOR Try it by hand: j = 0xB9; k = 0xC3; j & k j | k j ^ k j >> 4 j << 4 ~j Try printing the results: int j = 0xB9; int k = 0xC3; printf("0x%X & 0x%X == 0x%X\n", j, k, j & k); printf("0x%X | 0x%X == 0x%X\n", j, k, j | k); printf("0x%X ^ 0x%X == 0x%X\n", j, k, j ^ k); printf("0x%X >> 4 == 0x%X\n", j, j >> 4); printf("0x%X << 4 == 0x%X\n", j, j << 4); printf("~0x%X == 0x%X\n", j, ~j); // this gets weird + Character sets and character encodings - ASCII (character set) - the "ASCII value" of a character - see "codepoint" below - Unicode (character set) - unicode.org - codepoints vs. characters - UTF-16BE (character encoding) - UTF-16LE (character encoding) + Strategies - to_upper & to_lower - middle_bits - to_utf8 - from_utf8 + Printing integers int k = 1234; printf("%d\n", k); printf("0x%X\n", k); int k = -1234; printf("%d\n", k); printf("0x%X\n", k); + Printing characters char ch = 'A'; printf("%c\n", ch); printf(" + char, byte, character - ASCII - Unicode (much more next week) - od (Unix command) - hexdump (Unix command)