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

Should I replace all my if/else statements with ternary shorthand if/else? (PHP)

I was reading about ternary shorthand if/else and am wondering if it would make sense or be more beneficial to replace all (or most) of my traditional if/else statements with the ternary shorthand? Would that make it run faster? Or would the…
orbit82
  • 513
  • 3
  • 6
  • 17
3
votes
1 answer

Java return null for primitive in ternary

The following (logically) is a compile-time error: public int myMethod(MyObject input) { if (input == null) { return null; // compiler says I cannot return null for primitive type } else { return 1; } } So far so good. What I…
D. Kovács
  • 1,232
  • 13
  • 25
3
votes
1 answer

Plotting Ternary Phase Diagram with MATLAB

I would like to plot a ternary phase diagram based on ab-initio energy inputs. There, I found a useful tool which may help me: https://de.mathworks.com/matlabcentral/fileexchange/2299-alchemyst-ternplot There are several issues I need to alter: I…
halil
  • 33
  • 1
  • 3
3
votes
3 answers

Ternary using pass keyword python

I have a working conditional statement: if True: pass else: continue that I would like to turn it into a ternary expression: pass if True else continue However, Python does not allow this. Can anyone help? Thanks!
Kodiak North
  • 71
  • 1
  • 10
3
votes
2 answers

ternary foreach nested within if / else

I was wondering how I can rewrite the following using ternary within ternary or within alternative syntax. $tags = get_the_tags(); if (!empty($tags)) { foreach ($tags as $tag) { echo $tag->name . ', '; } } else { echo…
fizzy drink
  • 682
  • 8
  • 21
3
votes
1 answer

true or false as result of php ternary operator

I'm working with legacy PHP code and I'm seeing a lot of instances where the programmer has done this: $foo = ($bar === 'baz') ? true : false; instead of: $foo = ($bar === 'baz'); Is there ever a case of a conditional expression where the first…
thanksd
  • 54,176
  • 22
  • 157
  • 150
3
votes
1 answer

What is the Performance Cost of Ternary Operator

I've heard compilers are very smart and know how to optimize if / else statements. I've also heard ternaries are high performance because they go through the CPU's instruction pipeline less. Let me clarify, based on what I've heard: An if / else…
Magical Gordon
  • 404
  • 2
  • 8
3
votes
2 answers

Ternary within a ternary

So it's very common to have for loops ect, but when we have a ternary example such as int answer = (a < b) ? b : a; How can we put another ternary after the ? like an enhanced ternary question: Given two int values, return whichever value is…
Hello
  • 123
  • 7
3
votes
4 answers

How to do this with the ternary operator in one line?

Consider this const declaration of int num: int main() { bool a = true, b = false; // ... const int num = a ? (b? 2 : 4) : 4; std::cout << num; } What I want is for const int num to follow this truth table (which I apologize has…
prestokeys
  • 4,817
  • 3
  • 20
  • 43
3
votes
2 answers

Conditional Ternary Operations

First off, I came across this question recently and haven't being able to find a good explanation for it: int x = (30 > 15)?(14 > 4) ? 1 : 0 : 2; I have used ternary expression before so I am familiar with them, to be honest I don't even know what…
CDVN
  • 171
  • 1
  • 1
  • 11
3
votes
1 answer

Linq where with ternary and brackets

I have the following query: return Postcodes .Where( s => s.Postcode.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) > -1 && string.IsNullOrEmpty(country) ? true :…
Pete
  • 57,112
  • 28
  • 117
  • 166
3
votes
2 answers

PHP, Shorthand, If..Else using Ternary Operators

Is there a oneliner for this? A nice Ternary OP? $F_NAME = $_SESSION['USR']['F_NAME']; if(isset($_POST['F_NAME'])) {$F_NAME = $_POST['F_NAME'];} Basically "If the POST is sent, show that, even if the post is empty, otherwise grab the value from the…
Christian Žagarskas
  • 1,068
  • 10
  • 20
3
votes
5 answers

Java Ternary Search

I am trying to perfrom a ternary search on a array of strings. I have got most of code down and I think I am going on the right track but can't seem to get any results other then -1. Below is the code that I have genearated thus far. I know the…
M Lill
  • 31
  • 2
3
votes
4 answers

Ternary operator in actualization of a loop

I am trying to make two loops into one. This Loop should go through the array from the beginning to the end and otherwise. However my increment is not correct. Can anyone help? Thank you in advance. for (int i = ((asc == true) ? 0 : calendar.Length…
3
votes
4 answers

Can I conditionally choose what variable I assign a value to?

I know that you can do things like this in Python: var = value1 if( booleanCheck() ) else value2 What I want to know is if I can conditionally choose which var I place a value into with a similar sort of structure? something like this: (var1 if(…
user2614726
  • 131
  • 1
  • 4