Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean true - positive 1 or negative 1?

I'm designing a language, and trying to decide whether true should be 0x01 or 0xFF. Obviously, all non-zero values will be converted to true, but I'm trying to decide on the exact internal representation.

What are the pros and cons for each choice?

like image 971
zildjohn01 Avatar asked Apr 07 '09 19:04

zildjohn01


1 Answers

0 is false because the processor has a flag that is set when a register is set to zero.

No other flags are set on any other value (0x01, 0xff, etc) - but the zero flag is set to false when there's a non-zero value in the register.

So the answers here advocating defining 0 as false and anything else as true are correct.

If you want to "define" a default value for true, then 0x01 is better than most:

  • It represents the same number in every bit length and signedness
  • It only requires testing one bit if you want to know whether it's true, should the zero flag be unavailable, or costly to use
  • No need to worry about sign extension during conversion to other types
  • Logical and arithmetic expressions act the same on it

-Adam

like image 103
Adam Davis Avatar answered Sep 27 '22 16:09

Adam Davis