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
-4
votes
3 answers

Javascript ternary operator result always the same

The code below always responds with "hi". Even if i set the campaign1 variable to 0. Can anyone advise if I am writing this out incorrectly? campaign1 = 2; campaign_string = (typeof campaign1 > 1 ? "hello" : "hi" ); Kind regards,
Amar
  • 11
  • 3
-4
votes
1 answer

C# ternary will not output either or after user input

I was wondering why this piece of code is not outputting even or odd would I need to add a readline statement after the ternary operation using System; using System.Collections.Generic; using System.Linq; using System.Text; using…
user2005549
  • 231
  • 2
  • 3
  • 4
-4
votes
3 answers

Return result from Ternary in one line (JavaScript)

In JavaScript, rather than having to assign the result to a variable, is it possible to return the result of a ternary in one line of code? e.g. Instead of this: function getColor(val){ var result = val <= 20 ? '#000' : val >= 80 ? '#999' :…
Panomosh
  • 884
  • 2
  • 8
  • 18
-5
votes
2 answers

Convert if condition into ternary with C#

I would like to use ternary operator in order to factorize if condition. I know the process : condition ? consequent : alternative I have the if condition following : if (!myField.IsNullOrNa()) { messageErreur +=…
Essex
  • 6,042
  • 11
  • 67
  • 139
-5
votes
1 answer

who can explain the first output?(java operator)

public static void main(String[] args) { char alpha = 'A'; int foo = 65; boolean trueExp = true; System.out.println(trueExp ? alpha : 0); System.out.println(trueExp ? alpha : foo); } run result:A …
JunChen
  • 41
  • 1
  • 3
-5
votes
2 answers

Ternary operation with a "negative" variable

What does the negative variable do in a ternary? Why is the output -10 is 10? public class Ternary { public static void main(String[] args) { int i, k; i = -10; k = i < 0 ? -i : i; System.out.print(i + " is " +…
Enrique_Iglesias
  • 121
  • 1
  • 2
  • 11
-5
votes
3 answers

Am I using the ternary operator?

Here is my code: echo ""; what im trying to do is echo out the selected value from the database in echo
Adi
  • 72
  • 1
  • 2
  • 16
-5
votes
2 answers

C - Ternary operate to if else

Can someone help me convert this line to an if-else statement? temp = head->left ? head->left : head->right; Thanks!
-5
votes
3 answers

Ternary passing of arguments

In Java is it possible to use ternary to pass multiple arguments into a method call? For example - in a method have: print(degree == 270 ? "e", 5 : "t", 6); which calls: public void print(String s, int t){ } By using the ternary I…
Jim
  • 274
  • 3
  • 13
-6
votes
1 answer

Javascript ternary operator hidden logic

function even_or_odd(number) { return number % 2 === 0 ? 'Even' : 'Odd'; } function even_or_odd(number) { return number % 2 ? "Odd" : "Even" } Why do these two functions return the same result? How does return number % 2 ? "Odd" :…
Elan
  • 539
  • 1
  • 6
  • 18
-6
votes
1 answer

What is meaning of "?"

IF OFFSET >=0 THEN TIME_LOCAL:= ((current_time + OFFSET)>24) ? ((current_time + OFFSET) - 24) : (current_time + OFFSET); else TIME_LOCAL:= ((current_time + OFFSET) < 0) ? ((current_time + OFFSET) + 24) : (current_time + OFFSET); endif;
Videlov
  • 1
  • 1
-6
votes
4 answers

Ternary Operator not working properly in C++

I have the following code snippet: if (w) { if (b) { if (c) cout << "great!"; else cout << "3"; } else { cout << "2"; } } else { cout << "1"; } I have to use the ternary…
Andrei
  • 217
  • 2
  • 12
-7
votes
2 answers

How do you create ternary heatmap plots in R?

I want to make a triangle plot represents the response surface for all possible combinations of X, Y and Z factors and the gradient area inside the triangle expresses the response variable Gi. The dots represent the ten combinations of X, Y and Z in…
Hoang Le
  • 308
  • 1
  • 3
  • 14
1 2 3
42
43