Bitmasking is a method used to manipulate individual bits of a binary number by applying bitwise operations with a mask. A mask is a binary number designed to target and select specific bits from another binary number.
Example that extracts four different statuses from the same byte
In all the examples, we´ll use the failure status bit 0x21. It´s position in the raw BLE Advertising Frame is marked with pink color.
0x0201060A0957454D46464646354410FF0E0421114948490000000000005400
Byte Failure Status table
Mask | Masked Byte Value | Extracted Bit Values | Description | Comment |
---|---|---|---|---|
0x300011 0000 |
0x00 0x10 0x20 0x30 |
0 1 2 3 |
Battery extreme low Battery Low level Battery OK No data |
Battery level |
0x030000 0011 |
0x00 0x01 0x02 |
0 1 2 |
No Data No failure Failure |
Wheel End Failure |
0xC01100 0000 |
0x00 0x40 0x80 0xC0 |
0 1 2 3 |
No failure Piezo Failure WEM Failure Reversed position |
WEM failure = accel or RAM failure |
In TraX, we have the following bitmasks: 0x30, 0x03, 0xC0, and 0x0C. These hexadecimal values represent specific bit patterns.
Masking for Different Failures
Battery Level Mask 0x30
To apply the bitmask, we perform a bitwise AND operation between the byte and the mask:
0x21 (00100001) & 0x30 (00110000) = 0x20 (00100000)
Result: Applying the 0x30 bitmask to 0x21 is 0x20, indicating "Battery OK".
Wheel End Failure Mask 0x03
0x21 (00100001)
& 0x03 (00000011)
= 0x01 (00000001)
Result: Applying the 0x03 bitmask to 0x21 gives 0x01, indicating No Wheel End Failure.
WEM Failure Mask 0xC0
0x21 (00100001)
& 0xC0 (11000000)
= 0x00 (00000000)
Result: Applying the 0xC0 bitmask to 0x21 gives 0x00, indicating No WEM Failure.
Temperature Sensor Failure Mask 0x0C
0x21 (00100001) & 0x04 (00001100) = 0x00 (00000000)
Result: Applying the 0x0C bitmask to 0x21 gives 0x00, indicating No Temperature Failure.
Javascript Code Sample
See the file "bit-masking-example.js" attached to this article.
Comments
0 comments
Article is closed for comments.