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
-2
votes
2 answers

Do these ternary operator output the same result?

Is $time ?: 3600 equivalent to isset($time) ? $time : 3600 ? Thank you all.
Lewis
  • 14,132
  • 12
  • 66
  • 87
-2
votes
1 answer

Javascript ternary user error?

So what I'm trying to do is make a script that will show and hide the title of YouTube videos and all my script seems to be doing is setting it default to display none which I don't want it to do that and it seems as if the onclick function isn't…
Bren
  • 605
  • 4
  • 15
-2
votes
2 answers

Ternary Put Operator in cpp possible?

I was thinking about implementing a ternary put operator in cpp similar to "<<": mystream <<< param2 param3; Is this possible? Does it already exist? One remark: I remember having seen this: out <
B M
  • 3,893
  • 3
  • 33
  • 47
-2
votes
6 answers

Contiguous substrings containing one or two characters odf a given Ternary string

One question for an Interview was that : given a ternary string , find the number of contiguous substrings which contain only one or two character of the given ternary string. A ternary string is one which is made up of at most 3 characters. Like…
nishantsatyam
  • 27
  • 1
  • 2
  • 8
-3
votes
2 answers

Try to return in ternary expression

I have a Swift application. I'm getting the error Expected expression after '?' in ternary expression from Xcode compiler in private func getContentPre(highlight: Highlight) -> String! { highlight.contentPre.count == 0 ? return ">" :…
ozd
  • 1,194
  • 10
  • 34
-3
votes
5 answers

Ternary Condition for if { } else if { if { } else { } }

I want to change all the if else statements to ternary operator. Whats the ternary operator for this if else statements ? const compareHands = (playerChoice, computerChoice) => { // Update Text const winner =…
HorrorRice
  • 33
  • 1
  • 5
-3
votes
1 answer

What does the question mark followed by a period mean if it is next to a variable?

I am well aware of ternaries. Just wondering what on earth the question mark next to "l" in the following line of code: public static string GetRegion() { var ipAddress = GetIPAddress(); if…
d1du
  • 296
  • 1
  • 3
  • 12
-3
votes
1 answer

How do you decode a string in ternary (or any unusual base)?

For binary, quaternery, octonery and hex it's clear for me how to convert a stream of them to plain text. Can anybody help me understand how should I do it for ternary, quinary, senary and other bases? (Here's a ternary example, a python script…
Kennet Celeste
  • 4,593
  • 3
  • 25
  • 34
-3
votes
2 answers

Understanding the ternary operator

I have the following example: $a=false; $b=true; $c=false; if($a ? $b : $c){ echo 'false'; } else { echo 'true'; } I can't seem to understand this statement,and i need someone to explain me how it works...
Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
-3
votes
2 answers

If else with Ternary operator

Below is the example: If I write like: //Out will be "A" But if I write like below: It give me out…
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
-3
votes
1 answer

php multidimensional array ternary statement syntax

// $vote_table[index]['voter_meta']['name'] is the key to sort by. usort( $vote_table, function( $a, $b ){ return ($a['voter_meta']['name'] == $b['voter_meta']['name']) ? 0 : ( ($a['voter_meta']['name'] < $b['voter_meta']['name']) ? -1 …
-3
votes
3 answers

What would be the equivalent if else statement of this ternary expression?

bool wm(const char *s, const char *t) { return *t-'*' ? *s ? (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1) : !*t : wm(s,t+1) || *s && wm(s+1,t); } I have search the internet for ternary/if else equivalents, but this one seems weird…
-4
votes
1 answer

Clarification on Ternary Shortenings involving Null in Java

This site: Information on the null constant in Java states that the following two statements are synonymous: if(PossibleNullVariable!=null)PossibleNullVariable.Action(); PossibleNullVariable!.Action(); Also, This site: Information on avoiding null…
-4
votes
1 answer

Functions not executing in ternary operator

I tried this in my code and also in the Chrome console and it's not doing what I want. Do you know why it's not working? let r = 'd'; r == 'e' ? console.log('itisE') : r == 'd' ? console.log('itisD') : console.log('nothing'); Chrome's console…
user1944491
  • 559
  • 1
  • 8
  • 24
-4
votes
2 answers

Ruby Ternary Inside Hash Build

Looking for a way to include a ternary conditional inside a hash assignment. a = 5 h = {} h[:alpha] => a > 3 ? true : false # edited twice h[:alpha] => (a > 3 ? true : false) # edited twice There has to be a way of shortening this up.
Rich_F
  • 1,830
  • 3
  • 24
  • 45
1 2 3
42
43