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
20
votes
1 answer

Strange implicit conversions with the ternary operator

I have the following code: class A { public: operator int() const { return 5; } }; class B { public: operator int() const { return 6; } }; int main() { A a; B b; int myInt = true ? a : b; return 0; } Attempting to compile…
20
votes
4 answers

JAVA calling a method using a ternary operator

I am trying to use ? to decide which method i want to call, but i do not need to assign a variable. My question: Is there a way to use the ternary operator with out assigning a variable? (something i dont need) = (x == 1)?…
Trae Moore
  • 1,759
  • 3
  • 17
  • 32
17
votes
2 answers

Increment in function overwritten by +=

A method called in a ternary operator increments a variable and returns a boolean value. When the function returns false the value is reverted. I expected the variable to be 1 but am getting 0 instead. Why? public class Main { public int…
17
votes
2 answers

Ternary Operator Inside PHP String

I want to evaluate a simple ternary operator inside of a string and can't seem to find the correct syntax. My code looks like this: foreach ($this->team_bumpbox as $index=>$member) echo ".... class='{((1) ? abc : def)}'>...."; but I can't seem…
JonMorehouse
  • 1,313
  • 6
  • 14
  • 34
16
votes
2 answers

ternary operator in php with echo value

I want to write the following code in ternary operator. I tried in many way but it does not work at all.
sayful
  • 313
  • 1
  • 3
  • 12
16
votes
3 answers

How should I indent ternary conditional operator in python so that it complies with PEP8?

PEP8 doesn't say anything about ternary operators, if I am not mistaken. So what do you suggest, how should I write long lines with ternary conditional operators? some_variable = some_very_long_value \ if very_long_condition_holds \ …
TorokLev
  • 769
  • 6
  • 8
15
votes
3 answers

Can a JavaScript ternary operator support 3 conditions?

Given the following JavaScript ternary operator, is it possible to enable this to support 3 conditions versus the current two? const color = d.y >= 70 ? "green" : "red"; I would essentially like the following logic: >= 70, color = green; between…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
15
votes
3 answers

NullPointerException through auto-boxing-behavior of Java ternary operator

I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
13
votes
4 answers

UML ternary association

I'm currently having some trouble understanding ternary associations in UML. I get the binary ones, but I am unsure how multiplicity works on ternary. I'm doing exercises that I got from my university, the current one goes like this: One department…
Heeiman
  • 249
  • 1
  • 4
  • 15
13
votes
1 answer

C++ : Ternary Operator (Conditional Operator) and its Implicit Type Conversion Rules

Are there rules for implicit type conversion for arguments of the ternary operator? The ternary operator always needs to return the same type. This type is determined solely by the second and third argument (1st ? 2nd : 3rd), therefore both…
dib
  • 133
  • 1
  • 5
13
votes
5 answers

Ternary plot and filled contour

Users, I'd like to have some tips for a ternaryplot ("vcd"). I have this dataframe: a <- c(0.1, 0.5, 0.5, 0.6, 0.2, 0, 0, 0.004166667, 0.45) b <- c(0.75,0.5,0,0.1,0.2,0.951612903,0.918103448,0.7875,0.45) c <-…
FraNut
  • 676
  • 1
  • 11
  • 22
11
votes
2 answers

Result of ternary operator not an rvalue

If you compile this program with a C++11 compiler, the vector is not moved out of the function. #include using namespace std; vector create(bool cond) { vector a(1); vector b(2); return cond ? a : b; } int main()…
Pait
  • 757
  • 6
  • 19
10
votes
3 answers

Why do these two code snippets have the same effect?

template auto max (T1 a, T2 b) -> decltype(b auto max (T1 a, T2 b) -> decltype(true?a:b); I do not understand why these two code snippets can have the same effect. Plz give me…
Edee
  • 1,746
  • 2
  • 6
  • 14
10
votes
4 answers

How to use C#'s ternary operator with two byte values?

There doesn't seem to be a way to use C#'s ternary operator on two bytes like so: byte someByte = someBoolean ? 0 : 1; That code currently fails to compile with "Cannot convert source type 'int' to target type 'byte'", because the compiler treats…
enderminh
  • 166
  • 1
  • 6
10
votes
1 answer

Strange Java behaviour. Ternary operator

Why does this code work? Float testFloat = null; Float f = true ? null : 0f; And why does this throw an exception? Float testFloat = null; Float f = true ? testFloat : 0f; But the strangest thing is that this code also runs successfully without…
user2452103
  • 199
  • 5
1
2
3
42 43