Posts

Showing posts with the label bits

C++: How to see a byte's bits

Spanish version / Versión en Español If you ever go down to the dungeons to slay a draconic bug, this little function, adapted from the one written by Bruce Eckel (see Reference), may be a helpful torch. string printBinary(const unsigned char val) { string binaryString=""; for(int i = 7; i >= 0; i--){ if( val & (1 << i) ){ binaryString += "1"; }else{ binaryString += "0"; } } return binaryString; } Reference: Thinking in C++, Vol One, Chapter 3: The C in C++, section: "Shift operators"

C++: Cómo ver los bits de un byte

Versión en inglés / English version Si algún día tenemos necesidad de bajar a las mazmorras para matar un bug dracónico, esta funcioncita, adaptada de la que escribió Bruce Eckel para "Thinking in C++" (ver referencia), puede servir de antorcha: string printBinary(const unsigned char val) { string binaryString=""; for(int i = 7; i >= 0; i--){ if( val & (1 << i) ){ binaryString += "1"; }else{ binaryString += "0"; } } return binaryString; } Referencia: Thinking in C++, Vol One, Chapter 3: The C in C++, section: "Shift operators"