[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] small questions on bitwise and asBinaryDigits



On Dec 26, 2006, at 10:59 AM, karlos wrote:

Tdef(\test,{
    16.do({
        arg i;
        var pitches = 4;
        Post << (pitches & i) << " ";
    })  
}).play  
 
//returns

a Tdef
0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4 

pitches == 0100

so you have

0100 & 0000 = 0
0100 & 0001 = 0
0100 & 0010 = 0
0100 & 0011 = 0
0100 & 0100 = 4
0100 & 0101 = 4
0100 & 0110 = 4
0100 & 0111 = 4

What was the desired output?

//then, as someone suggested, I used the 2^n to look for each bit

Tdef(\test,{
    16.do({
        arg i;
        var pitches = 4;
        Post << (pitches & (2**i)) << " ";
    })  
}).play 

//returns

a Tdef
ERROR:
Math operation failed.

IIRC bitwise operators work only on integers, but the result of ** is always a float.

(2**2).dump
   Float 4.0   40100000 00000000
4

So you should do (2**i).asInteger or, more efficient, (1 << i) which is a bitwise shift.

8.do({ |i| (1 << i).postln });
1
2
4
8
16
32
64
128

hjh


: H. James Harkins

: jamshark70@xxxxxxxxxxxxxxxxx

: http://www.dewdrop-world.net

.::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:


"Come said the Muse,

Sing me a song no poet has yet chanted,

Sing me the universal."  -- Whitman