Short and quick version. (For those already familiar with bit-wise variable usage from other systems and just need to know the ACK equivalent syntax...
You can use the following bit operations (the operators "AND" and "OR") in macros to set/clear individual bits in a variable without affecting other bits in that variable:
ACK syntax for Logical AND
SET <variable> = <variable> & <value>
ACK syntax for Logical OR
SET <variable> = <variable> | <value>
bit 0 - 1
bit 1 - 2
bit 2 - 4
bit 3 - 8
bit 4 - 16
bit 5 - 32
bit 6 - 64
bit 7 - 128
bit 8 - 256
bit 9 - 512
bit 10 - 1024
bit 11 - 2048
bit 12 - 4096
bit 13 - 8192
bit 14 - 16384
bit 15 - 32768
To set a bit in a variable, do a logical OR of that bit's value with the variable. For example, to set bit 3 (value 8) in variable C, do
SET C = C | 8
To clear a bit in a variable, you take that bit's value, subtract it from 255, then "AND" the result to the variable. For example, to clear bit 5 (value 32) in variable B, do
SET B = B & 223 (because 255-32=223)
You can test a single bit in a variable with
IF <variable> & <bit> THEN...
for example, "IF A & 64 THEN 12" will test whether bit
6 (with bit weight/value 64) is set in variable A. If bit 6 is set to 1, execution will jump to line 12, otherwise it will continue on the next line as usual.
Lengthy in depth study of how it works...
LOW/HIGH BYTES
BASICS:
A byte is 8 bits. Zero is counted so 8 bits would be numbered 0-7.
ACK has two sets of variables. First set of "small" variables (A-G and A2-G2) are 8 bit only. Each variable holding just one byte values.
The second set (H-Z and H2-Z2) of "large" variables are stored using two bytes or 16 bits. Here is where we get the concept of a low and a high byte.
As rld explained:
"For the 16 bit variables, you can also consider them as the sum of a 'low byte' and a 'high byte', where each byte is 8 bits. If you look in the ACK source code, this is actually the way these variables are stored internally, such that
variable value = (low byte value) + (high byte value * 256)"
The maximum value that one of these large (16bit or 2 byte variables) can hold is 65535 which is simply 256 multiplied 256 times. Remember to count the zero bit as the first bit so 256 is actually 0-255.
Check out this chart that outlines the binary format. It helped me alot.
http://www.vaughns-1-pagers.com/compute ... s-of-2.htm
THE CHART EXPLAINED IN RELATION TO ACK VARIABLE VALUES:
Notice the exponents in the 2nd column. Each bit of the byte increases the bytes value exponentially by a power of 2.
In the first 8 bits you have exponent 0-7 (that is 8 exponents). In the next 8 bits after that which is bit line number 9-16 you have exponents 8-15 (another 8 exponents).
If you examine these closely you will see that each of powers of 2 in the second set of higher exponents correspond to each of the lower powers of 2 in the first 8 bit set.
Each exponent in the second set is simply the same exponent in the first set plus 8.
First bit in the first byte (or first 8 bits) is base 2 to the power of 0.
The first bit of the second byte (or second set of 8 bits) is base 2 to the power of 8 (0+8).
The second bit of the first byte is 2 to the power of 1.
The second bit of second byte is 2 to the power of 9 (1+8).
We can see another pattern occurring in the third column titled "Binary Bit Weight in Decimal."
The first bit of the first byte has a binary bit weight of 1. The first bit of the second byte (or 9th bit) has a binary bit weight of 256 (or 256 multiplied by 1).
The second bit of the first byte has a binary bit weight of 2.
The second bit of second byte (or 10th bit) has a binary bit weight of 512 (or 256 multiplied 2 times).
1st Byte, 3rd bit = binary bit weight 4
2nd Byte, 3rd bit (or 11th bit) = binary bit weight 1024 (or 256 multiplied 4 times).
And so on increasing exponentially by powers of 2...
Okay final piece of puzzle here.
Now look at the fourth column of the binary chart and the values listed under Decimal. Notice how each number in the decimal column is the sum of all the bit weights to the left and up from it. in other words what you are looking at in the decimal column is the totals if you were to turn on just those bits, or if you were to add the values of just the bits listed up to that point in the chart.
Here is where we can see how values can be obtained by simply switching certain bits on and off. When a bit is off it has no value. When on it has the value of its bit weight. When more than one bit is on you add the bit weights to determine the total binary value for that byte.
For single byte variables you only have 8 bits to look at and add up. For the larger variables you have 2 bytes so 16 bits to add up which could be seen as actually two sets corresponding to the first and second byte or first 8 bits plus the second set of 8 bits.
So we can see by all this here that bit-wise variable values are just following the pattern from this binary chart. Each bit represents a specific binary bit weight which is determined by the powers of 2 column.
And ACK is very simple in that is is limited to just 2 bytes. There are no 3 byte variables. So you can easily memorize not only the pattern here but also the values. Theres only 16 binary weights to remember, or just 8 if you only use the 8 bit variables.
Just remember that each bit has a bit-wise value that never changes. It can be set on or off but it's value never changes. The value of the whole byte is just a total of all the bits that are turned on. If you just want the first bit of a byte turned on there is a total byte value for that. If you want just the last bit turned on there is a total byte value for that. If you want both the first and last bit turned on there is a total byte value for that (just add bit 1 weight to bit 8 value). And any combination of bits will have a different byte value.
For the large variables you are actually adding the total value of one byte to the total value of the second byte to determine the grand total value for that variable.
EDIT: Changed old typo in this post... "1st Byte, 3rd bit = binary bit weight 3" ... to "1st Byte, 3rd bit = binary bit weight 4"[/b]
Short and quick version. (For those already familiar with bit-wise variable usage from other systems and just need to know the ACK equivalent syntax...
You can use the following bit operations (the operators "AND" and "OR") in macros to set/clear individual bits in a variable without affecting other bits in that variable:
[b]ACK syntax for Logical AND[/b]
SET <variable> = <variable> & <value>
[b]ACK syntax for Logical OR[/b]
SET <variable> = <variable> | <value>
bit 0 - 1
bit 1 - 2
bit 2 - 4
bit 3 - 8
bit 4 - 16
bit 5 - 32
bit 6 - 64
bit 7 - 128
bit 8 - 256
bit 9 - 512
bit 10 - 1024
bit 11 - 2048
bit 12 - 4096
bit 13 - 8192
bit 14 - 16384
bit 15 - 32768
To set a bit in a variable, do a logical OR of that bit's value with the variable. For example, to set bit 3 (value 8) in variable C, do
SET C = C | 8
To clear a bit in a variable, you take that bit's value, subtract it from 255, then "AND" the result to the variable. For example, to clear bit 5 (value 32) in variable B, do
SET B = B & 223 (because 255-32=223)
You can test a single bit in a variable with
IF <variable> & <bit> THEN...
for example, "IF A & 64 THEN 12" will test whether bit
6 (with bit weight/value 64) is set in variable A. If bit 6 is set to 1, execution will jump to line 12, otherwise it will continue on the next line as usual.
[b]Lengthy in depth study of how it works...[/b]
LOW/HIGH BYTES
BASICS:
A byte is 8 bits. Zero is counted so 8 bits would be numbered 0-7.
ACK has two sets of variables. First set of "small" variables (A-G and A2-G2) are 8 bit only. Each variable holding just one byte values.
The second set (H-Z and H2-Z2) of "large" variables are stored using two bytes or 16 bits. Here is where we get the concept of a low and a high byte.
As rld explained:
"For the 16 bit variables, you can also consider them as the sum of a 'low byte' and a 'high byte', where each byte is 8 bits. If you look in the ACK source code, this is actually the way these variables are stored internally, such that
variable value = (low byte value) + (high byte value * 256)"
The maximum value that one of these large (16bit or 2 byte variables) can hold is 65535 which is simply 256 multiplied 256 times. Remember to count the zero bit as the first bit so 256 is actually 0-255.
Check out this chart that outlines the binary format. It helped me alot.
http://www.vaughns-1-pagers.com/computer/powers-of-2.htm
THE CHART EXPLAINED IN RELATION TO ACK VARIABLE VALUES:
Notice the exponents in the 2nd column. Each bit of the byte increases the bytes value exponentially by a power of 2.
In the first 8 bits you have exponent 0-7 (that is 8 exponents). In the next 8 bits after that which is bit line number 9-16 you have exponents 8-15 (another 8 exponents).
If you examine these closely you will see that each of powers of 2 in the second set of higher exponents correspond to each of the lower powers of 2 in the first 8 bit set.
Each exponent in the second set is simply the same exponent in the first set plus 8.
First bit in the first byte (or first 8 bits) is base 2 to the power of 0.
The first bit of the second byte (or second set of 8 bits) is base 2 to the power of 8 (0+8).
The second bit of the first byte is 2 to the power of 1.
The second bit of second byte is 2 to the power of 9 (1+8).
We can see another pattern occurring in the third column titled "Binary Bit Weight in Decimal."
The first bit of the first byte has a binary bit weight of 1. The first bit of the second byte (or 9th bit) has a binary bit weight of 256 (or 256 multiplied by 1).
The second bit of the first byte has a binary bit weight of 2.
The second bit of second byte (or 10th bit) has a binary bit weight of 512 (or 256 multiplied 2 times).
1st Byte, 3rd bit = binary bit weight 4
2nd Byte, 3rd bit (or 11th bit) = binary bit weight 1024 (or 256 multiplied 4 times).
And so on increasing exponentially by powers of 2...
Okay final piece of puzzle here.
Now look at the fourth column of the binary chart and the values listed under Decimal. Notice how each number in the decimal column is the sum of all the bit weights to the left and up from it. in other words what you are looking at in the decimal column is the totals if you were to turn on just those bits, or if you were to add the values of just the bits listed up to that point in the chart.
Here is where we can see how values can be obtained by simply switching certain bits on and off. When a bit is off it has no value. When on it has the value of its bit weight. When more than one bit is on you add the bit weights to determine the total binary value for that byte.
For single byte variables you only have 8 bits to look at and add up. For the larger variables you have 2 bytes so 16 bits to add up which could be seen as actually two sets corresponding to the first and second byte or first 8 bits plus the second set of 8 bits.
So we can see by all this here that bit-wise variable values are just following the pattern from this binary chart. Each bit represents a specific binary bit weight which is determined by the powers of 2 column.
And ACK is very simple in that is is limited to just 2 bytes. There are no 3 byte variables. So you can easily memorize not only the pattern here but also the values. Theres only 16 binary weights to remember, or just 8 if you only use the 8 bit variables.
Just remember that each bit has a bit-wise value that never changes. It can be set on or off but it's value never changes. The value of the whole byte is just a total of all the bits that are turned on. If you just want the first bit of a byte turned on there is a total byte value for that. If you want just the last bit turned on there is a total byte value for that. If you want both the first and last bit turned on there is a total byte value for that (just add bit 1 weight to bit 8 value). And any combination of bits will have a different byte value.
For the large variables you are actually adding the total value of one byte to the total value of the second byte to determine the grand total value for that variable.
EDIT: Changed old typo in this post... "1st Byte, 3rd bit = binary bit weight 3" ... to "1st Byte, 3rd bit = binary bit weight 4"[/b]