From 6fd46e4d319dea1749279cbb8ff97af1ad68022b Mon Sep 17 00:00:00 2001 From: KleptsovKD Date: Tue, 4 Feb 2025 22:53:26 +0300 Subject: [PATCH] bit digit and print in binary --- lab04/main.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lab04/main.cpp b/lab04/main.cpp index 048e98a..2863d8b 100644 --- a/lab04/main.cpp +++ b/lab04/main.cpp @@ -43,6 +43,21 @@ const uint8_t* as_bytes(const void* data) } +char +bit_digit(uint8_t byte, uint8_t bit) { + if (byte & (0x1 << bit)) { + return '1'; + } + return '0'; +} + +void +print_in_binary(uint8_t byte) { + for (int bit = 7; bit >= 0; bit--) { + cout << bit_digit(byte, bit); + } +} + int main() { @@ -82,10 +97,14 @@ int main() cout << '\n'; - //print_in_hex(byte); + cout << "0x" << hex << static_cast(byte) << " hex: "; + print_in_hex(&byte, sizeof(byte)); + cout << "0x" << hex << static_cast(byte) << " bin: "; + print_in_binary(byte); + cout << "\n\n"; cout << "Press any key for exit... "; - cin.get(); + cin.ignore(); return 0; }