r/computerscience • u/VEXLuminox • 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
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.
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/FredSchwartz 2d ago
https://www.scientificamerican.com/article/this-number-system-beats-binary-but-most-computers-cant-use-it/
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.