Heyho,
did anyone try to mess with the bit-logic (like [& ], [| ], etc.)
There it's somewhere written that it works for 32bit...
But that does not seem to be true, although pd only uses 32bit-floats...
Well, does anyone know some more about it?!
Bye
-
Using bitwise logics...
-
It does work with 32-bits, but they are converted to integers first, so you still have to work with integers to get expected results. Also, keep in mind that 32-bit floats only have 24-bit integer precision.
-
I'm aware of the 32bit-float structure, the "int" part is rather called the mantissa(+sign).
Why would one convert it to int (=don't use the exponent-part) at first?? If one just uses the mantissa it's no surprise that it just works for 24bits..
But 32bit are 32bit.. So theoretically it should be no problem.. And in fact it works for "2^n" since these numbers only contain 1 "1bit"...
So I still hope this is a bug (within the single objects, hopefully - and not the whole pd).. -
It's not a bug. The source code explicitly converts them to ints before applying the operation. My guess for why is that these types of operations are typically done on integers. I agree, though, it doesn't make a lot of sense, especially since they are converted back to floats once they leave the object.
-
Do you have some knowledge of textbased programming..? Mine is rather limited. Actually I started with pd and only recently got into textbased - in order to understand how pd is made.
So isn't it possible to convert without data loss??
I have the source here (as anyone with pd does I guess..). Could you point me to the place for the bitwise operators?
And do you (or anyone else, of course - don't want to exclude anybody) know how to use the "m_pd.h"?? Should it be self-explanatory, or is there a small, good tutorial or something?? -
I have a decent understanding of C, just enough to write non-GUI externals. m_pd.h isn't particularly well documented, at least not to me. It's enough to let you know what functions are provided for those writing externals, but it doesn't necessarily tell you what they do (and if you're new to C, it may just look like gibberish). If you'd like to learn, though, it's probably best to start here:
http://iem.at/pd/externals-HOWTO/
It's not the most thorough tutorial, but it can at least get you started. I started with that, then studied the source of other externals to find out how to do things I didn't know how to do.
The bitwise operators are in x_arithmetic.c. A quick Googling suggests that the bitwise operators in C only work on integer types, though. You might be able to get around this using a union, which basically allocates a space in memory that can be treated as different variables of different types. I believe it's meant to be used for memory conservation where you can assume only one of those variables will be used depending on what is needed, so there's no need to actually reserve space for both. However, you can also use it to write to the memory in one type and access it in another, which I think would be useful here.
As a simple illustration:
union num { /* these types are defined in m_pd.h */ t_float f; /* 32-bit float */ t_int i; /* integer at least 32-bits */ } num.f = 123.567; post("%d", num.i);
The code above would store the number 123.567 as a float. num.f and num.i use the same space in memory, so num.i would interpret the data stored as an integer. I haven't really tried this, but I think it can be used to do what you are looking for.