Questions tagged [ternary]

The base-3 positional numeral system which represents numbers using the digits 0, 1, and 2

Ternary is the base-3 positional numeral system which represents numbers using the digits 0, 1, and 2. In computing it is less common than , but there have been ternary computers, and it can be used to represent sequences of ternary (3-way) choices.

A unit of information in the ternary numeral system is sometimes called a trit (by analogy with for binary).

Number systems:

       Name         |   Base  |   numbers
------------------------------------------                 
ternary   - system  |     2   |    0... 2
decimal   - system  |    10   |    0... 9

ternary    |    decimal      
-----------------------
         0 |        0        
         1 |        1         
         2 |        2       
        10 |        3        
        11 |        4      
        12 |        5       
        20 |        6      
        21 |        7       
        22 |        8       
        30 |        9      
       101 |       10       
       121 |       16      
      1012 |       32       
      2101 |       64       
     11202 |      128     
    100110 |      255      

Decimal -> Ternary

Number: 42

42 / 3 = 14 Rest 0    ^
14 / 3 =  4 Rest 2    |
 4 / 3 =  1 Rest 1    |
 1 / 3 =  0 Rest 1    |  => 42 = 1120
                                 ----->

Ternary -> Decimal

Number: 1120

1  1  2  0   
|  |  |   -------- 0 x 3^0  ->   0
|  |   ----------- 2 x 3^1  ->   6
|   -------------- 1 x 3^2  ->   9
 ----------------- 1 x 3^3  ->   27
                                    = 42
643 questions
10
votes
4 answers

Using statements on either side of a python ternary conditional

Why is it prohibited to use statements on either side of python's ternary conditional? I can't see any obvious reason how a few of the following naive syntax examples could be ambiguous or broken - but I'm sure there must be a good reason why it's…
wim
  • 338,267
  • 99
  • 616
  • 750
9
votes
0 answers

Testing which trits are set in a binary representation

I have a problem for which I have eight elements that can contain 0, 1, or 2. I can easily represent this in 16 bits, but for SIMD efficiency reasons, I need it to occupy 13 bits (it is not the only thing present in the lane). Fortunately,…
Octavianus
  • 421
  • 4
  • 12
9
votes
2 answers

using PHP's null coalescing operator on an array

I am using PHP's null coalescing operator described by http://php.net/manual/en/migration70.new-features.php. Null coalescing operator ¶ The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a…
user1032531
  • 24,767
  • 68
  • 217
  • 387
8
votes
2 answers

XSLT 1.0 Idiom for ternary if?

This Java program makes use of a Ternary if, to map booleans to output strings: (a "*" for true, an empty string for false). public class ternary { public static void main(String[] args) { boolean flags[]={true,false,true}; for (boolean f…
monojohnny
  • 5,894
  • 16
  • 59
  • 83
8
votes
3 answers

What means the operator "??" in Dart/Flutter?

I've seen this code and need an explanation for "??". I know ternary operators like "?" and then the true-condition and after ":" the false/else condition. But what means the double "??" ? Thanks in advance widget.secondaryImageTop ?? …
cyberpVnk
  • 109
  • 1
  • 1
  • 2
8
votes
0 answers

Adjust plotly axes limits and axes title positions on a ternary scatter plot

I am trying to plot data from three columns in a dataframe in a scatter_ternary diagram with contoured datapoints. The columns in the dataframe are Power (W), Speed (mm/s), and Hatch (mm), and I am using another column in the dataframe (Energy…
jcfuller
  • 81
  • 5
8
votes
2 answers

Why break cannot be used with ternary operator?

while(*p!='\0' && *q!='\0') { if(*p==*q) { p++; q++; c++; } else break; } I have written this using ternary operator but why its giving error for break…
Shubham S. Naik
  • 341
  • 1
  • 4
  • 14
8
votes
7 answers

Javascript Ternary operator with empty else

I'm trying to convert the following if-else to it's ternary operator representation in javascript as follows var x = 2; if (x === 2) {alert("2");} else { //do nothing} But when I do this: (t==2)?(alert("1")):(); Chrome throws a…
Probosckie
  • 1,585
  • 4
  • 25
  • 40
8
votes
5 answers

Replacing IF with a logical expression in PHP

I was refactoring some old code when I stumbled upon a construct similar to this: // function bar() returns a value // if the value is an instance of customException class, terminate with error code // else process the regular data $foo =…
user1853181
  • 813
  • 6
  • 12
7
votes
4 answers

Bitboard to titboard (ternary bitboard) conversion

In many board games (like checkers, go and othello/reversi) each square can be represented by three states: white, black or empty. 8x8 boards in such game engines are usually represented as two bitboards: one 64-bit integer for location of white…
Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49
7
votes
16 answers

Is this a reasonable use of the ternary operator?

Are there any understanding / maintainability issues that result from code like inVar1 == 0 ? NULL : v.push_back(inVar1); inVar2 == 0 ? NULL : v.push_back(inVar2); and so forth. The possibly confusing idea is using the ternary operator for program…
user63572
  • 73
  • 1
  • 4
7
votes
2 answers

How can I combine a React element and text inside a ternary?

Normally if you have something like: you can make it optional by using a ternary: {bar ? : null} But what if the block you are starting with contains a component plus some text (a single…
machineghost
  • 33,529
  • 30
  • 159
  • 234
7
votes
2 answers

Truth table for XOR function for non binary bases

I know that the XOR operator in base 2 for two bits A and B is (A+B)%2. In others words, it's addition modulo 2. If I want to compute the truth table for XOR operation in a ternary system (base 3), is it same as addition modulo 3? Eg: In a base 3…
Bugaboo
  • 973
  • 1
  • 17
  • 36
7
votes
1 answer

Ternary heatmap in R

I'm trying to come up with a way of plotting a ternary heatmap using R. I think ggtern should be able todo the trick, but I don't know how to do a binning function like stat_bin in vanilla ggplot2. Here's What I have so…
phildeutsch
  • 683
  • 1
  • 8
  • 18
7
votes
1 answer

Using continue in python ternary?

How can I use continue in python ternary? Is that even possible? E.g. >>> for i in range(10): ... x = i if i == 5 else continue give a SyntaxError: invalid syntax If continue in ternary is possible, is there any other way of doing this: >>>…
alvas
  • 115,346
  • 109
  • 446
  • 738
1 2
3
42 43