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

Ternary operator and Sequence Points in C

I've an expression of the form shown below :- while (count) { ... ... index = ((count == 20)? 0 : index++); ... ... } Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part. Is this…
Zshn
  • 121
  • 1
  • 5
6
votes
3 answers

Can I Initialize a char[] with a Ternary?

I asked a question about it and didn't get a really clear answer, but after reading this article I started preferring const char[] to const char*. I've come upon a difficulty when initializing with a ternary. Given const bool bar, I've tried: const…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
1 answer

Can the ternary operator be equivalent to short circuiting with the logical operators?

With short circuiting you can prevent the evaluation of part of an expression: let x = "", y = 123; x && alert("foo"); // "" y || alert("bar") // 123 Since the logical ops form expressions you can use them in function invocations or return…
user5536315
6
votes
5 answers

How to convert from unbalanced to balanced ternary?

My goal is to convert a decimal number into balanced ternary. Converting from decimal to unbalanced ternary simply requires division by 3 and keeping track of remainders. Once I have the unbalanced ternary representation of the number I can't seem…
corecase
  • 1,278
  • 5
  • 18
  • 29
6
votes
5 answers

Ternary operator

Why the output of following code is 9.0 and not 9 ? If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ? public class Ternary { public static void main(String args[]) { int a…
Praveen Kumar
  • 1,624
  • 5
  • 18
  • 21
5
votes
6 answers

Conditional operator for an operator

I have a piece of code: if (foo > bar) { baz = foo - bar } else { baz = foo + bar } I have a question if I can somehow shorten this code to a single line, something like PSEUDOCODE: baz = foo (foo > bar ? + : -) bar Real code I'd like…
Pavel T
  • 79
  • 5
5
votes
2 answers

How to merge two binary numbers into a ternary number

I have two binary integers, x0 and x1 that are 8 bits (so they span from 0 to 255). This statement is always true about these numbers: x0 & x1 == 0. Here is an example: bx0 = 100 # represented as 01100100 in binary bx1 = 129 # represented as…
Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45
5
votes
1 answer

ASP.NET razor ternary expression on anonymous types

I have the following code:
@Html.DropDownListFor(model => model.SelectedAssignToId, Model.AssignToListItems, ViewBag.CanEditRequest ? new { @class = "form-control" } : new { @class = "form-control",…
user1408767
  • 677
  • 1
  • 8
  • 30
5
votes
3 answers

PHP ternary inside concatenation with no else?

I want to check if 2 variables are the same and if so, echo a string. Is this possible within a concatenation? And to do it without creating a separate function? e.g. $var = 'here is the first part and '. ( $foo == $bar ) ? "the optional middle…
Andrew Tibbetts
  • 2,874
  • 3
  • 23
  • 28
5
votes
3 answers

whats wrong with this ternary operator?

I have an object menuNames which should maintain a list of menu items. If menuNames already has the slug, increment the value, if it doesnt contain the slug, set the value equal to 1. I'm doing this to track unique names. I want to end up with…
jaredrada
  • 1,130
  • 3
  • 12
  • 25
5
votes
1 answer

What is VB.NET's equivalent of C#'s default keyword?

Possible Duplicate: Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null While this question may seem like a duplicate of many, it is actually being asked for a specific reason. Take this code, for…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
4
votes
0 answers

.net component to draw ternary diagram?

For a winform application written in C# (4.0 for framework or less), we need to display data in a ternary diagram. Do somebody know a graphical component written in C# that can be used? (open-source or not) We want to be able to : zoom/unzoom on…
Manu13
  • 71
  • 1
4
votes
1 answer

Ternary producing different results than If

EDIT: For anyone who has arrived here with this issue, I found this documented almost verbatim in the perldoc at https://perldoc.perl.org/perlop#Conditional-Operator In writing a simple subroutine, I found I kept getting incorrect results using the…
Myersguy
  • 43
  • 4
4
votes
6 answers

Java calling method and using ternary operator and assign in the parameters?

I was reviewing some code and I came across this: public static doSomething(String myString, String myString2) { //Stuff } public static doAnotherThing(String myString) { return doSomething(myString = myString != null ?…
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
4
votes
1 answer

Javascript ternary if statement, assigning it to a variable and incrementation

So, I have this piece of code, which actually works: (hash would be an object like this: {"bob" => "12, "Roger" => "15", etc}, and isGood(key) is a calling the function isGood that just returns if the player is good or bad) let score =…
Olivier Girardot
  • 389
  • 4
  • 16