World's most popular travel blog for travel bloggers.

[Solved]: Why is there no two-bit data type?

, , No Comments
Problem Detail: 

Why do most programming languages (if not all) lack a 2-bit data type?

A boolean is a single bit with the values being either true or false.

Why is there no native, corresponding type where the values can be either true, false, neither or both? This data type can of course be constructed inside the language, but I figure there would be lots of usages for a more fundamental language support for it.

This data type would be very handy in, for instance:

  • gene programming (values being: A, C, G or T)
  • GUI programming (a checkbox that is: not checkable, unchecked, checked, or both/unknown)
  • testing (test result can be: not run, pass, fail or inconclusive)
  • quantum programming (values being: true, false, neither or both)

Is there a scientific reason to why this data type (quad, qool, qit etc.) has been excluded?

Asked By : Reyhn

Answered By : torhu

Like Geier said, processors can't directly handle values smaller than a byte. So a boolean type that was actually implemented as just a single bit would in most cases be inefficient. The processor needs to do a bit of extra work to extract just that one bit.

The reason that the boolean type exists is more about semantics than anything else. Logical true/false values is a very useful concept. In many programming languages a conditional expression like "if x is 5" has a boolean value as the result. And lets not forget that a boolean type corresponds nicely to a single bit, which is the building block of the binary numbers that computers work with.

There has been tri-state "booleans" in use, and there probably still is today. But they are generally less useful and more complicated to deal with than the ordinary true/false ones.

What you are talking about would generally be implemented as bit flags (or bit fields), or simply by assigning different meanings to different numbers.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/10722

0 comments:

Post a Comment

Let us know your responses and feedback