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

NHibernate ternary association with multiple values - how to map in a nice way

I've asked a similar question, but I've given up on the idea I had there to solve this problem so I would like some help solving this in a neat way instead. I've got tables Image - (Id, Name, RelativeFilePath) ImageFilter - (Id, Type) ImageContext -…
Max
  • 4,345
  • 8
  • 38
  • 64
4
votes
2 answers

error: expression is not assignable ternary operator

I have the following code and MPLABX XC8 compiler gives this error: error: expression is not assignable U1ERRIRbits.RXFOIF ? uart1.oerr = 1 : uart1.oerr = 0; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ Here is the relevant code section:…
Arda30
  • 107
  • 5
4
votes
1 answer

How do I convert nested ternary operator to nested if-else statements?

I'm having a hard time trying to convert the following C# code into a if-else statement, so that I can understand the business logic. Can anybody help me, checking whether my conversion is ok? Code: dateInit = objInstance == null ? (DateTime?)null: …
Pedro Coelho
  • 1,411
  • 3
  • 18
  • 31
4
votes
1 answer

How do you convert hexadecimal to ternary in Python?

I'm trying to convert any length hexadecimal to ternary (two way), specifically in Python. I've tried using int, and that didn't work. I also tried this library called baseconvert, but that just spits an error, NameError: name 'base' is not defined,…
j3d1H
  • 43
  • 7
4
votes
1 answer

How to put the scaling on the ticks in ternary plot instead of x and y axis

I am try to work out with my atomic composition with ternary phase diagram, here is my picture I wish to put my scale to the ticks on the ternary phase diagram (i.e. those triangular axis) instead of x and y axis. Is there a ways to put the scale…
4
votes
2 answers

Converting streaming data into ternary (base-3)

Given a clocked 3-level (-1,0,+1) channel between two devices, what is the most stream-efficient way to convert a stream of bits to and from the channel representation? The current method is to take 3 binary bits, and convert into two trits. I…
frLich
  • 163
  • 1
  • 5
4
votes
4 answers

C# Enum requires cast in ternary?

Today I was doing another Codegolf challenge over at Codegolf StackExchange, and I tried to do this: SomeEnum = SomeCondition ? 1 : 2; but this tells me Cannot convert source type 'int' to target type 'SomeEnum', so I tried this instead: SomeEnum =…
Metoniem
  • 239
  • 2
  • 15
4
votes
3 answers

Swift Ternary Operator function call?

I want to start using the Swift ternary operator so that I don't set values but I fire different functions. if position.x < kWidth / 2 ? fireLeft() : fireRight() It does not work, and I am actually quite by that. What can I do to make it work?
4
votes
0 answers

Sequelizejs HasMany ternary relationship

I want to make in Sequelize a ternary relationship as seen in the picture below Image https://www.dropbox.com/s/v8bgsir2qqw6ccv/relationship%20.jpg If I apply this code in…
CMM
  • 311
  • 3
  • 9
4
votes
4 answers

String concatenation does not work properly in Java when concatenating 2 results of ternary operators

Dear Java guru 's! Can you, please, explain me, why String concatenation does not work properly in Java when concatenating 2 results of ternary operators? Example: String str = null; String x = str != null ? "A" : "B" + str == null ? "C" :…
daniilyar
  • 2,602
  • 3
  • 22
  • 25
4
votes
5 answers

Multiple Ternary operator in c++

I have a doubt in the following scenario (C++): Say, I have an if condition if ( a ? b ? c : d : false) { // do something } else { // do something else } This is my interpretation of how it works: If a is true, it checks b. Then, - If…
mdv
  • 499
  • 2
  • 8
  • 14
4
votes
1 answer

Assigning extension methods to a delegate using ternary statements

Assumptions Let's assume we have an interface, and the following extension methods defined for said interface (their implementations aren't important) public interface IPerson; public class IPersonExtensionMethods { public static bool…
Richard
  • 8,110
  • 3
  • 36
  • 59
4
votes
3 answers

python ternary iteration with list comprehension

Is ternary iteration possible? A simplistic version of what I mean, though this particular example could be done in a better way: c = 0 list1 = [4, 6, 7, 3, 4, 5, 3, 4] c += 1 if 4 == i for i in list1 else 0 A more practical example: …
Jack J
  • 1,514
  • 3
  • 20
  • 28
3
votes
3 answers

JS Ternary operator confusion

I'm learning about ternary operators now. I got the basics down, but then I saw this snippet and it doesn't make any sense to me. Can anyone please explain how is it put together?! b.m & 4 || (c |= 2, 63 <= a && 77 >= a ? a = 65 : 48 <= a && 57 >= a…