From e73fb5fb540109e6aeb099428e3eaf5555881112 Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Sat, 7 Jan 2023 21:44:26 -0600 Subject: [PATCH] beginning of endianness discussion --- section_3/endian/.vscode/settings.json | 6 ++++ section_3/endian/main.cpp | 38 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 section_3/endian/.vscode/settings.json create mode 100644 section_3/endian/main.cpp diff --git a/section_3/endian/.vscode/settings.json b/section_3/endian/.vscode/settings.json new file mode 100644 index 0000000..2d5aa6c --- /dev/null +++ b/section_3/endian/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "ostream": "cpp", + "iomanip": "cpp" + } +} \ No newline at end of file diff --git a/section_3/endian/main.cpp b/section_3/endian/main.cpp new file mode 100644 index 0000000..4fa1cce --- /dev/null +++ b/section_3/endian/main.cpp @@ -0,0 +1,38 @@ +/* Determine Endianness + Perry Kivolowitz + Carthage College +*/ + +#include +#include +#include +#include +#include + +using namespace std; + +template +string Dump(T & i) { + stringstream ss; + unsigned char * p = reinterpret_cast(&i); + + ss << hex << setfill('0'); + for (uint32_t counter = 0; counter < sizeof(T); counter++) { + ss << setw(2) << static_cast(*(p++)); + } + return ss.str(); +} + +int main() { + int16_t i16 = 0x0123; + int32_t i32 = 0x01234567; + int64_t i64 = 0x0123456789ABCDEF; + + cout << hex << setfill('0'); + cout << "Endianness of this computer:\n"; + cout << "i16: " << setw(16) << Dump(i16) << " value: " << setw(16) << i16 << endl; + cout << "i32: " << setw(16) << Dump(i32) << " value: " << setw(16) << i32 << endl; + cout << "i64: " << setw(16) << Dump(i64) << " value: " << setw(16) << i64 << endl; + cout << "If little endian, column 1 will not equal column 2.\n"; + return 0; +}