Question about binary from lost computer science student

  • With 1 bit, you can represent two integers: 0,1
  • With 2 bits, you can represent four integers, 0, 1, 2, 3
  • With 3 bits, you can represent eights integers, 0, 1, 2, 3, 4, 5, 6, 7
  • With 4 bits, you can represent sixteen integers, 0, 1, ..., 15
  • With 5 bits, you can represent thirty integers, 0, 1, ..., 31
  • See the trend?
  • With n bits, you can represent 2n integers, from 0 to (2n)-1

  • To write a number in binary, you must use powers of 2.

  • Examples:

  • Q: How can I represent 100? A: 64+32+3+1

  • Q: How can I represent 37? A: 32+4+1

  • Q: How can I represent 24? A: 16+8

  • Q: How can I represent 26? A: 16+8+2

  • Q: How can I represent 200? A: 128+64+8 **

  • Now how do I convert those powers into binary form?

  • Easy.

  • You can see that powers of 2 is the following series:

  • 1+2+4+8+16+32+64+128+256+...

  • Now to represent your 200 countries, you need 8, 64 and 128.

  • Q: What exponent do I need to give to 2 to get 8? A: 3

  • Q: What exponent do I need to give to 2 to get 64? A: 6

  • Q: What exponent do I need to give to 2 to get 128? A: 7

  • What we just calculated that the positions of 8, 64 and 128 are 3, 6, and 7 respectively.

  • Now we need to form a binary numbers, knowing that when forming binary numbers, position ZERO is the rightmost side of the number

  • Like this:

  • [p7][p6][p5][p4][p3][p2][p1][p0]

  • So, we need position 3 6 and 7. Let's do it now. Let's put 1 in each of these positions, and fill the rest with zeroes.

  • [p7][p6][p5][p4][p3][p2][p1][p0]

  • [1][1][0][0][1][0][0][0]

  • Then we verify:

  • 0x20+0x21+0x22+1x23+0x24+0x25+1x26+1x27=200

  • Yay! Thus, to answer your answer, recall that with n bits you can represent integers from 0 to 2n-1. 2n is a power of 2 so you need the smallest power of 2 greater or equal to 200. The smallest is 256, which is 28, which can represent 0 to 255 ((28)-1=255

/r/compsci Thread