Bitmasking in DSP Code
Posted: Wed Sep 20, 2017 9:20 am
Howdee,
the subject of using bitmasks in FS e.g. to implement logical structures comes up every now and then so I thought perhaps a tutorial might be helpful to have. I plan to do this in a number of installments so this is only the first part of a series. Please feel free to interrupt me and post questions.
It is always a good idea to consult the User Guide first, so let me start with that:
Let's elaborate a bit. There is the & operator, a "bitwise and" for the arguments left and right of it. In the example given, one argument is a boolean expression (x >= 1) and the other is a float number 1.0. Both have a 32 bit representation in FS per SSE channel. The boolean expression is either true or false:
Float numbers are represented as bit patterns according to the IEEE 754 standard, which for the example happens to be
"Bitwise and" means that each of the 32 binary digits is evaluated to 1 if both those digits are 1, and is zero otherwise. At the end of the day, all you need to remember is the following:
By the way, the order of the arguments does not matter, so bool & number is the same as number & bool.
the subject of using bitmasks in FS e.g. to implement logical structures comes up every now and then so I thought perhaps a tutorial might be helpful to have. I plan to do this in a number of installments so this is only the first part of a series. Please feel free to interrupt me and post questions.
It is always a good idea to consult the User Guide first, so let me start with that:
Let's elaborate a bit. There is the & operator, a "bitwise and" for the arguments left and right of it. In the example given, one argument is a boolean expression (x >= 1) and the other is a float number 1.0. Both have a 32 bit representation in FS per SSE channel. The boolean expression is either true or false:
- Code: Select all
true = 11111111111111111111111111111111
false = 00000000000000000000000000000000
Float numbers are represented as bit patterns according to the IEEE 754 standard, which for the example happens to be
- Code: Select all
1.0 = 00111111100000000000000000000000
"Bitwise and" means that each of the 32 binary digits is evaluated to 1 if both those digits are 1, and is zero otherwise. At the end of the day, all you need to remember is the following:
- Code: Select all
number & true = number
number & false = 0
By the way, the order of the arguments does not matter, so bool & number is the same as number & bool.