Alex Rivera | Logout

Why do most languages not allow binary numbers?

Asked 2009-01-27T15:04:25.303
17

Why do most computer programming languages not allow binary numbers to be used like decimal or hexadecimal?

  • In VB.NET you could write a hexadecimal number like &H4
  • In C you could write a hexadecimal number like 0x04

Why not allow binary numbers?

  • &B010101
  • 0y1010

Bonus Points!... What languages do allow binary numbers?


Edit

Wow! - So the majority think it's because of brevity and poor old "waves" thinks it's due to the technical aspects of the binary representation.

Edit
Report

2 Answers

8

In order for a bit representation to be meaningful, you need to know how to interpret it. You would need to specify what the type of binary number you're using (signed/unsigned, twos-compliment, ones-compliment, signed-magnitude).

The only languages I've ever used that properly support binary numbers are hardware description languages (Verilog, VHDL, and the like). They all have strict (and often confusing) definitions of how numbers entered in binary are treated.

answered 2009-01-27T19:40:00.333
0

Although it's not direct, most languages can also parse a string. Java can convert "10101000" into an int with a method.

Not that this is efficient or anything... Just saying it's there. If it were done in a static initialization block, it might even be done at compile time depending on the compiler.

If you're any good at binary, even with a short number it's pretty straight forward to see 0x3c as 4 ones followed by 2 zeros, whereas even that short a number in binary would be 0b111100 which might make your eyes hurt before you were certain of the number of ones.

0xff9f is exactly 4+4+1 ones, 2 zeros and 5 ones (on sight the bitmask is obvious). Trying to count out 0b1111111110011111 is much more irritating.

I think the issue may be that language designers are always heavily invested in hex/octal/binary/whatever and just think this way. If you are less experienced, I can totally see how these conversions wouldn't be as obvious.

Hey, that reminds me of something I came up with while thinking about base conversions. A sequence--I didn't think anyone could figure out the "Next Number", but one guy actually did, so it is solvable. Give it a try:

10
11
12
13
14
15
16
21
23
31
111
?

Edit: By the way, this sequence can be created by feeding sequential numbers into single built-in function in most languages (Java for sure).

answered 2009-08-08T01:11:48.370

Your Answer