Beispiel #1
0
std::string toBinary(const unsigned char* c_ptr, const std::size_t &bytes)
{
    unsigned char bitPattern;
    std::string binaryString("");
    for (std::size_t byte(0); byte < bytes; ++ byte) {
        bitPattern = 0x80;    // 10000000 in binary
        for (unsigned int bit = 0; bit < 8; ++ bit) {
            if ((bitPattern & *(c_ptr + byte)) > 0)    // (c_ptr + byte) is the current address.
                binaryString += "1";
            else
                binaryString += "0";
            bitPattern >>= 1;
        }
    }
    return binaryString;
}
Beispiel #2
0
int main() {

  int x = 0xAA;
  int y = 0x56;

  printf("int x = 0x%X; // %s\n", x, binaryString(x)+24 );
  printf("int y = 0x%X; // %s\n\n", y, binaryString(y)+24 );

  printf("x & y = ");
  getc(stdin);
  printf("0x%X (%s)\n", x & y, binaryString(x & y)+24 );

  getc(stdin);
  printf("x && y = ");
  getc(stdin);
  printf("0x%X (%s)\n", x && y, binaryString(x && y)+24 );

  getc(stdin);
  printf("x | y = ");
  getc(stdin);
  printf("0x%X (%s)\n", x | y, binaryString(x | y)+24 );

  getc(stdin);
  printf("x ^ y = ");
  getc(stdin);
  printf("0x%X (%s)\n", x ^ y, binaryString(x ^ y)+24 );

  getc(stdin);
  printf("x || y = ");
  getc(stdin);
  printf("0x%X (%s)\n", x || y, binaryString(x || y)+24 );

  getc(stdin);
  printf("~x = ");
  getc(stdin);
  printf("0x%X (%s)\n", ~x, binaryString(~x)+24 );

  getc(stdin);
  printf("~y = ");
  getc(stdin);
  printf("0x%X (%s)\n", ~y, binaryString(~y)+24 );

  getc(stdin);
  printf("!x = ");
  getc(stdin);
  printf("0x%X (%s)\n", !x, binaryString(!x)+24 );

  getc(stdin);
  printf("!y = ");
  getc(stdin);
  printf("0x%X (%s)\n", !y, binaryString(!y)+24 );

  getc(stdin);
  printf("x << 2 = ");
  getc(stdin);
  printf("0x%X (%s)\n", x << 2, binaryString(x << 2)+24 );

  getc(stdin);
  printf("y << 2 = ");
  getc(stdin);
  printf("0x%X (%s)\n", y << 2, binaryString(y << 2)+24 );

  getc(stdin);
  printf("x >> 2 = ");
  getc(stdin);
  printf("0x%X (%s)\n", x >> 2, binaryString(x >> 2)+24 );

  getc(stdin);
  printf("y >> 2 = ");
  getc(stdin);
  printf("0x%X (%s)\n", y >> 2, binaryString(y >> 2)+24 );

}