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

C program to check for presence of ternary operator

I am unable to find out if the ternary operator is present or not through following code.. Plz help. #include #include #include void one(); int main() { FILE *fp; …
Raj Chauhan
  • 15
  • 1
  • 8
-1
votes
1 answer

Can I store html in a variable if the html includes a concatenated isset() ternary expression?

I don't even know how to ask this clearly so I apologize for any confusion. Basically I'm trying to store html in hoverHTML whereby if $img['socialfb'] contains a value it will concatenate the
  • element that displays the correct social icon and…
  • NikF
    • 21
    • 7
    -1
    votes
    2 answers

    How To Use Nested Ternary Operator In C#?

    Here i have used if , else if condition to show an error message and make some label visible and invisible, but i am trying to use ternary operator to do so but i am quite unfamiliar with ternery operator and unable to use it for all condition i…
    CarrotCrop
    • 73
    • 1
    • 5
    • 12
    -1
    votes
    1 answer

    JavaScript reduce ternary operator

    This is silly, but I'm having hard time understanding how these two statements outputs different results. 'theyyyyy wheels on q bus'.split(' ').reduce((shortest, w) => { return w.length < shortest.length ? shortest = w : shortest; }) //…
    -1
    votes
    3 answers

    I do not understand this PHP Code, what is it doing?

    How to read php i'm learning php, I wrote this. (It is from a book)
    Vina L
    • 49
    • 5
    -1
    votes
    1 answer

    Ternary Heap Null Pointer Exception

    package queue; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TernaryHeap > extends AbstractPriorityQueue { private List keys; private int…
    J. Choi
    • 45
    • 7
    -1
    votes
    1 answer

    Is metric tool in eclipse counting ternary operator complexity?

    I am using sonarQube(third party analyzer-Findbug) at server and metric tool for calculating cyclomatic complexity at local computer. SonarQube is calculating the complexity for the following code but metric is not counting it.I am using metric…
    rushang patel
    • 115
    • 2
    • 10
    -1
    votes
    1 answer

    how to plot 3d ternary plot for three mixtures

    I have tried with your terndemo but I'm unable get a graphical data like you sir. My aim is to present that how, each formulation is responsible for different zetasizes in 3d ternary plot. I tried like editing the file of yours but the result is…
    -1
    votes
    2 answers

    Python Ternary Rescursion

    I'm creating two functions one, that returns the ternary representation for a base 10 number, and one that returns the base 10 representation for a ternary number using recursion. For example 52 would return 1221. Right now, I have this down, but…
    Phil Cho
    • 131
    • 3
    • 14
    -1
    votes
    1 answer

    Ternary Conversion in Python not working well. Why?

    I've tried to make a ternary calculator on Python, with some other functions like hex, bin, and oct with the built-in functions. There isn't one for ternary so I built one. def ternary(n): e = n/3 q = n%3 e = n/3 q = e%3 return…
    harmony5
    • 3
    • 1
    • 4
    -1
    votes
    4 answers

    Can strings be used in a ternary conditional statement?

    I'm (very) new to Java and have been using Codecademy, and after learning about ternary conditionals I was wondering if a string could be used in place of char? I know that strings aren't a real primitive data type in Java whereas char is, but it…
    -1
    votes
    1 answer

    Using Java's ternary operator, what's the implication of assigning a variable to itself?

    I make excessive use of the ternary operator, sometimes even replacing very simple if statements with it. For example, I just found myself replacing something like this: if (foo != null) { bar = foo; } with this: bar = (foo == null) ? bar :…
    domsson
    • 4,553
    • 2
    • 22
    • 40
    -1
    votes
    1 answer

    ternary operators quotes issue

    I'm trying to display a default image in a HTML page in case the original one is not available. I'm using a ternary operator as the condition is inside an "echo" but I'm having some issues I think with quotes. The below code doesn't give me an error…
    ChrisA
    • 81
    • 11
    -1
    votes
    2 answers

    Conceptual data model using ternary relationship

    I have the following statements to create my conceptual model: EMPLOYEE belongs to one DEPARTMENT; EMPLOYEE can work in many PROJECTs of his DEPARTMENT; PROJECT is managed by only one DEPARTMENT; DEPARTMENT can have many PROJECTs; So 1 EMPLOYEE…
    Roni Castro
    • 1,968
    • 21
    • 40
    -1
    votes
    2 answers

    PHP ternary operators

    I have written a ternary function in PHP and it seems to work, although I am not sure if it is correct, can someone take a look and tell me if it is right? I have added the ternary and the if of what should be happening. //Foreach Loop …
    Can O' Spam
    • 2,718
    • 4
    • 19
    • 45