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

[sc-users] small questions on bitwise and asBinaryDigits



Hi all,

I have some questions regarding bitwise analysis of integers. First I want to sum up the number of binary one's of a given integer and I thought 'haha, there are these really convenient methods..' like .asBinaryDigits, which returns an Array of the binary sequence and .sum, which would return me what I wanted to compute. Now I wonder why .asBinaryDigits returns an Array of 8bits, while it should be 16, if I am not mistaken.

4.asBinaryDigits      //gives

[ 0, 0, 0, 0, 0, 1, 0, 0 ]


Could someone explain why its only 8bits? Next, I was trying to find a way to get the number of one's in the binary sequence of an integer by comparing them bitwise, but I am struggling to that. Maybe someone could explain how? This is the code I have been tinkering about with:

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

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

Any hints?

Happy cake and coffee time!

Karsten