We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › direct binary use request
Page Index Toggle Pages: 1
direct binary use request (Read 805 times)
direct binary use request
Feb 10th, 2007, 12:20am
 
It would be nice in some cases if binary could be used directly.

example:

byte data = B10101010;

just a thought.
Re: direct binary use request
Reply #1 - Feb 10th, 2007, 4:22pm
 
It can, and it is. All data in your computer is already being used directly as binary. I built my GA library on this principle.

Code:

int myInt = 15;
println(binary(myInt));
byte myBin = (byte)unbinary("1000");
println(myBin);


So long as your int or byte is an arbitrary variable you can stop thinking of it as an int or byte and start treating it like a binary.

The following page has an excellent range of methods for performing tasks in binary.

http://graphics.stanford.edu/~seander/bithacks.html

Most useful is discovering the log base 2 of a binary. That's basically the position of the highest bit set, or how many digits that are in your binary number. The following function is from the above page and is far faster than using Processing's float based methods to get the same answer.

Code:

final int [] MultiplyDeBruijnBitPosition = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4,8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
static final int bitLog(int num){
num |= num >> 1; // first round down to power of 2
num |= num >> 2;
num |= num >> 4;
num |= num >> 8;
num |= num >> 16;
num = (num >> 1) + 1;
return MultiplyDeBruijnBitPosition[(num * 0x077CB531) >> 27];
}
Page Index Toggle Pages: 1