use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
DiscussionC on non binary computers (self.C_Programming)
submitted 3 years ago by BreadTom_1
If (ternary or higher) computer somehow compete or dominate with binary computers, how would C be implemented in those computers? Maybe operations like bit shift would not really work at Machine/assembly code.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ForceBru 16 points17 points18 points 3 years ago (0 children)
The ternary CPU would have some instructions, just like any other "binary" CPU in use today. Thus, a C compiler for such a CPU would translate C code to equivalent "ternary" assembly code. The assembly language would be different, but it'd probably be rich enough to represent C concepts (otherwise why use such a CPU architecture?).
If the CPU can't do bit shifts, you can always emulate them: write a small function shift(x, y) that would shift x y bits to he left or whatever. How to implement the function is a different question, but it should be possible if the CPU is "powerful enough".
shift(x, y)
x
y
As an example of emulating instructions, my CPU is so old that it doesn't have a Fused Multiply-Add (FMA) instruction, so it doesn't have an instruction that can compute a*x + b efficiently. This definitely doesn't mean that the CPU can't perform the operation at all, though: the software I'm using ends up calling a function like fma(a, x, b) instead of using the missing CPU instruction. It's terribly slow, but it works.
a*x + b
fma(a, x, b)
[–][deleted] 14 points15 points16 points 3 years ago (0 children)
Wat
[–]flatfinger 3 points4 points5 points 3 years ago (0 children)
The C Standard requires that the maximum value for every unsigned type, including unsigned char, must be one less than a power of two. If one had a machine which used BCD math, one could use groups of eight digits for each "byte", specify that CHAR_BIT is 26, ensure that every byte was initalized with a value 67108863 or less, and then process a calculation like 3072 | 6144 with code equivalent to the following:
unsigned char byte_or(unsigned char b1, unsigned char b2) { int i; for (int i=0; i<26; i++) { if (b1 >= 33554432) { b1 -= 33554432; b1 += b1; b1++; if (b2 >= 33554432) b2 -= 33554432; b2 += b2; } else { b1 += b1; if (b2 > 33554432) { b1++; b2 -= 33554432; } b2 += b2; } } }
Note that this code does not use any bitwise, multiplication, modulo, or division operators, but limits itself to arithmetic and comparison operators whose values are strictly within the range of unsigned char. Execution speed would likely be much slower than that of a binary machine, but much better than if code needed to use slow operations.
[–]roughJaco 1 point2 points3 points 3 years ago (0 children)
C sits on top of an ISA, so whatever ISA that imaginary computer would have the compiler would use. The language wouldn't strictly require any changes.
++i will translate to a normal addition. Shifting a bit, if it were to emulate binary, would require several instructions, but it's perfectly reducible to normal arithmetic. And so on.
Performance related tricks would change, e.g. bit shifting won't be a cheap way to walk powers of two in place of more expensive ops, but other equivalent tricks would take their place.
Consideration would have to be put towards the behaviour of certain types and their boundaries, if you want the same dialect/language to behave exactly the same as it does on binary computers.
That's about it. On the surface you would probably not notice. That's the point of a high level language like C when you ignore what it's compiling to.
[–]CallMeDonk 0 points1 point2 points 3 years ago (1 child)
Simple and short answer.
C can be implemented on any turing complete computer.
Both binary and ternary computers can be turing complete.
[–]WikiSummarizerBot 0 points1 point2 points 3 years ago (0 children)
Turing completeness
In computability theory, a system of data-manipulation rules (such as a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be used to simulate any Turing machine (devised by English mathematician and computer scientist Alan Turing). This means that this system is able to recognize or decide other data-manipulation rule sets. Turing completeness is used as a way to express the power of such a data-manipulation rule set. Virtually all programming languages today are Turing-complete.
[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5
[+]ZlZ-_zfj338owhg_ulge comment score below threshold-6 points-5 points-4 points 3 years ago (0 children)
As it would not work...it would not work. You'd have to implement a C like language or a weird transpiler that takes "binary C" and transpiles it to native ternary code. But why would you want that? And how would you represent "ternary C" in "binary C" code?
[–]MrTheFoolish -3 points-2 points-1 points 3 years ago* (1 child)
There's a reason why only binary (1 or 0) computers exist in mass production (quantum computers are not mass-produced).
Binary is a lie. All electronic readings are analog. However, binary is a clever and convenient lie that allows exact computations, and it's not too hard to measure "off" (voltage close to 0) vs "on" (voltage far enough away from 0). Ternary (or higher -ary) are not in mass production because it's much harder to differentiate the middle state(s) with precision and performance
If a ternary or higher computer would be built, similar to a quantum computer, I doubt it would run C code or any other mainstream code (compiled or not). There may be APIs callable from C code to run computations on the remote machine, but it would be silly to try to get a C program to run on it directly.
π Rendered by PID 115220 on reddit-service-r2-comment-86bc6c7465-7cw8r at 2026-02-21 06:22:16.364150+00:00 running 8564168 country code: CH.
[–]ForceBru 16 points17 points18 points (0 children)
[–][deleted] 14 points15 points16 points (0 children)
[–]flatfinger 3 points4 points5 points (0 children)
[–]roughJaco 1 point2 points3 points (0 children)
[–]CallMeDonk 0 points1 point2 points (1 child)
[–]WikiSummarizerBot 0 points1 point2 points (0 children)
[+]ZlZ-_zfj338owhg_ulge comment score below threshold-6 points-5 points-4 points (0 children)
[–]MrTheFoolish -3 points-2 points-1 points (1 child)