Forums

One-Hot or Not?

Started by MaxMaxfield 3 months ago8 replieslatest reply 3 months ago80 views

Here's another question that just popped into my mind. Suppose we have a state machine with four states, we could represent these states using two registers and binary encoding (00, 01, 10, 11) or four registers and one-hot encoding (0001, 0010, 0100, 1000).

Now suppose we have a 7-segment display where each segment is controlled by its own bit, so we coudl represent this as:

0000001 // Segment 1
0000010 // Segment 2
0000100 // Segment 3
0001000 // Segment 4
0010000 // Segment 5
0100000 // Segment 6
1000000 // Segment 7

Would it be correct to describe this as "one-hot" encoding?

Now, suppose that we want to light up multiple segments simultaneously; for example:

0011001 // Light segments 1, 4, and 5

Do we still refer to this as "one-hot"?

[ - ]
Reply by martin_harnevieJune 8, 2021

0001000
0011000
0101000
0001000
0001000
0001000
0111110

0100010
0100010
0100010
0111110
0100010
0100010
0100010


0011100
0100010
0100010
0100010
0100010
0100010
0011100

0111110
0001000
0001000
0001000
0001000
0001000
0001000


Possible ;-) 

[ - ]
Reply by MaxMaxfieldJune 8, 2021

The reason I'm confused is when we use "one-hot" in the context of state machines, we can only have one 1 -- but in the multi-segment display example, although we have one bit per segment, we can have multiple bits active at the same time, so can we still refer to this as "one-hot"?

[ - ]
Reply by martin_harnevieJune 8, 2021

Sorry, strictly speaking when more than one bit is set to 1 then it's not a "one-hot".

[ - ]
Reply by MaxMaxfieldJune 8, 2021
I feared not LOL
[ - ]
Reply by trogers531June 8, 2021

The relevant detail is the meaning, not the representation. In the one-hot state machine representation the meaning of the four bits is the machine’s current state; in the seven segment display example the meaning is a convoluted recasting of the expected interpretation of the user viewing the display.

The state machine representation has additional significance, in that the one-hot data is inherently exclusive on position. Depending on the way the state variable is instantiated, enforcing exclusivity can be a quick reality check on state validity, or the state may be distributed, in which one-hot becomes a form of token passing, and so on.

The one’s in the seven segment display variable have an entirely different meaning. The best way to think of this is that the meaningful solution space for the two variables are simply different. The differences define an entire universe of valuable information that isn’t apparent simply by accessing the variable’s value, such things as valid range, what operations are fair game, what results should trigger an exception, what to do when dealing with that, and so on.

So the answer is probably yes, call ‘em both one-hot, as long as you pay attention to which solution space the thing you’re referring to lives in. And for that, the answer shades over to no, for the same reason that having multiple independent variables with the same names is a bad idea, even if they’re the same type, and even if you promise to remember they’re different, ‘cause sooner or later, you, or somebody else much less careful, won’t.

 —T

[ - ]
Reply by MaxMaxfieldJune 9, 2021

So the answer is "yes" and "no" -- I can live with that :-)

[ - ]
Reply by andy_pJune 9, 2021

The whole point of one-hot state encoding is that the comparison needed to indicate "we are in State ABC" is very simple: is the bit that represents that state true or not?

With small machines that have only a handful of states, the compare isn't too bad if it's binary encoded (8 states represented by three bits, say).

A machine with 20 states or more? One-hot wins.

Anyway, what you are describing for your segment control is a simple bitmap.

[ - ]
Reply by MaxMaxfieldJune 9, 2021

You make my little bitmap sound so pedestrian -- in reality I'm working with a 29-segment display -- but I see what you mean :-)