// 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 }; char cStringArea[1024]; 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++) { print(s.characters[i]); } println(""); } 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) { switch(n) { case 0: return '0'; case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4'; case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9'; case 10: return 'A'; case 11: return 'B'; case 12: return 'C'; case 13: return 'D'; case 14: return 'E'; case 15: return 'F'; default: return 'X'; } } 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; } int getInt(char character) { if(character == '0') return 0; else return 1; } PascalString bitwiseDualAnd(PascalString a, PascalString b) { PascalString result = {a.length, '0'}; for(int i = b.length; i >= 0; --i) { if(a.length < i) result.characters[i] = a.characters[i]; println("i: ", i, " ", intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i]))); result.characters[i] = intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i])); } return result; } PascalString bitwiseHexAnd(PascalString a, PascalString b) { PascalString result = {a.length, '0'}; for(int i = b.length; i >= 0; --i) { if(a.length < i) result.characters[i] = a.characters[i]; println("i: ", i, " ", a.characters[i] & b.characters[i]); result.characters[i] = a.characters[i] & b.characters[i]; } return result; } int to_c_string(PascalString s) { } void line() { println("==================================="); } int main(int argc, char** argv, char** envp) { PascalString s = {3, '1', '0', '0'}; PascalString s2 = {4, 'f', 'f', 'f', 'f'}; println(hexStringToInt(s)); line(); println(hexStringToInt(s2)); line(); println(hexDigitToInt('d')); line(); println(hexDigitToInt('9')); line(); println(hexDigitToInt('2')); printPascalString(s2); line(); int controlRegister = 128; controlRegister |= 64+32; controlRegister ^=16; controlRegister &= 128+64; controlRegister <<= 1; println(controlRegister); line(); println(intToHexChar(6)); line(); println(intToHexChar(10)); line(); printPascalString(intToDual(4106)); line(); printPascalString(intToHex(965)); line(); printPascalString(intToHex(1956)); line(); printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '0'})); line(); printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {4, '1', '1', '1', '0'})); line(); printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '1'})); line(); printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {4, '1', '0', '1', '1'})); return 0; }