Display each bit in a char C++ -



Display each bit in a char C++ -

i want display each bit in char. not work :

char flags = bytefromfile(); for( int = 0; < 8; i++ ){ int tmp = ( flags >> ) & 0x2; cout << tmp; } cout << endl;

what's wrong code?

i zeros byte has value of 3 ( looked debugger ).

there couple of problems in code.

you using & 0x2 instead of & 0x1. you printing to the lowest degree important bit first.

the next should prepare both.

#include <iostream> int main(int argc, char** argv) { unsigned char flags = argv[1][0]; (int = 7; >= 0; --i) { std::cout << ((flags >> i) & 0x1); } std::cout << '\n'; }

c++

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -