// file: main_02_MENT.cpp // THIS IS C++, use clang++ #include #include "../helpers/println.hpp" struct PascalString{ int length; // number of chars used char characters[256]; // chars of some character string }; int hexDigitToInt(char hexDigit) { int value = 0; if(hexDigit > 47 && hexDigit < 58) value = hexDigit - 48; if(hexDigit > 96 && hexDigit < 103) value = hexDigit - 97 + 10; return value; } int hexStringToInt(PascalString binaryDigits) { int returnValue = 0; for(int i = 0; i <= binaryDigits.length; ++i) { returnValue += binaryDigits.characters[i]; } return returnValue; } void printPascalString(PascalString s) { for(int i = 0; i <= s.length; i++) { println(s.characters[i]); } } PascalString intToDual(int n) { int i = std::to_string(n).length() * 4; PascalString string = {i}; while(n >= 1) { string.characters[i] = (n % 2) + '0'; n = n / 2; i--; } return string; } char intToHexChar(int n) { if(n > 9) { switch(n) { case 10: return 'A'; break; case 11: return 'B'; break; case 12: return 'C'; break; case 13: return 'D'; break; case 14: return 'E'; break; case 15: return 'F'; break; default: return 'X'; } } else return n + '0'; } PascalString intToHex(int n) { int i = std::to_string(n).length(); PascalString string = {i}; while(n >= 1) { string.characters[i] = intToHexChar(n % 16); n = n / 16; i--; } return string; } PascalString bitwiseDualAnd(PascalString a, PascalString b) { PascalString shortString = a.length < b.length ? a : b; PascalString longString = a.length > b.length ? a : b; PascalString result = {longString.length, '0'}; for(int i = longString.length; i >= 0; --i) { if(shortString.characters[i] == '1' && longString.characters[i] == '1') result.characters[i] = '1'; else result.characters[i] = '0'; } return result; } int main(int argc, char** argv, char** envp) { PascalString s = {3, '1', '0', '0'}; PascalString s2 = {4, 'f', 'f', 'f', 'f'}; println(hexStringToInt(s)); println(hexStringToInt(s2)); println(hexDigitToInt('d')); println(hexDigitToInt('9')); println(hexDigitToInt('2')); printPascalString(s2); int controlRegister = 128; controlRegister |= 64+32; controlRegister ^=16; controlRegister &= 128+64; controlRegister <<= 1; println(controlRegister); println(intToHexChar(6)); println(intToHexChar(10)); printPascalString(intToDual(4106)); printPascalString(intToHex(965)); printPascalString(intToHex(1956)); printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '0'})); return 0; }