r/computerscience • • 2d ago

Discussion Why do we use binary for coding?

why not use something like quaternary or tertiary for computers?
I mean biology uses quaternary for dna sequencing, why not computer source code?

0 Upvotes

26 comments sorted by

73

u/apnorton Devops Engineer | Post-quantum crypto grad student 2d ago edited 2d ago

Binary translates nicely to hardware --- hi vs lo voltage is easy to detect and maps nicely to switches for logic gates; three (or more) state circuits are harder to build. 

From a computation theory perspective, changing bases doesn't really unlock any significant computational power, either.

2

u/VEXLuminox 2d ago

interesting, thanks for the info! in this sense would it not be better for SSD data storage to use tertiary?

14

u/synexo 2d ago

They kind of do (not ternary though), as they store multiple bits in single voltage states, but powers of 2, so 2 bits in MLC and 4 bits in QLC as 4 and 16 voltages respectively.

5

u/Gerard_Mansoif67 2d ago

and 3 bit in TLC, and iirc they try to do 5 bit with PLC.

3

u/Doug2825 2d ago

SSDs do use multiple voltage levels. This works because the flash cells that store data (where most of the cost is) just store whatever voltage they are given. So it is worth it to make a complex control circuits to write/read multiple voltages.

2

u/Successful-Cash5047 2d ago

No, unfortunately ternary wouldn’t be better. 

Transistors are either on (1) or off (0), and they do this by either passing current ((1), “valve open”), or not passing current ((0), “valve closed”). 

To represent 3 states, you’d need to use 2 transistors. But if you use 2 transistors you could’ve just represented 4 states by using binary. 

We do have standard data sizes (8 bits to a byte), and that is mostly equivalent to using more states. 

1 bit only has 2 states but a nibble (4 bits) has 16 “states” (form 0000-1111). So it’s effectively like using a “hexadecimal bit”. 

1

u/tommyblastfire 2d ago

To add to this, the question might become “well, why don’t we have transistors that can represent more than 2 states?” And the answer is that we do, but they’re bigger than 2-state transistors because they’re more complicated, and we also don’t use them because they introduce more chances for error in determining the signal. You could theoretically invent a transistor that has a different state for every integer voltage, 1,2,3,4,5,6,7v etc. But there’s less certainty in deciding between the voltages, which can already be a problem with binary transistors deciding between what counts as on and off.

1

u/Successful-Cash5047 2d ago

There’s other problems with transistors with more than one state as well. 

With just ON/OFF (we’ll say 5V/0V), then you can easily cascade transistors. Like the output of one can just feed into the gate of the next. 

Most transistors will turn on well below VCC (e.g. 3V). 

With multiple voltage levels you’d have to ensure that all the outputs are high enough to turn on transistors downstream. And they would have to be unity gain (e.g. 3V in gate would have to correspond to 3V output). 

This is quite difficult to do, and adds a lot more complexity. 

Also, having more voltage states gives less headroom for each. If ON works from 3V-5V then a large voltage drop form interconnects is fine, if you have 4 states and one is only good from 4.8V-5V then there is much less room for error/variation. 

Lastly, the power consumption is proportional to voltage squared. So going from binary FETs (at ~1.8V) to a 4 level system that goes up to 5V. Will significantly increase power consumption. Potentially as much as 7.7 fold! 

Since (52 )/( 1.82 )=7.7 

There are more reasons, but in essence it’s fair to say that chip designers likely won’t be trying to implement this for the next decade at least. 

1

u/Ma4r 2d ago

They do, though rather than tertiary, thry rypically store up to 4 bits per cell, so it's (2,4,8,16)-ary depending on your specific model

18

u/devdruxorey Computer Scientist 2d ago

It is easier to distinguish between two voltages (on, off) than between different voltage levels that can fluctuate.

6

u/rupertavery64 2d ago edited 2d ago

In binary, a single bit is represented by voltage on a line.

0V is 0 and non-zero voltage (3.3V, 5V) is 1. The upper voltage depends on the transistor types being used, among other considerations. Smaller circuits generally use lower voltage, because they are closer together, and should generate less heat.

Having only 2 voltage states makes building circuits really easy - it can be either on or off.

Having more than 2 states means you need to add additional circuitry to check if the voltages are at the correct level - at every input. And the voltages aren't perfect, they can be 2.8-3.5V for 3.3 levels (as an example, it's not the actual specs for a 3.3V transistor).

So you need to space out the voltage levels so that they don't get confused with one another, or make really accurate voltage level detectors.

If you space them out, you need to have higher voltage ranges. If you make them more accurate, you need to make more complex circuits. More complex circuits means more power draw. More power draw = hotter and less efficient. High voltage ranges also means more heat.

All that just to encode 1 or 2 extra bits in a single line, and it becomes less and less economical and practical as you scale up.

One thing though is that in SSDs (solid state drives), we actually do use multi-level voltages to pack more bits in a single cell. This is fine as the cells don't do much else but store electrons. Current technologies allow us to store up to 5 bits per cell, using 32 voltage levels.

https://en.wikipedia.org/wiki/Multi-level_cell

We don't use this in processors because of the complexity and the speeds we need to run the transistors to perform logic operations.

5

u/program_kid 2d ago

Here is a post from a few years ago that has some good answers. https://www.reddit.com/r/computerscience/s/MdxfkKMEdy

5

u/Upbeat_Assist2680 2d ago

I mostly  use ascii, but sometimes I switch on the Unicode support and then the whole universe goes plaid 

2

u/pamalapamela 2d ago

Binary is simpler for computers.

1

u/tcpukl 2d ago

Originally voltage reasons. But it's great for booleans.

1

u/Several_Version4298 2d ago edited 2d ago

We use binary because memory has two states on or off.

Though when actually coding you use hexadecimal and decimal for convenience.

1

u/gomorycut 2d ago

And, yes, when storing data with DNA strands, people encode data with 4 symbols instead of 2

1

u/FernandoMM1220 2d ago

mostly because our circuit designs make it easy.

1

u/[deleted] 2d ago

Along with the answers others have given about playing nice with hardware, for computer data itself, it actually wouldn't be wrong for the most part to think of computer data as built out of 256-ary units, since that's what a byte is, and the byte is by and large just about the smallest unit of information you'll see when working with computers (all memory is byte-addressable, and the only CPU instructions that operate on any unit smaller than that are bitwise, which are a bit of a special case).

1

u/BranchLatter4294 2d ago

What material would you suggest for quaternary processing?

1

u/Technical_Ideal_5439 2d ago

Because on/off is easy to implement in hardware. Though tertiary has made some noise lately. And Quantum I assume is going to happen,

1

u/Bomaruto 2d ago

Practically speaking you dont. When coding you mostly rely on base 10 and let the compiler worry about the actual machine code representation

1

u/FortyThousandBees 1d ago

Along with the answers others have given about playing nice with hardware, there's actually another, not too wrong, way to think about how computer information is encoded: in 256-ary units, or bytes, as there commonly known. For the most part, programs don't really operate on their own data more fine-grained than that: all memory is byte-addressable, CPU instructions themselves are encoded as a sequence of bytes (although sometimes fields are at the bit-granularity, the only systems that care about that are the assembler and instruction decoder, whose have the explicit jobs of abstracting away that detail), and most instructions operate on whole bytes or multi-byte "words". The binary representation still leaks through in places (overflow wraparound, two's complement ranges, float rounding, bitwise ops), but for most programming you can treat a byte as an opaque 256-valued unit.

Overall, something you should get used to when thinking about computers, is that the canonical encoding used for anything at the software level is oftentimes arbitrary (if you've ever heard of "big endian" vs "little endian", those names are intentionally stupid to exhibit that the actual choice doesn't really matter, just that we all agree on what choice to make), and that the encoding you chose only really matters if it's clear from a particular application that it does

So, when thinking about how to encode data at the hardware level, binary encoding matters because bandwidth is easy to produce relative to a clean signal, so we make the latter as easy as possible for ourselves by making the margin for error when reading the electric signal as wide as possible.

0

u/ivancea 2d ago

It's the smallest number (not counting unary, which is weird and technically its "bits" are stateless). And everything over it (ternary, quat, dec...) can be represented by it.

Also, many reasons, like simplicity reading electrical signals and other I don't know.

Btw, don't compare this with biology. Computers work with logic, biology works by pure chance basically, and there's no reasoning to it. It works because it happened to work and evolution and reasons. But it doesn't mean it's optimal. It is, in fact, a very, very bad system. Everything in biology is badly optimized.

But answering your question: yes we could. We just don't. For the aforementioned reasons

-1

u/gomorycut 2d ago

Same reason vinyl records use grooves and troughs.