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

C++ conditional ternary operator

Lines 11, 12, 15 and 16 are getting errors: " invalid operands of types int and const char[2] to binary operator<< " (I removed the "`" so it wouldn't display it code format). #include using namespace std; int main(){ int…
-1
votes
1 answer

Why cant I return the result of ternary operator?

Consider this code: private static Context myContext; public static Context getInstance() { myContext = myContext == null ? new Context() : myContext; return myContext; } After refactoring like this, my app started to throw NullPointers:…
MrAs
  • 77
  • 8
-1
votes
4 answers

Is it possible to fit in a ternary operator as the termination in a for loop?

So, I just want to know if its possible to slip in any code or a ternary operator inside the termination bit of a for loop. If it is possible, could you provide an example of a ternary operator in a for loop? Thanks! for (initialization;…
nextDouble
  • 43
  • 4
-1
votes
1 answer

How to condense my if statement

I have been asked to create a mark to grade converter in Windows Form Application. My code below is from the click of a button. Once a user has input their mark into 'Markbox' and the button is clicked the if statement will run and it will find the…
Bal Jhand
  • 61
  • 5
-1
votes
1 answer

IF IF statement with a Ternary (JavaScript)

How can I turn the following statement into a Ternary. if($.isNumber(arg1)) { if($.isNumber(arg2)) { // something } } I know how to do an IF / ELSE with a Ternary statement like: var a = $.isNumber(arg1) ? arg1 :…
Panomosh
  • 884
  • 2
  • 8
  • 18
-1
votes
3 answers

Couldn't understand the ternary operator behavior

Output of the following program is : 3 1 3 int main() { int a = 0, b = 1, c = 3; *((a) ? &b : &a) = a ? b : c; // Couldn't understand expression printf("%d %d %d \n", a, b, c); return 0; } How ternary operator is working here for…
dcoder11
  • 31
  • 4
-1
votes
1 answer

R VCD: Ternary plot legend

I have successfully created a ternary plot with the following code: data <- read.csv(file, header=TRUE) ternaryplot(data[,3:5], scale = 100, pch = 19, cex = 0.4, col = Category, dimnames = c("Al", "Mg", "Fe + Ti"), labels =…
Holly Elliott
  • 103
  • 3
  • 10
-1
votes
3 answers

Shorthand IF statement in PHP

I'm wondering if the following is possible. I want the if statement that I wrote below to be in one line (ternary). I want to use the ternary(shorthand php code) in a array for cURL. Form data example: $_POST['data'] = 'sometextrighthereisgood:this…
Alex
  • 1
  • 1
-1
votes
1 answer

Using the PHP ternary operator to return a specific value

I googled this and browsed stack overflow already. I am trying to shorten my code and I have this working method below I want to rewrite in ternary style. Can someone tell me if this is possible and if so what im doing wrong. Thanks. public function…
peb7268
  • 23
  • 4
-2
votes
1 answer

can we code to find negative number and positive number using ternary operator in java

can we code to identify positive number and negative number in java using ternary operator. import java.util.*; public class negative{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int…
-2
votes
1 answer

swift: about ternary operator Question. Why my code is error code??? Please tell me why I'm wrong

swift: about ternary operator Question. Why my code is error code??? Please tell me why I'm wrong. var arr = [0,1,2,3,4,5,6,7,8] var result = 0; for a in 0..
sweetSwift
  • 37
  • 5
-2
votes
1 answer

How to use ternary operator in php?

When I use ternary operator in PHP I realize that 0, '0',and null is null so this is little strange, in my opinion that this value '0' considered as a string and no longer considered as null, but the result in ternary this will return to null…
-2
votes
1 answer

using swift, I'm simply trying to turn an else block in to a ternary operator

if bulletsOn == true { bullets -= 0.003 } else { bullets += 0.001 } when I put in form of ternary like so, I get error from compiler. bulletsOn ? bullets -= 0.003 : bullets += 0.001 error: Result…
-2
votes
1 answer

using ternary operation instead of if statement

I want to do this code you have seen using ternary operation instead of if conditional blocks. Generally, is the use of the ternary operator always a good choice? In some places they say it is more readable and more flexible code to use the ternary…
-2
votes
1 answer

How to convert this example of nested ternary if-else to if-else loop

How to convert this example of nested ternary constructor to if-else loop. I am an begginer and is complicated form me. int bill = 2000; int qty = 10; int days = 10; int discount = (bill > 1000)? (qty > 11)? 10 : days > 9? 20 : 30 :…