Is it possible in C ++ to output to the console the value of a bit field related to a particular structure? If so, how?
|
1 answer
Output as usual:
#include <iostream> struct Foo { unsigned bar : 3; unsigned baz : 6; }; int main() { Foo foo; foo.bar = 2; foo.baz = 4; std::cout << foo.bar << std::endl; std::cout << foo.baz << std::endl; return 0; } If necessary in binary form, then:
#include <iostream> #include <bitset> struct Foo { unsigned bar : 3; unsigned baz : 6; }; int main() { Foo foo; foo.bar = 2; foo.baz = 4; std::cout << std::bitset<3>(foo.bar) << std::endl; std::cout << std::bitset<6>(foo.baz) << std::endl; return 0; } |