I have the following code for self learning:
#include <iostream>
using namespace std;
struct bitfields{
unsigned field1: 3;
unsigned field2: 4;
unsigned int k: 4;
};
int main(){
bitfields field;
field.field1=8;
field.field2=1e7;
field.k=18;
cout<<field.k<<endl;
cout<<field.field1<<endl;
cout<<field.field2<<endl;
return 0;
}
I know that unsigned int k:4
means that k is 4 bits wide, or a maximum value of 15, and the result is the following.
2
0
1
For example, filed1
can be from 0 to 7 (included), field2
and k
from 0 to 15. Why such a result? Maybe it should be all zero?