Bitwise Operators on Floating Point Signal

Emmett Palaima's icon

Emmett Palaima

9月 10 2024 | 11:05 午後

Hi, I'd like to know if there are any operators I can use to precisely set and compare the bit values of a 32-bit floating point audio signal.

Something that would set bits 0-32 to 0 or 1, and could check whether bits 0-32 are 0 or 1.

Is there anything like this in gen or within the audio objects?

Is there any workaround that could accomplish precisely setting and comparing bits?

Roman Thilenius's icon

Roman Thilenius

9月 11 2024 | 4:09 午前

in realtime?

the first bit can be found by [> 0], the rest is thin ice via "workaround". ;)

plus your data wil be 64 bits as soon as it is in MSP.

eventually it can be done using decimal maths in msp or gen~, but even if that works error-free, it will require quite some CPU.

the guy who posted this picture does not even understand how you would compare bits

for single samples / nonrealtime you could convert the samples to data rate and then there is surely a conversion calculator for java or js available.

Emmett Palaima's icon

Emmett Palaima

9月 11 2024 | 6:13 午前

Yes in realtime.

I am trying to update 32 binary values at audio rate using a 32-bit audio signal I am receiving via black hole.

Here is a gen~ patcher I have used successfully to pack 16 binary values into a floating point audio signal, maybe some variation on this would be possible?

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon

Roman Thilenius

9月 11 2024 | 7:30 午後


yes, for 16 bit integer it is a^2+b^4+... easy.

but in 64 bit float this kind of conversion will only work for values which fit exactly into bits 3-10, the other 99.999999% of potential input values will fail.

precisely 12.567 does not exist in float. so any attempt to arithmetically derive most of the mantissa bit´s meaning will introduce errors. but if those are relevant or can just be rounded apart, no idea.

12.567 in binary is 1100.10010001001001101111

now you need to calculate as much places behind the point as required to fill all the bits (possibly 52 for float64) and then you have to calculate something which includes shifting the binary point, and adding the bias to get the exponent bits, which somebody else must explain.

Roman Thilenius's icon

Roman Thilenius

9月 12 2024 | 2:05 午前

ah, silly me. leftshift does exist for MSP, so you can use the polarity trick.

so here we go... this is for float32 signals, it would work likewise for 64 in max 8.

12.567messagebox

12.5670003890991210937510

0_10000010_10010010001001001101111_f32

0_10000000010_1001001000100100110111010010111100011010100111111100_f64

if your incoming audio was 32 and your max is 64, remove those 3 extra 0s at the beginning of the first block (sparing the leading 1), and then cut off the additional 0s at the tail of the mantissa when you want to compare them.

01000001010010010001001001101111
-->
0100000000101001001000100100110111000000000000000000000000000000