## bitwise reversal

Nov 3, 2008 at 2:31am

# bitwise reversal

hi,

how would you reverse the order of bits in an integer?

for example:
00010011 -> 11001000
10000010 -> 01000001

thanks, james.

#40628
Nov 3, 2008 at 5:13am

There might a super-easy solution to this (perhaps an mxj object?), but a sort of brute force method would be to convert the number to a list of binary values, reverse the list, and convert it back to decimal. There was a thread about decimal to binary conversion a while back that might be helpful:

http://www.cycling74.com/forums/index.php?t=rview&th=35324&rid=7743

#143891
Nov 3, 2008 at 9:13am

hi james,

the [bitlist] external in my collection offers the two bit ordering
modes you are looking for.
to get the reversal you convert an int to binary using one mode and
convert back using the other.

the following patch also shows how to convert to and from a list of
bits using iterative bit-shifting and masking using stock objects only.

my object library can be downloaded here: http://www.jasch.ch/dl/

hth

/*j

max 4.x version below

– Pasted Max Patch, click to expand. –

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P message 336 225 79 196617 0 0 0 1 0 0 1 1;
#P newex 336 205 62 196617 prepend set;
#P message 446 224 79 196617 1 1 0 0 1 0 0 0;
#P newex 446 204 62 196617 prepend set;
#P number 436 287 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 326 287 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 352 256 1058 702;
#P window setfont “Sans Serif” 9.;
#P number 249 319 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P message 249 81 79 196617 0 0 0 0 1 1 1 1;
#P newex 249 295 49 196617 +;
#P newex 288 273 49 196617 +;
#P newex 327 251 49 196617 +;
#P newex 366 229 49 196617 +;
#P newex 405 207 49 196617 +;
#P newex 444 185 49 196617 +;
#P newex 483 163 49 196617 +;
#P newex 522 140 27 196617 * 1;
#P newex 483 140 27 196617 * 2;
#P newex 444 140 27 196617 * 4;
#P newex 405 140 27 196617 * 8;
#P newex 366 140 32 196617 * 16;
#P newex 327 140 32 196617 * 32;
#P newex 288 140 32 196617 * 64;
#P newex 249 140 38 196617 * 128;
#P newex 81 196 37 196617 pow 2;
#P newex 81 174 37 196617 t 2 i;
#N counter;
#X flags 0 0;
#P newobj 81 150 66 196617 counter;
#P newex 64 126 27 196617 t l b;
#P newex 249 109 286 196617 unpack 0 0 0 0 0 0 0 0;
#P newex 64 297 27 196617 +;
#P newex 81 273 27 196617 i;
#P newex 64 245 27 196617 t i b;
#P newex 64 221 27 196617 *;
#P newex 64 101 47 196617 zl iter 1;
#P window linecount 0;
#P newex 64 73 40 196617 t l b 0;
#P outlet 64 336 15 0;
#P inlet 64 46 15 0;
#P connect 0 0 2 0;
#P connect 2 0 3 0;
#P connect 3 0 9 0;
#P connect 9 0 4 0;
#P connect 4 0 5 0;
#P connect 5 0 7 0;
#P connect 7 0 1 0;
#P connect 9 1 10 0;
#P connect 10 0 11 0;
#P connect 11 0 12 0;
#P connect 12 0 4 1;
#P connect 5 1 6 0;
#P connect 6 0 7 1;
#P fasten 2 2 6 1 99 94 156 94 156 258 103 258;
#P fasten 7 0 6 1 69 318 134 318 134 263 103 263;
#P connect 11 1 12 1;
#P fasten 2 1 10 2 84 97 114 97;
#P connect 28 0 8 0;
#P connect 8 0 13 0;
#P connect 13 0 27 0;
#P connect 27 0 29 0;
#P connect 8 1 14 0;
#P connect 14 0 26 0;
#P connect 26 0 27 1;
#P connect 8 2 15 0;
#P connect 15 0 25 0;
#P connect 25 0 26 1;
#P connect 8 3 16 0;
#P connect 16 0 24 0;
#P connect 24 0 25 1;
#P connect 8 4 17 0;
#P connect 17 0 23 0;
#P connect 23 0 24 1;
#P connect 8 5 18 0;
#P connect 18 0 22 0;
#P connect 22 0 23 1;
#P connect 8 6 19 0;
#P connect 19 0 21 0;
#P connect 21 0 22 1;
#P connect 8 7 20 0;
#P connect 20 0 21 1;
#P pop;
#P newobj 436 254 67 196617 p to_decimal;
#N vpatcher 352 256 1058 702;
#P window setfont “Sans Serif” 9.;
#P number 249 319 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P message 249 81 79 196617 0 0 0 0 1 1 1 1;
#P newex 249 295 49 196617 +;
#P newex 288 273 49 196617 +;
#P newex 327 251 49 196617 +;
#P newex 366 229 49 196617 +;
#P newex 405 207 49 196617 +;
#P newex 444 185 49 196617 +;
#P newex 483 163 49 196617 +;
#P newex 522 140 27 196617 * 1;
#P newex 483 140 27 196617 * 2;
#P newex 444 140 27 196617 * 4;
#P newex 405 140 27 196617 * 8;
#P newex 366 140 32 196617 * 16;
#P newex 327 140 32 196617 * 32;
#P newex 288 140 32 196617 * 64;
#P newex 249 140 38 196617 * 128;
#P newex 81 196 37 196617 pow 2;
#P newex 81 174 37 196617 t 2 i;
#N counter;
#X flags 0 0;
#P newobj 81 150 66 196617 counter;
#P newex 64 126 27 196617 t l b;
#P newex 249 109 286 196617 unpack 0 0 0 0 0 0 0 0;
#P newex 64 297 27 196617 +;
#P newex 81 273 27 196617 i;
#P newex 64 245 27 196617 t i b;
#P newex 64 221 27 196617 *;
#P newex 64 101 47 196617 zl iter 1;
#P window linecount 0;
#P newex 64 73 40 196617 t l b 0;
#P outlet 64 336 15 0;
#P inlet 64 46 15 0;
#P connect 0 0 2 0;
#P connect 2 0 3 0;
#P connect 3 0 9 0;
#P connect 9 0 4 0;
#P connect 4 0 5 0;
#P connect 5 0 7 0;
#P connect 7 0 1 0;
#P connect 9 1 10 0;
#P connect 10 0 11 0;
#P connect 11 0 12 0;
#P connect 12 0 4 1;
#P connect 5 1 6 0;
#P connect 6 0 7 1;
#P fasten 7 0 6 1 69 318 134 318 134 263 103 263;
#P fasten 2 2 6 1 99 94 156 94 156 258 103 258;
#P connect 11 1 12 1;
#P fasten 2 1 10 2 84 97 114 97;
#P connect 28 0 8 0;
#P connect 8 0 13 0;
#P connect 13 0 27 0;
#P connect 27 0 29 0;
#P connect 8 1 14 0;
#P connect 14 0 26 0;
#P connect 26 0 27 1;
#P connect 8 2 15 0;
#P connect 15 0 25 0;
#P connect 25 0 26 1;
#P connect 8 3 16 0;
#P connect 16 0 24 0;
#P connect 24 0 25 1;
#P connect 8 4 17 0;
#P connect 17 0 23 0;
#P connect 23 0 24 1;
#P connect 8 5 18 0;
#P connect 18 0 22 0;
#P connect 22 0 23 1;
#P connect 8 6 19 0;
#P connect 19 0 21 0;
#P connect 21 0 22 1;
#P connect 8 7 20 0;
#P connect 20 0 21 1;
#P pop;
#P newobj 326 254 67 196617 p to_decimal;
#P newex 326 179 37 196617 zl rev;
#N vpatcher 352 256 1058 702;
#P window setfont “Sans Serif” 9.;
#P message 249 359 238 196617 0 0 0 0 1 1 1 1;
#P window linecount 1;
#P newex 249 338 62 196617 prepend set;
#P newex 249 311 245 196617 pack 0 0 0 0 0 0 0 0;
#P newex 249 290 27 196617 & 1;
#P newex 249 266 29 196617 >> 1;
#P newex 282 265 27 196617 & 1;
#P newex 282 241 29 196617 >> 1;
#P newex 315 240 27 196617 & 1;
#P newex 315 216 29 196617 >> 1;
#P newex 348 215 27 196617 & 1;
#P newex 348 191 29 196617 >> 1;
#P newex 381 190 27 196617 & 1;
#P newex 381 166 29 196617 >> 1;
#P newex 414 165 27 196617 & 1;
#P newex 414 141 29 196617 >> 1;
#P newex 480 115 27 196617 & 1;
#P newex 447 140 27 196617 & 1;
#P newex 447 116 29 196617 >> 1;
#P number 480 82 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 136 72 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 136 48 60 196617 loadmess 8;
#P newex 64 216 55 196617 zl group 8;
#P newex 64 182 27 196617 & 1;
#P newex 64 158 40 196617 >> 1;
#P newex 64 135 52 196617 i;
#P newex 64 105 41 196617 uzi 8 0;
#P window linecount 0;
#P newex 64 73 52 196617 t b i;
#P outlet 64 265 15 0;
#P inlet 64 46 15 0;
#P comment 182 75 129 196617 number of bits to generate;
#P connect 1 0 3 0;
#P connect 3 0 4 0;
#P connect 4 0 5 0;
#P connect 5 0 6 0;
#P connect 6 0 7 0;
#P connect 7 0 8 0;
#P fasten 4 1 8 0 84 128 54 128 54 207 69 207;
#P connect 8 0 2 0;
#P connect 4 2 6 1;
#P connect 10 0 4 1;
#P connect 3 1 5 1;
#P connect 10 0 8 1;
#P connect 9 0 10 0;
#P connect 23 0 25 0;
#P connect 25 0 26 0;
#P connect 26 0 27 0;
#P connect 27 0 28 0;
#P hidden connect 28 0 29 0;
#P connect 21 0 23 0;
#P connect 23 0 24 0;
#P connect 24 0 27 1;
#P connect 19 0 21 0;
#P connect 21 0 22 0;
#P connect 22 0 27 2;
#P connect 17 0 19 0;
#P connect 19 0 20 0;
#P connect 20 0 27 3;
#P connect 15 0 17 0;
#P connect 17 0 18 0;
#P connect 18 0 27 4;
#P connect 12 0 15 0;
#P connect 15 0 16 0;
#P connect 16 0 27 5;
#P connect 11 0 12 0;
#P connect 12 0 13 0;
#P connect 13 0 27 6;
#P connect 11 0 14 0;
#P connect 14 0 27 7;
#P pop;
#P newobj 436 158 62 196617 p to_binary;
#N vpatcher 352 256 1058 702;
#P window setfont “Sans Serif” 9.;
#P message 249 359 238 196617 0 0 0 0 1 1 1 1;
#P window linecount 1;
#P newex 249 338 62 196617 prepend set;
#P newex 249 311 245 196617 pack 0 0 0 0 0 0 0 0;
#P newex 249 290 27 196617 & 1;
#P newex 249 266 29 196617 >> 1;
#P newex 282 265 27 196617 & 1;
#P newex 282 241 29 196617 >> 1;
#P newex 315 240 27 196617 & 1;
#P newex 315 216 29 196617 >> 1;
#P newex 348 215 27 196617 & 1;
#P newex 348 191 29 196617 >> 1;
#P newex 381 190 27 196617 & 1;
#P newex 381 166 29 196617 >> 1;
#P newex 414 165 27 196617 & 1;
#P newex 414 141 29 196617 >> 1;
#P newex 480 115 27 196617 & 1;
#P newex 447 140 27 196617 & 1;
#P newex 447 116 29 196617 >> 1;
#P number 480 82 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 136 72 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 136 48 60 196617 loadmess 8;
#P newex 64 216 55 196617 zl group 8;
#P newex 64 182 27 196617 & 1;
#P newex 64 158 40 196617 >> 1;
#P newex 64 135 52 196617 i;
#P newex 64 105 41 196617 uzi 8 0;
#P window linecount 0;
#P newex 64 73 52 196617 t b i;
#P outlet 64 265 15 0;
#P inlet 64 46 15 0;
#P comment 182 75 129 196617 number of bits to generate;
#P connect 1 0 3 0;
#P connect 3 0 4 0;
#P connect 4 0 5 0;
#P connect 5 0 6 0;
#P connect 6 0 7 0;
#P fasten 4 1 8 0 84 128 54 128 54 207 69 207;
#P connect 7 0 8 0;
#P connect 8 0 2 0;
#P connect 4 2 6 1;
#P connect 10 0 4 1;
#P connect 3 1 5 1;
#P connect 10 0 8 1;
#P connect 9 0 10 0;
#P connect 23 0 25 0;
#P connect 25 0 26 0;
#P connect 26 0 27 0;
#P connect 27 0 28 0;
#P hidden connect 28 0 29 0;
#P connect 21 0 23 0;
#P connect 23 0 24 0;
#P connect 24 0 27 1;
#P connect 19 0 21 0;
#P connect 21 0 22 0;
#P connect 22 0 27 2;
#P connect 17 0 19 0;
#P connect 19 0 20 0;
#P connect 20 0 27 3;
#P connect 15 0 17 0;
#P connect 17 0 18 0;
#P connect 18 0 27 4;
#P connect 12 0 15 0;
#P connect 15 0 16 0;
#P connect 16 0 27 5;
#P connect 11 0 12 0;
#P connect 12 0 13 0;
#P connect 13 0 27 6;
#P connect 11 0 14 0;
#P connect 14 0 27 7;
#P pop;
#P newobj 326 158 62 196617 p to_binary;
#P message 169 224 79 196617 1 1 0 0 1 0 0 0;
#P newex 169 204 62 196617 prepend set;
#P message 74 224 79 196617 0 0 0 1 0 0 1 1;
#P newex 74 204 62 196617 prepend set;
#P number 62 287 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 159 287 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 159 254 55 196617 bitlist 0 8;
#P newex 62 254 55 196617 bitlist 0 8;
#P newex 159 158 55 196617 bitlist 0 8;
#P newex 62 158 55 196617 bitlist 1 8;
#P number 62 110 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 62 89 66 196617 loadmess 19;
#P connect 0 0 1 0;
#P fasten 1 0 2 0 67 141 67 141;
#P connect 2 0 4 0;
#P connect 4 0 7 0;
#P connect 2 0 8 0;
#P hidden connect 8 0 9 0;
#P fasten 1 0 3 0 67 141 164 141;
#P connect 3 0 5 0;
#P connect 5 0 6 0;
#P connect 3 0 10 0;
#P hidden connect 10 0 11 0;
#P fasten 1 0 12 0 67 141 331 141;
#P connect 12 0 14 0;
#P connect 14 0 15 0;
#P connect 15 0 17 0;
#P connect 14 0 21 0;
#P hidden connect 21 0 22 0;
#P fasten 1 0 13 0 67 141 441 141;
#P connect 13 0 16 0;
#P connect 16 0 18 0;
#P connect 13 0 19 0;
#P hidden connect 19 0 20 0;
#P window clipboard copycount 23;

#143892
Nov 3, 2008 at 9:13am

An example with standard objects:

– Pasted Max Patch, click to expand. –

_
johan

#143893
Nov 3, 2008 at 3:55pm

johan,

Your solution is very interesting, it seems that the radiogroup object will do the binary/list translation pretty much by itself. Makes the complex solutions posted by Stephen Lee seem redundant:

http://www.cycling74.com/forums/index.php?t=rview&th=35324&rid=7743

The only maths in your patch is to resize the radiogroup objects depending on how many bits in the incoming integer. Since I am only dealing with bytes I can just leave the radiogroup objects at 8 long.

Max is restricted to 32bit integers, is it not? So you could just leave the radiogroup objects at 32 long and be done with it.

Thanks, James.

#143894
Nov 3, 2008 at 4:54pm

Another problem for me would be that the radiogroup is a gui element. I avoid those like the plague…

Although jasch rules, can’t you just:

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 186 125 40 196617 itoa;
#P newex 186 105 37 196617 zl rev;
#P newex 186 148 32 196617 print;
#P message 186 66 46 196617 111010;
#P newex 186 85 40 196617 atoi;
#P connect 4 0 2 0;
#P connect 3 0 4 0;
#P connect 0 0 3 0;
#P connect 1 0 0 0;
#P window clipboard copycount 5;

?

#143895
Nov 3, 2008 at 7:12pm

Quote: Bas van der Graaff wrote on Mon, 03 November 2008 09:54
—————————————————-
> Another problem for me would be that the radiogroup is a gui element. I avoid those like the plague…

I agree, but it does work with the least amount of objects.

Your example doesn’t work for numbers.

#143896
Nov 4, 2008 at 10:08am

You mean leading zeros? Correct, hehe.
It’s near impossible to treat those correctly in max, even things like [sprintf "%s"] and [unpack s] like to remove them. Explicit annoyance.

#143897
Nov 4, 2008 at 1:45pm

Hmm, are you sure? Even when i use [printit] directly after [sprintf symout %.3d], i’ve already lost my leading zeroes.

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 573 325 39 196617 printit;
#P newex 537 325 32 196617 print;
#P message 473 363 50 196617 111;
#P newex 473 325 62 196617 prepend set;
#P message 473 178 46 196617 111;
#P newex 473 236 103 196617 sprintf symout %.3d;
#P connect 0 0 2 0;
#P connect 0 0 4 0;
#P connect 0 0 5 0;
#P connect 1 0 0 0;
#P connect 2 0 3 0;
#P window clipboard copycount 6;

Mind you, i don’t need this for my patch or anything, just generally interested how things work. Personally, I might prefer more strict use of datatypes, as things like these are hardly intuitive, but it remains to be tested how this influences workflow…

#143899
Nov 4, 2008 at 2:00pm

My proposal for Max 6 (seriously):

1 -> int
0.1 -> float
1 0.1 -> list of int and float
“hello” -> string
“hello world” -> string
“hello” “world” -> list of two strings
hello -> message / keyword
hello world -> list of two messages
hello “world” -> list of message and string

Special cases:
000111 -> int (equal to 111)
00.0111 -> float (equal to 0.0111)
“000111″ -> string

- if an object doesn’t understand a keyword, it throws an error
- we lose the term ‘symbol’

Mattijs

Quote: Bas van der Graaff wrote on Tue, 04 November 2008 14:45
—————————————————-
> Personally, I might prefer more strict use of datatypes, as things like these are hardly intuitive, but it remains to be tested how this influences workflow…
—————————————————-

#143900
Nov 4, 2008 at 3:14pm

Quote: Bas van der Graaff wrote on Tue, 04 November 2008 03:08
—————————————————-
> You mean leading zeros? Correct, hehe.

No, I mean numbers, integers. You are reversing a message.

You are taking message that looks like the binary representation of a number and reversing it.

Have a look at johan’s patch and see how it differs form yours.

The real revelation for me here is the radiogroup object. It seems to do the job of converting a number into a list of the binary version straight off. I agree that it’s a shame that it’s a gui object, also a shame that you need to know how many bits in your number, but it still seems that this is the best solution for this problem.

#143902
Nov 4, 2008 at 3:22pm

Quote: Emmanuel Jourdan wrote on Tue, 04 November 2008 08:14
—————————————————-
> Am I missing something?

Yes. We’re talking about numbers here, not messages that look like numbers.

#143903
Nov 4, 2008 at 3:54pm

Mattijs Kneppers schrieb:
> My proposal for Max 6 (seriously):
>
> 1 -> int
> 0.1 -> float
> 1 0.1 -> list of int and float
> “hello” -> string
> “hello world” -> string
> “hello” “world” -> list of two strings
> hello -> message / keyword
> hello world -> list of two messages
> hello “world” -> list of message and string
>
> Special cases:
> 000111 -> int (equal to 111)
> 00.0111 -> float (equal to 0.0111)
> “000111″ -> string

This would mix a bit the difference between presentation of a number (or
visualisation) and the number itself, it would create an overhead not
worth it. Each simple number would have to carry a presentation along
with it…
I doubt that anybody would want her patches 100% broken when switching
from Max 5.x to Max 6, though computing power might be so much higher,
that you don’t know anymore how to burn it…

It is like with going from C to C++, it had to be a new language.
(And the world is still using this bad hack of a language called C…)

Stefan

Stefan Tiedje————x——-
–_____———–|————–
–(_|_ —-|—–|—–()——-
– _|_)—-|—–()————–
———-()——–www.ccmix.com

#143904
Nov 4, 2008 at 4:44pm

Presentation? O.o?

Internally, all variables have C data types already, so the only difference would be that you could get rid of all the excess automatic conversion, only objects presented with incorrect data types would generate an error.

You’re correct by stating some patches may be broken in Max 6, but the errors will be easy to trace and fix by adding a simple [i], [f] or [str] object for typeconversion. Most of your data is type-specific, only rarely one datatype is used as another.

Of course, these are only ideas, but can’t harm to discuss whether it needs change, as it’s clearly not very easy-to-use at the moment.

Quote: stefantiedje wrote on Tue, 04 November 2008 16:54
—————————————————-
> This would mix a bit the difference between presentation of a number (or
> visualisation) and the number itself, it would create an overhead not
> worth it. Each simple number would have to carry a presentation along
> with it…
> I doubt that anybody would want her patches 100% broken when switching
> from Max 5.x to Max 6, though computing power might be so much higher,
> that you don’t know anymore how to burn it…
>
> It is like with going from C to C++, it had to be a new language.
> (And the world is still using this bad hack of a language called C…)
>
> Stefan
>

#143905

You must be logged in to reply to this topic.