Two's Complement

Dan Nigrin's icon

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?

Roman Thilenius's icon


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.

Dan Nigrin's icon

Is this it? I'm not a huge fan of if statements in Max, but.....

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

Roman Thilenius's icon

of course the "if" alone needs 10 times more CPU than [+ 128] :)

kLSDiz's icon

try [& 255]

Dan Nigrin's icon

Not following you @klsdiz - what's the [& 255] replacing in my patch?

kLSDiz's icon

For [& 255] I meant an object with `& 255` typed inside. It's just a simple bit masking done with an AND operator, see:

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

cheers!

Dan Nigrin's icon

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....

Roman Thilenius's icon

that was the first which i tried and it didnt seem to work?

Dan Nigrin's icon

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.

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

Roman Thilenius's icon


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.

Dan Nigrin's icon

Hahaha - you're onto me Roman! ;-)

Dan Nigrin's icon

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?

Roman Thilenius's icon


that is a bit like searching the opposite of abs() in arithmetics.

volker böhm's icon

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

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


Roman Thilenius's icon
Dan Nigrin's icon

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...

volker böhm's icon

you are welcome!
btw. the [>> 0] in the upper right of the bit subpatcher is a leftover from another patch - it's obsolete here.

Dan Nigrin's icon

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!

Roman Thilenius's icon


i sometimes replace [+ 0] or [- 0] with [>> 0] to make my patches look more interesting.