Skip to content

Instantly share code, notes, and snippets.

@gjb2048
Created September 7, 2026 11:23
Show Gist options
  • Select an option

  • Save gjb2048/3692bf644b442b076ad0df920a8ab137 to your computer and use it in GitHub Desktop.

Select an option

Save gjb2048/3692bf644b442b076ad0df920a8ab137 to your computer and use it in GitHub Desktop.
Demonstration of endianness using time.
/**
* @file time.cpp
* @copyright (c) 2026 G J Barnard.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @par License
* SPDX-License-Identifier: BSD-3-Clause - https://spdx.org/licenses/BSD-3-Clause.html.
*
* @version V1.0.0
*/
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
// Byte swapping functions, adapted from: https://github.com/krxecs/byteswap-c.
// License: 0-clause BSD License (SPDX license code: `0BSD`): https://spdx.org/licenses/0BSD.html.
/**
* @brief Swap the byte order of a 32-bit unsigned integer.
* @param num The 32-bit unsigned integer to swap.
* @return The byte-swapped 32-bit unsigned integer.
*/
uint32_t bswap32(uint32_t num) {
int nums = sizeof(num);
vector<uint8_t> numbytesp(reinterpret_cast<uint8_t*>(&num), reinterpret_cast<uint8_t*>(&num) + nums);
cout << "bswap32 parameter dec value: " << dec << num << endl;
cout << "bswap32 parameter hex value: " << hex << setw(8) << setfill('0') << num << endl;
cout << "bswap32 parameter in memory: ";
for (uint8_t i = 0; i < nums; i++) {
cout << hex << setw(2) << setfill('0') << (int) numbytesp.at(i);
}
cout << endl;
// Note: Changed flipping of middle pair to be same as bswap64.
uint32_t bshifted[4];
bshifted[0] = (num >> 24);
bshifted[1] = (num >> 8);
bshifted[2] = (num << 8);
bshifted[3] = (num << 24);
for (uint8_t i = 0; i < 4; i++) {
cout << "byte " << dec << (int) i << " shifted: " << hex << setw(8) << setfill('0') << bshifted[i] << endl;
}
uint32_t banded[4];
banded[0] = bshifted[0] & 0xff;
banded[1] = bshifted[1] & 0xff00;
banded[2] = bshifted[2] & 0xff0000;
banded[3] = bshifted[3] & 0xff000000;
for (uint8_t i = 0; i < 4; i++) {
cout << "byte " << dec << (int) i << " bitwise anded: " << hex << setw(8) << setfill('0') << banded[i] << endl;
}
uint32_t result = banded[0] | banded[1] | banded[2] | banded[3];
/*
return ((num >> 24) & 0xff) | // move byte 3 to byte 0
((num << 8) & 0xff0000) | // move byte 1 to byte 2
((num >> 8) & 0xff00) | // move byte 2 to byte 1
((num << 24) & 0xff000000); // byte 0 to byte 3
*/
vector<uint8_t> numbytesr(reinterpret_cast<uint8_t*>(&result), reinterpret_cast<uint8_t*>(&result) + nums);
cout << "bswap32 all bytes swapped result dec value: " << dec << result << endl;
cout << "bswap32 all bytes swapped result hex value: " << hex << setw(8) << setfill('0') << result << endl;
cout << "bswap32 all bytes swapped result in memory: ";
for (uint8_t i = 0; i < nums; i++) {
cout << hex << setw(2) << setfill('0') << (int) numbytesr.at(i);
}
cout << endl;
return result;
}
/**
* @brief Swap the byte order of a 64-bit unsigned integer.
* @param x The 64-bit unsigned integer to swap.
* @return The byte-swapped 64-bit unsigned integer.
*/
uint64_t bswap64(uint64_t num) {
int nums = sizeof(num);
vector<uint8_t> numbytesp(reinterpret_cast<uint8_t*>(&num), reinterpret_cast<uint8_t*>(&num) + nums);
cout << "bswap64 parameter dec value: " << dec << num << endl;
cout << "bswap64 parameter hex value: " << hex << setw(16) << setfill('0') << num << endl;
cout << "bswap64 parameter in memory: ";
for (uint8_t i = 0; i < nums; i++) {
cout << hex << setw(2) << setfill('0') << (int) numbytesp.at(i);
}
cout << endl;
uint64_t bshifted[8];
bshifted[0] = num >> 56;
bshifted[1] = num >> 40;
bshifted[2] = num >> 24;
bshifted[3] = num >> 8;
bshifted[4] = num << 8;
bshifted[5] = num << 24;
bshifted[6] = num << 40;
bshifted[7] = num << 56;
for (uint8_t i = 0; i < 8; i++) {
cout << "byte " << dec << (int) i << " shifted: " << hex << setw(16) << setfill('0') << bshifted[i] << endl;
}
uint64_t banded[8];
banded[0] = bshifted[0] & 0xff;
banded[1] = bshifted[1] & 0xff00;
banded[2] = bshifted[2] & 0xff0000;
banded[3] = bshifted[3] & 0xff000000;
banded[4] = bshifted[4] & 0xff00000000;
banded[5] = bshifted[5] & 0xff0000000000;
banded[6] = bshifted[6] & 0xff000000000000;
banded[7] = bshifted[7] & 0xff00000000000000;
for (uint8_t i = 0; i < 8; i++) {
cout << "byte " << dec << (int) i << " bitwise anded: " << hex << setw(16) << setfill('0') << banded[i] << endl;
}
uint64_t result =
banded[0] | banded[1] | banded[2] | banded[3] |
banded[4] | banded[5] | banded[6] | banded[7];
/*
return x >> 56 // move byte 7 to byte 0
| ((x >> 40) & 0xff00) // move byte 6 to byte 1
| ((x >> 24) & 0xff0000) // move byte 5 to byte 2
| ((x >> 8) & 0xff000000) // move byte 4 to byte 3
| ((x << 8) & 0xff00000000) // move byte 3 to byte 4
| ((x << 24) & 0xff0000000000) // move byte 2 to byte 5
| ((x << 40) & 0xff000000000000) // move byte 1 to byte 6
| x << 56; // move byte 0 to byte 7
*/
vector<uint8_t> numbytesr(reinterpret_cast<uint8_t*>(&result), reinterpret_cast<uint8_t*>(&result) + nums);
cout << "bswap64 all bytes swapped result dec value: " << dec << result << endl;
cout << "bswap64 all bytes swapped result hex value: " << hex << setw(16) << setfill('0') << result << endl;
cout << "bswap64 all bytes swapped result in memory: ";
for (uint8_t i = 0; i < nums; i++) {
cout << hex << setw(2) << setfill('0') << (int) numbytesr.at(i);
}
cout << endl;
return result;
}
/**
* Sending and receiving time over a conceptual communications channel.
*/
int main() {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
cout << "Little endian on: ";
#else
cout << "Big endian on: ";
#endif
#ifdef __linux__
cout << "Linux." << endl;
#elif _WIN32
cout << "Windows." << endl;
#else
cout << "Not Linux or Windows." << endl;
#endif
cout << "Current time is: ";
time_t timestamp;
time(&timestamp);
cout << ctime(&timestamp);
int ts = sizeof(timestamp);
cout << "timestamp decimal and size: " << timestamp << " : " << ts << endl;
vector<uint8_t> timestampbytes(reinterpret_cast<uint8_t*>(&timestamp), reinterpret_cast<uint8_t*>(&timestamp) + ts);
cout << "timestamp hex value: " << hex << timestamp << endl;
cout << "timestamp in memory: ";
for (uint8_t i = 0; i < ts; i++) {
cout << hex << setw(2) << setfill('0') << (int) timestampbytes.at(i);
}
cout << endl;
// Convert to network byte order for our endianness.
uint32_t tosend32 = timestamp;
uint64_t tosend64 = timestamp;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
tosend32 = bswap32(tosend32);
tosend64 = bswap64(tosend64);
#endif
int tss32 = sizeof(tosend32);
vector<uint8_t> tosend32bytes(reinterpret_cast<uint8_t*>(&tosend32), reinterpret_cast<uint8_t*>(&tosend32) + tss32);
cout << "Sending: " << endl << "32: ";
for (uint8_t i = 0; i < tss32; i++) {
cout << hex << setw(2) << setfill('0') << (int) tosend32bytes.at(i);
}
cout << endl << "64: ";
int tss64 = sizeof(tosend64);
vector<uint8_t> tosend64bytes(reinterpret_cast<uint8_t*>(&tosend64), reinterpret_cast<uint8_t*>(&tosend64) + tss64);
for (uint8_t i = 0; i < tss64; i++) {
cout << hex << setw(2) << setfill('0') << (int) tosend64bytes.at(i);
}
cout << endl;
// Convert back from network byte order to our endianness.
uint8_t *fromsend32n8ar = tosend32bytes.data();
uint8_t *fromsend64n8ar = tosend64bytes.data();
cout << "Received: " << endl << "32: ";
for (uint8_t i = 0; i < tss32; i++) {
cout << hex << setw(2) << setfill('0') << (int) fromsend32n8ar[i];
}
cout << endl << "64: ";
for (uint8_t i = 0; i < tss64; i++) {
cout << hex << setw(2) << setfill('0') << (int) fromsend64n8ar[i];
}
cout << endl;
uint32_t *fromsend32nptr = reinterpret_cast<uint32_t *>(fromsend32n8ar);
uint32_t fromsend32n = *fromsend32nptr;
uint64_t *fromsend64nptr = reinterpret_cast<uint64_t *>(fromsend64n8ar);
uint64_t fromsend64n = *fromsend64nptr;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint32_t fromsend32h = bswap32(fromsend32n);
uint64_t fromsend64h = bswap64(fromsend64n);
#endif
cout << "Received 32 time value: " << dec << fromsend32h << " : " << hex << fromsend32h << endl;
cout << "Received 64 time value: " << dec << fromsend64h << " : " << hex << fromsend64h << endl;
cout << "Received 32 time is: ";
uint64_t rect32t = fromsend32h;
time_t *r32time = reinterpret_cast<time_t*>(&rect32t);
cout << ctime(r32time);
cout << "Received 64 time is: ";
uint64_t rect64t = fromsend64h;
time_t *r64time = reinterpret_cast<time_t*>(&rect64t);
cout << ctime(r64time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment