Configuration Variable/BitSwitches

There are several ways to store data in CVs. The one that causes the most confusion for those who are not acquainted with the inner workings of a computer is the "bit switch". This system is far simpler that it might appear. Consider the table below: 
==== Bit Switch Table ==== 
Most 
Significant
Bit	Bit 	Bit 	Bit 	Bit 	Bit 	Bit 	Bit 	Bit 	Least
Significant
Bit
Most 
Significant
Bit	7 	6 	5 	4 	3 	2 	1 	0 	Least
Significant
Bit
Most 
Significant
Bit	Weight	Weight	Weight	Weight	Weight	Weight	Weight	Weight	Least
Significant
Bit
Most 
Significant
Bit	128 	64 	32 	16 	8 	4 	2 	1 	Least
Significant
Bit
Most 
Significant
Bit	0 	0 	0 	0 	0 	0 	0 	0 	Least
Significant
Bit
Think of the eight bits of the CV as a bank of eight simple on/off switches. Each switch is identified by the bold number above it. A value of "0" means the switch is OFF or in its default mode, a value of "1" turns it ON. A decimal number between 0 and 255 identifies a unique setting for every switch in the bank of 8. This allows you to turn on or off any possible combination of these 8 switches by entering the appropriate binary number into the CV. These software switches are the digital equivalent of the DIP switches, selector switches, or the jumpers that were often found on computer hardware. 
==== Example of Byte Value Calculation ====
Here is an example of the best-known CV that uses this technique, CV29:
EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com	EXAMPLE OF CV 29 PARAMETERS DCCWiki.com
 	Multifunction / Accessory Decoder 	RESERVED 	Primary / Extended Address Mode (EAM) 	Speed Table Enable 	Advanced Acknowledge Enable 	Alternate Power Source 	F0 Control 	Normal Direction of Travel (NDOT) 	Decimal Value
Bit 	7 	6 	5 	4 	3 	2 	1 	0 	
Weight 	128 	64 	32 	16 	8 	4 	2 	1 	
Default Value 	0 	X 	0 	0 	0 	1 	1 	0 	6
Status 	OFF 	OFF 	OFF 	OFF 	OFF 	ON 	ON 	OFF 	
Example 	OFF 	OFF 	ON 	OFF 	OFF 	ON 	OFF 	OFF 	
Value 	0 	0 	32 	0 	0 	2 	0 	0 	36
	
 
In the above example, 2 bits are on: Bits 32 and 4. Add 32 + 4 and you get a total of 36, the decimal value of the bit switch settings shown. If you wanted to turn on the speed tables too, then you add 16 to that value, for a total decimal value of 52.
Some times you need to work in reverse: You read the value of the CV from the decoder, then figure out what switches are on and which are off. It's simple, but tedious.
The binary equivalents of the switch positions (as shown in the table above) are 128 - 64 - 32 - 16 - 8 - 4 - 2 - 1. 
Start with the value you read from the CV, in this case 52.
# 52 is less than 64, so there are no 128s or 64s in the CV, those switches are off.
# 52 is greater than 32, so there is a 32 in the CV; that switch is on.
# Subtract 32 from 52 giving a remainder of 20.
# 20 is greater than 16, so there is a 16 in the CV; that switch is on.
# Subtract 16 from 20 giving the remainder of 4.
# 4 is less than 8, so that switch is off.
# 4 = 4, so that switch is on.
# Subtract 4 from 4 giving 0; all remaining switches (2 and 1) are off. 
# Switches 32, 16, and 4 are on, the rest are off. By matching the function to the bit switch, you determine what parameters are active.
Note: Bit 0, Normal Direction of Travel, should not be used to correct a miswired motor.
Category:CV
Category:Programming