I am trying to set bytes in an unsigned short variable individually, using memset:
#include <cstring>
#include <iostream>
int main()
{
unsigned short test2;
std::cout << "size of test2: " << sizeof(test2) << std::endl;
memset(&test2, 0, 2);
memset(&test2, 0x4D, 1);
memset(&test2+1, 0x42, 1);
std::cout << "value: " << test2 << std::endl;
return 0;
}
The output I get is:
size of test2: 2
value: 77
77 - 0x4D. Therefore, for some reason, it does not raise 0x42, which I am trying to set on the second byte of a variable. Why is this?
source
share