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
141
votes
6 answers

Inline conditions in Lua (a == b ? "yes" : "no")?

Is there anyway to use inline conditions in Lua? Such as: print("blah: " .. (a == true ? "blah" : "nahblah"))
Softnux
  • 2,440
  • 4
  • 20
  • 21
115
votes
8 answers

Python Ternary Operator Without else

Is it possible to do this on one line in Python? if : myList.append('myString') I have tried the ternary operator: myList.append('myString' if ) but my IDE (MyEclipse) didn't like it, without an else.
JCB
  • 1,797
  • 5
  • 18
  • 22
109
votes
10 answers

Java - Check Not Null/Empty else assign default value

I am trying to simplify the following code. The basic steps that the code should carry out are as follows: Assign String a default value Run a method If the method returns a null/empty string leave the String as default If the method returns a…
Cam1989
  • 1,241
  • 2
  • 9
  • 9
69
votes
5 answers

Ternary operator with return statements JavaScript

I need to return true or false if an option in a drop down selected. This is my code: var active = sort.attr('selected') ? return true : return false; I get an error that the first return is unexpected. Why?
JDillon522
  • 19,046
  • 15
  • 47
  • 81
67
votes
13 answers

Java: avoid checking for null in nested classes (Deep Null checking)

Imagine I have a class Family. It contains a List of Person. Each (class) Person contains a (class) Address. Each (class) Address contains a (class) PostalCode. Any "intermediate" class can be null. So, is there a simple way to get to PostalCode…
llappall
  • 2,852
  • 3
  • 23
  • 26
57
votes
7 answers

Java: Ternary with no return. (For method calling)

I was wondering if it was possible to do a ternary operation but without returning anything. If it's not possible in Java is it possible in other languages, if so which ones apply? name.isChecked() ? name.setChecked(true):name.setChecked(false);
TylerKinkade
  • 858
  • 2
  • 8
  • 15
55
votes
11 answers

ternary operator without else in C

I want to use ternary operator without else in C. How do I do it. (a)? b: nothing; something like this. What do I use in nothing part?
user437777
  • 1,423
  • 4
  • 17
  • 28
50
votes
2 answers

?: ternary conditional operator behaviour when leaving one expression empty

I was writing a console application that would try to "guess" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly, The code is: #include #include int…
TooBored
  • 503
  • 1
  • 4
  • 6
37
votes
9 answers

How to write ternary operator condition in jQuery?

In this fiddle http://jsfiddle.net/mjmitche/6nar4/3/, if you drag, for example, the little blue box into the yellow box, then the big black box will turn pink. All of the 4 boxes along the left can be dragged into the boxes inside the black box. At…
Leahcim
  • 40,649
  • 59
  • 195
  • 334
37
votes
5 answers

return statement in ternary operator c++

I wrote the absolute function using ternary operator as follows int abs(int a) { a >=0 ? return a : return -a; } I get the following error messages ../src/templates.cpp: In function ‘int abs(int)’: ../src/templates.cpp:4: error: expected…
ameen
  • 811
  • 2
  • 12
  • 16
37
votes
4 answers

Java Ternary without Assignment

Is there a way to do a java ternary operation without doing an assignment or way to fake the assingment? I like how succinct ternary code looks when doing a bunch of if/then/elses. I'm hoping to be able to call one of two void functions based on a…
James Oravec
  • 19,579
  • 27
  • 94
  • 160
30
votes
4 answers

Javascript ternary operator and assignment

I get unexpected result for this simple JavaScript assignment statement: var t = 1 == 1 ? 1 : 0; undefined I would have expected to get 1 assigned to t instead. Same result if you do var t = (1 == 1 ? 1 : 0); undefined Can somebody explain why…
faridz
  • 729
  • 1
  • 9
  • 9
24
votes
4 answers

Java autoboxing and ternary operator madness

Just spent a frustrating couple of hours debugging this code: LinkedHashMap rsrqs = new LinkedHashMap(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) :…
Exponent
  • 492
  • 1
  • 11
  • 18
23
votes
3 answers

ER-Diagram: Ternary Relationship - How to read properly?

Im not quite sure how to read ternary relationships within a ER-Diagram. Lets say this is the ternary relationship that is given. What can I interpret out of that? It says that you have to put your hand on 2 entity sets and then read it like…
user2276094
  • 399
  • 1
  • 4
  • 11
21
votes
14 answers

Multiple conditions in ternary operators

First off, the question is "Write a Java program to find the smallest of three numbers using ternary operators." Here's my code: class questionNine { public static void main(String args[]) { int x = 1, y = 2, z = 3; int…
Mike
  • 219
  • 1
  • 2
  • 3
1
2 3
42 43