Two's Complement
OK, so I could brute force this, but I'm thinking that there's probably some elegant way of using bitwise operators to take an int in the range of -128 to 127, and take its two's complement to get a positive value from 0-255.
Who's got the cleanest solution?
i believe that when the range is bipolar, there is no way to use combinations of bitwise operators or bitshift to perfom linear arithmentic tasks (such as + 128)
it would need something like "bit split" then you could do the rest with ~ and &
also, a range of 255 wont fit into one byte.
Is this it? I'm not a huge fan of if statements in Max, but.....
of course the "if" alone needs 10 times more CPU than [+ 128] :)
try [& 255]
Not following you @klsdiz - what's the [& 255] replacing in my patch?
For [& 255] I meant an object with `& 255` typed inside. It's just a simple bit masking done with an AND operator, see:
cheers!
Thanks @klsdiz - I should have been more precise, I knew exactly what object you were referring to, I just wasn't sure how you were using it in relation to my original patch. And now I see that that's *all* you're using!!
I'm face-palming myself that it was that simple, thank you!! I knew there was a simpler way....
that was the first which i tried and it didnt seem to work?
Coming back to this after a few years - is there an easier way to go back to the signed int after converting to two's complement, than the way I am doing it here? I'm thinking it's going to be something easy like adding or subtracting 1, and then doing some other bitwise operation, but again, not able to easily wrap my head around this! Thanks for any help.
you need this for the next midi editor dont ya
expression should be more effective and one object less (SNCR)
expr ($i1<128) * $i1 + ($i1>=128) * ($i1-256)
otoh, that is still not better than using offsets.
i see nothing shorter - next please.
Hahaha - you're onto me Roman! ;-)
In reading more about two's complement (this is the most understandable/helpful link I've found): https://www.tutorialspoint.com/two-s-complement , what I'm most after that I'm surprised to have not easily found is a Max object to simply flip the bits of an integer - every 0 becomes a 1 and vice versa. Is it staring me in the face and I'm just missing it? Or 3rd party?
that is a bit like searching the opposite of abs() in arithmetics.
what I'm most after that I'm surprised to have not easily found is a Max object to simply flip the bits of an integer - every 0 becomes a 1 and vice versa. Is it staring me in the face and I'm just missing it?
try [expr $i1 ^ 255]
this works for 8-bit numbers
?

try [expr $i1 ^ 255] this works for 8-bit numbers
That's it Volker, thank you!! And BTW, I like your bit abstraction too, I've always used Peter Elsea's bit object, but this gives me a nice, native Max way to do the same...
you are welcome!
btw. the [>> 0] in the upper right of the bit subpatcher is a leftover from another patch - it's obsolete here.
Thank you! I was wondering what bit shifting a number by 0 places did, had that on my list to explore! I'm guessing in another patch you changed that 0 to another number.... Thanks for clarifying!
i sometimes replace [+ 0] or [- 0] with [>> 0] to make my patches look more interesting.