I’m confused by the following code:
#include <iostream>
using namespace std;
struct bit
{
int a:3;
int b:2;
int c:3;
};
int main(int argc, char* argv[])
{
bit s;
char *c = (char*)&s;
*c = 0x99;
cout << s.a <<endl <<s.b<<endl<<s.c<<endl;
return 0;
}
Since a takes 3 bits , b 2 bits , c 3 bits , when i cast this struct to a char* pointer , did i took the first 8 bytes or the last 8 bytes ?
-
On a Intel 32 bit machine , how will the compiler store an 32 bit integer ?
-
Why i get
1 , -1 , -4as a result ?
0x99 is 10011001 binary is 100 11 001, which looks very much like 1,-1,-4 (in reverse order). And yes, it’s 8 least significant bits.
I believe signedness has confused you, so you may want to use
unsigned intin the struct. If it’s not signedness, then please, be more specific.