Math - Number Base Challenge

Decided to get my better half to pick a number, which turned out to be 162.

Binary of 162 = 10100010
Hexadecimal of 162 = A2

Got a question on this though. I have noticed that sometimes people include an additional 0 at the start of their binary, so my answer of 10100010 would be written as 010100010. Is it more correct to include that additional 0 just to show that there is nothing more to the number, or does it really not matter?

Great work @JaylisPlay.
Leading zeros aren’t required in binary and when you see them they’re being used to either;

  • show how much storage the number is using
  • have some encoding reason to be there

On the storage front - an 8-bits equals 1 byte, and 4-bits can be represented as a single digit of hexadecimal.
Therefore, it’s quite common to see binary broken down into 4-digit chunk. This also aids readability, since it’s much easier to parse 4-digits at a time.

For your example of 162 = 10100010 it’s already 8-digits, so there’s no need to add any leading zeros. However, you could add some spaces and write it as 1010 0010

If instead, you had 98 = 0110 0010 it may be beneficial to add that extra leading zero to show that it takes up 1 byte of space.

If you’re encoding the binary into something else then the leading zeros do become important, as they will hold some information even when they’re zero.

For instance, when you encode a 32-bit signed integer, the first digit represents the sign of the number (0 = positive and 1 = negative), so including all of the digits can become important.

As an example 162 would be 0 000 0000 1010 0010 and -162 would be 1 000 000 1010 0010 (note the extra gap between the first and second digit being added for additional readability)

I hope that helps to answer your question.

1 Like

That really helps @garypettie, thanks :slight_smile:

I had a vague recollection about the positive and negative with 32-bit signed integers, but it has been a long time since I was very active with my programming (and math to be honest :stuck_out_tongue:)

Actually very glad I got these courses, really clear explanations given, and no question seems to go unanswered :smiley:

1 Like