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 › Bit Fields- constructing
Page Index Toggle Pages: 1
Bit Fields- constructing? (Read 700 times)
Bit Fields- constructing?
Sep 1st, 2008, 12:39am
 
Hello,
I was wondering if anyone could give me a good explanation for how to create bit fields in Processing/Java (assuming they work the same in this area). I've searched the internet, but most of the results involve C or some variant thereof, and leave me hopelessly confused.  : p

Also, what's the best method for viewing a single bit from a a bit field?

Cheers,
Gordon
Re: Bit Fields- constructing?
Reply #1 - Sep 1st, 2008, 10:33am
 
Yes, you will do them in Java, since Processing is Java (with a bit of syntactic sugar over it).
I will just point to the reference: java.util.BitSet since I haven't used them yet.
Of course, such structure is useful only because it can save some memory: it is much faster (I think) to use a simple array of booleans if you have less than some millions of bits (or if you work in a memory constrained environment).
Re: Bit Fields- constructing?
Reply #2 - Sep 1st, 2008, 2:09pm
 
it does work like java, which is to say it's kinda gross. for instance, to check the 2^2 bit:

int something = 2342345;
if ((something & 4) != 0) {
 // blah
}

in c/c++ that would be
if (something & 4) {
 // blah
}

the shift operator is also useful:
int bits0and1and4 = (1 << 4) | (1 << 1) | (1 << 0);

checking particular bits is most easily done with the shift operator:

boolean checkbit(int value, int index) {
 return (value & (1 << index)) != 0;
}

also the binary() and unbinary() functions are helpful to see what's going on.

you can also just use 'boolean' if you don't care about space, but keep in mind that a single boolean in java is the same size as an int (at least 32 bits).

or BitSet will let you play with zillions of em.
Re: Bit Fields- constructing?
Reply #3 - Sep 1st, 2008, 7:28pm
 
Thanks both, I just couldn't figure out why the shift operators were there for before.

One thing though, considering a byte has 8 bits, shouldn't it be able to hold a max value of 255? Or is the 8th bit for creating a negative value?
Re: Bit Fields- constructing?
Reply #4 - Sep 1st, 2008, 8:04pm
 
Quote:
One thing though, considering a byte has 8 bits, shouldn't it be able to hold a max value of 255? Or is the 8th bit for creating a negative value?

both! Smiley

the eighth bit is the sign bit (java has no unsigned types), so a byte has the range -128 to 127. to convert it to a value between 0 and 255, use this:

byte b = -120;
int a = b & 0xff;

what's happening here is that you cast it to an int, which is 32 bits. doing this does a "sign extension" which means all 24 of the new bits are all 1s. then, you use the & operator to grab just the bottom 8 bits (0xff == 255 == 11111111). because it's still an int, it's now a number between 0 and 255.

if that doesn't make sense, read it six times, going more slowly each time. or draw a picture of it. or at least that's what worked for me.

or wikipedia has more:
http://en.wikipedia.org/wiki/Two's_complement
Re: Bit Fields- constructing?
Reply #5 - Sep 1st, 2008, 8:59pm
 
Wow. Thanks for that, it all makes sense now.
Page Index Toggle Pages: 1