int arr[5]; // sizeof(int) = 4, sizeof(short) = 2
arr[3] = 128; // 3 * 4 bytes each [ | | | ][ | | | ][ | | | ][A | | | ][ | | | ] write at 12th byte
// A = binary 10000000 = decimal 128
((short*)arr)[7] = 2; // 7 * 2 bytes each [ | | | ][ | | | ][ | | | ][ A | | B | ][ | | | ] write at 14th byte
// B = binary 00000010 = decimal 2
cout << arr[3] << endl; // It will read this as an integer: 00000000 00000010 00000000 10000000
// Output is 131200
I should also mention that this example was created on the matrix server and the machines are little endian meaning they store the least significant byte in the smallest address first (so the above would normally be read as [ | B | | A ]. Pretty cool stuff.
https://github.com/wongbsn/Codepad/blob/master/Bitwise%20Operation%20and%20Representation/data1.cpp
https://github.com/wongbsn/Codepad/blob/master/Bitwise%20Operation%20and%20Representation/data1.cpp
0 comments:
Post a Comment