reverse Endianness Algorithm
In computing, endianness is the ordering or sequencing of bytes of a word of digital data in computer memory storage or during transmission. In its narrow sense, endianness refers to the ordering of the bytes (or digits) in an (atomic and contiguous) multi-byte storage object which is conceptually accessed as a whole by appropriate computer hardware instructions. The Intel 8051, contrary to other Intel CPUs, expects 16-bit addresses for LJMP and LCALL in large-endian format; however, xCALL instructions store the return address onto the stack in small-endian format. The DEC Alpha, Atmel AVR, VAX, the MOS technology 6502 family (including Western design center 65802 and 65C816), the Zilog Z80 (including Z180 and eZ80), the Altera Nios II, and many other CPUs and CPU family are also small-endian. The Motorola 6800 / 6801, the 6809 and the 68000 series of CPUs used the large-endian format.
#include <iostream>
#include <cstdio>
enum endianess {
LITTLE_ENDIAN_MACHINE = 0,
BIG_ENDIAN_MACHINE
};
endianess determine_endianess()
{
unsigned int num = 1;
char * c = (char *) #
if (*c == 1) {
return LITTLE_ENDIAN_MACHINE;
} else {
return BIG_ENDIAN_MACHINE;
}
}
void printBytes( char * start, int size)
{
for ( int i = 0; i < size; ++i ) {
printf("%.2x ", start[i] );
}
std::cout << std::endl;
}
int reverseEndianNess( int num )
{
int byte1, byte2, byte3, byte4;
byte1 = (num & 0x000000FF) >> 0;
byte2 = (num & 0x0000FF00) >> 8;
byte3 = (num & 0x00FF0000) >> 16;
byte4 = (num & 0xFF000000) >> 24;
return ((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4 << 0));
}
int main() {
endianess sys_endianess = determine_endianess();
if (sys_endianess == LITTLE_ENDIAN_MACHINE) {
std::cout << "System is little endian\n\n";
} else {
std::cout << "System is big endian\n\n";
}
int num = 0x01234567;
std::cout << "Num in decimal: " << num << std::endl;
std::ios::fmtflags f(std::cout.flags());
std::cout << "Num in hexadecimal:" << std::hex << num << std::endl;
std::cout.flags( f );
std::cout << "Printing individual bytes:\n";
printBytes((char*)&num, sizeof(num));
std::cout << std::endl;
std::cout << "Num in reversed endianness:\n";
int num1 = reverseEndianNess(num);
std::cout << "Num in decimal :" << num1 << std::endl;
f = std::cout.flags();
std::cout << "Num in hexadecimal:" << std::hex << num1 << std::endl;
std::cout.flags( f );
std::cout << "Printing individual bytes:\n";
printBytes((char*)&num1, sizeof(num1));
return 0;
}