Guide: Binary and Hexadecimal Numbers


Everyone is familiar with decimal numbers. However, computers use binary and hexadecimal representations of numbers. We also need to learn how to deal with bits, and bitwise operators.

Do you know the answers to the following questions?

  • What is the decimal value of 0b1000?
  • What is 255 in hexadecimal?
  • What is 0xA in binary?
  • What is the minimum number of bits required to store the number 8?
  • What does 0b01 + 0b10 equal?
  • The six bitwise operators are & (and), | (or), ^ (xor), ~ (invert), << >> (left and right shift). Do you know how each operates? What is the result of evaluating the following expressions:
    • 0x01 & 0x02
    • 0x01 | 0x2
    • 0x01 ^ 0x02
    • 0x01 << 2
  • Given a number x, what expression computes a new number y that is equal to x but with a zero in its least significant bit?

Try your hand at these exercises on base conversion and bitwise operations used for practice in the first lab of the regular CS107.

If you don't know the answers to these questions, we recommend that you review the following materials:


Credit to: http://cowbirdsinlove.com/43 all your base