Binary bitmask
doozer
Junior Member
Hi All,
I'm sure i'm doing something stupid here (my background is in C which tends to cause me problems when coding for netlinx), but I can't seem to use a binary integer when doing a bitmask comparison.
I'm receiving a char via serial and need to check if a bit (in this case bit 2) is set. I tried using:
if (receivedChr & 00000010b)
{
// do something
}
But it results in a compiler error:
ERROR: C10201: Syntax error; Illegal or Invalid syntax in code
Of course changing it to:
if (receivedChr & $02)
{
// do something
}
Compiles fine. My question is, how come I can't do a binary bitmask (it will make the code easier to understand)?
I have also tried declaring my binary value as a constant (which the docs seem to indicate is legal) but that too generates an error.
Thanks,
Matt.
I'm sure i'm doing something stupid here (my background is in C which tends to cause me problems when coding for netlinx), but I can't seem to use a binary integer when doing a bitmask comparison.
I'm receiving a char via serial and need to check if a bit (in this case bit 2) is set. I tried using:
if (receivedChr & 00000010b)
{
// do something
}
But it results in a compiler error:
ERROR: C10201: Syntax error; Illegal or Invalid syntax in code
Of course changing it to:
if (receivedChr & $02)
{
// do something
}
Compiles fine. My question is, how come I can't do a binary bitmask (it will make the code easier to understand)?
I have also tried declaring my binary value as a constant (which the docs seem to indicate is legal) but that too generates an error.
Thanks,
Matt.
Comments
-
Looks like a bug to me. I've never tried to write a constant in binary, but the help file definitely seems to indicate that it should work, and it definitely does not.
-
I have also tried declaring my binary value as a constant (which the docs seem to indicate is legal) but that too generates an error.
Thanks,
Matt.
A binary number is not an intinsic data type in Netlinx or C. You will have to use an integer or the hex equivalent, in your case, 2 or $02.
Paul -
It might not be implemented, but it's definitely described in the NetLinx help file:NetLinx/Axcess - Comparison by Structure: Constants
The DEFINE_CONSTANT section in NetLinx is similar to the DEFINE_CONSTANTS section in Axcess. The scope of the constant extends throughout the module in which it is defined. If the DEFINE_CONSTANT section appears in the main program or in an include file, the constant's scope extends globally throughout the program. DEFINE_CONSTANT accepts data in these formats:
Types
Formats
Examples
Decimal Integer
0000
1500
Hexadecimal Integer
$000
$DE60
Binary Integer
000b
01110011b
Floating Point
000.0
924.5
Exponential Notation
0.0e0
.5e-12
Character
'c' or <char code>
'R' or 255
String Literal
'ssss?
'Reverse'
The standard format for DEFINE_CONSTANT is:
<constant name> = <constant expression>
NetLinx allows variables to be defined as constants in the DEFINE_VARIABLE section of the program or module, and in the LOCAL_VAR section of a DEFINE_CALL or a DEFINE_FUNCTION. Assigning constants is consistent with C++ programming conventions. -
It might not be implemented, but it's definitely described in the NetLinx help file:
I have seen that several times when I ran through the help file, it has never worked. I think it was a "WISH" item from the beginning of time, just never got removed from the help file. -
Just to clarify, binary is a notation not a data type. The original poster was trying to use binary notation instead of hex notation or decimal notation. Intrinsic data types are things like INTEGER,CHAR, etc.A binary number is not an intinsic data type in Netlinx or C. You will have to use an integer or the hex equivalent, in your case, 2 or $02.
The help file does imply that binary notation is acceptable; however, as discovered that isn?t the case. An error in a help file, imagine that.
-
Thanks for all your comments. Indeed the help file was the first place I turned to when it didn't work.
I shall move on with a hex bitmask. -
I use binary bitmaps all the time, I just don't write the code in binary. I convert the binary to decimal and write that in my code. For example, part of an Aprilaire thermostat module where I use a bitmask to track the relay states of a thermostat:
DEFINE_CONSTANT // For easier code reading FAN_RELAY_ON = 1 Y1_RELAY_ON = 2 // 1st Stage Cooling/Dehumidify W1_RELAY_ON = 4 // 1st Stage Heat/Humidify Y2_RELAY_ON = 8 // 2nd Stage Cooling/Dehumidify W2_RELAY_ON = 16 // 2nd Stage Heat/Humidify B_RELAY_ON = 32 // Cool reversing valve O_RELAY_ON = 64 // Heat reversing valve // from the parsing function: ACTIVE (FIND_STRING(sRsp, "'HVAC='", 1)) : { STACK_VAR CHAR nBase ; nBase = 128 ; REMOVE_STRING(sRsp, "'HVAC='", 1) ; IF(FIND_STRING(sRsp, "'G+'", 1)) nBase = nBase | FAN_RELAY_ON ; IF(FIND_STRING(sRsp, "'G-'", 1)) nBase = nBase & ~FAN_RELAY_ON ; IF(FIND_STRING(sRsp, "'Y1+'", 1)) nBase = nBase | Y1_RELAY_ON ; IF(FIND_STRING(sRsp, "'Y1-'", 1)) nBase = nBase & ~Y1_RELAY_ON ; IF(FIND_STRING(sRsp, "'Y2+'", 1)) nBase = nBase | Y2_RELAY_ON ; IF(FIND_STRING(sRsp, "'Y2-'", 1)) nBase = nBase & ~Y2_RELAY_ON ; IF(FIND_STRING(sRsp, "'W1+'", 1)) nBase = nBase | W1_RELAY_ON ; IF(FIND_STRING(sRsp, "'W1-'", 1)) nBase = nBase & ~W1_RELAY_ON ; IF(FIND_STRING(sRsp, "'W2+'", 1)) nBase = nBase | W2_RELAY_ON ; IF(FIND_STRING(sRsp, "'W2-'", 1)) nBase = nBase & ~W2_RELAY_ON ; IF(FIND_STRING(sRsp, "'B+'", 1)) nBase = nBase | B_RELAY_ON ; IF(FIND_STRING(sRsp, "'B-'", 1)) nBase = nBase & ~B_RELAY_ON ; IF(FIND_STRING(sRsp, "'O+'", 1)) nBase = nBase | O_RELAY_ON ; IF(FIND_STRING(sRsp, "'O-'", 1)) nBase = nBase & ~O_RELAY_ON ;
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums