If you speak computer you more then likely do not need this. But if you
do not and/or do not understand bit masking, then I will try to help you
out. One of the problems is trying to relate it to something you can understand,
once you can relate it to something, then it will start to be easier to
understand. In the case here a byte consists of 8 bits. Now if we
can look at a bit as being a switch with only two states, just like a single
pole single throw toggle switch. This is the same way a bit works on and
off, off =0 and on=1. With me so far.
Ok now if we take 8 of these little switches and put them all in a
row and lets give each one a name, bit 0, bit 1, etc. And to make it a
little harder, lets number them from right to left. We end up with a row
of switches starting with 0 and ending with 7 being read from right to
left. Now keep in mind we can turn each one of these switches on or off
as we like. Lets take a look at the example below.
| bit 7
on value 128 loco |
bit 6
on value 64 reserved |
bit 5
on value 32 2/4 |
bit 4
on value 16 table |
bit 3
on value 8 ack |
bit 2
on value 4 analog |
bit 1
on value 2 14/28 |
bit 0
on value 1 direction |
Here each box has our switches in it with it's name bit 0, bit 1, etc.
Already to be turned on and off as we want or need. Ok I think if you look
this over a few times you will see how it all works. But now we need a
way to turn the bit switches on and off. This is where the mask comes from.
If you notice there is a on value added to each box. This is the decimal
value assigned to that bit switch when turned on, if a bit switch is turned
off the value is always 0. The on value totals represent the number we
enter in to that CV. The maximum number would be 255, just total all the
on-values up, this would represent all 8 bit switches turned on. The minimum
number would be 0, this would represent all bit switches turned off. Pretty
simple huh. So how do we put it all to a practical use.
Lets say you want to set CV29 to the following: Forward base
direction (bit 0 off =0). Advanced speed step control (bit 1 on =2). Analog
conversion (bit 2 on =4). User load able speed table off (bit 4 off = 0).
Four digit addressing (bit 5 on =32). To enter the value in to CV29 we
just add them up (2+4+32)=38, CV29=38. What happens when we enter a decimal
38 in to CV29, Bits 1, 2, and 5 are turned on. It really is pretty simple
if you look it over a little bit [pun intended].
But wait you say you know that should be CV29=26 not 38. That is because
your system uses Hexadecimal not decimal. 38 decimal = 26 hexadecimal.
If this is the case there are several ways you can go, I will only get
into what I feel are the two easiest ways here. The first is do it just
like above, then look at a Decimal to Hex conversion chart, you will usually
find this in your manual, if Hex is what your system uses.
The other is the way I feel is easiest to do, because you are dealing
with smaller numbers. Besides I do not like looking up the charts. There
a couple of things you need to learn first. The first thing is how to count
in Hex, this has to do with rollover points. In Decimal the rollover point
is based on 10. We count 0123456789, then we rollover at 10, that is we
go back to 0 and carry our 1. Then we go to 19 rollover at 20 and carry
our 2, etc, etc.
In Hexadecimal the rollover point is based on F. We count 0123456789ABCDEF
then rollover to 10 and then go up to 1F and roll over to 20, etc, etc.
But here all you have to worry about is the 0-F count, not the rollover.
The next thing to learn is about Least and Most Significant bits. But
that is easy enough, the right 4 bit switches are the least significant
and the left 4 are the most significant. Lets look at the example below.
| bit 7
on value 8 loco |
bit 6
on value 4 reserved |
bit 5
on value 2 2/4 |
bit 4
on value 1 table |
bit 3
on value 8 ack |
bit 2
on value 4 analog |
bit 1
on value 2 14/28 |
bit 0
on value 1 direction |
Things look pretty much the same as the other Decimal example, except we
have divided in to the Left and Right digits, and you will notice the small
on values. Makes things a lot easy to add up doesn't it. As long as you
remember how to count in Hex 0123456789ABCDEF, the minimum value would
be 0 all bit switches off. And the maximum value would be FF all bit switches
on. By the way Hex FF = Dec 255 sound familiar. Ok got all this, lets try
the same example as we did with the decimal values.
Lets say you want to set CV29 to the following: Forward base
direction (bit 0 off =0). Advanced speed step control (bit 1 on =2). Analog
conversion (bit 2 on =4). User load able speed table off (bit 4 off = 0).
Four digit addressing (bit 5 on =2). To enter the value in to CV29 we just
add up the Left Hex Digit [most significant] bits (5)= 2,
and the Right Hex Digit [least significant] bits (2+4)= 6
CV29= 26. Notice all we did was work the left and right digits separately.
Try these a few times on paper and I think you will find them a lot easier then they look. And I hope you now know a little bit more then when you started and CVs and Bit masking are not as mysterious as they were to you.
NOTE: Some systems
count their bits a little different then normal. Instead of starting at
0 they start at 1 and count to 8. This is not a big problem at all. Just
replace the bit numbers, all other values remain the same.
NOTE: The examples
given here were for CV29. But they also apply to any multi function bit masked CVs.
Copyright © 1998 Don Crano