how do Bitsets work?

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-26-2013, 12:52 PM
outsorts
how do Bitsets work?
i have looked at an example for Bitsets in here : Java - The BitSet Class
but the output doesn't seem to do anything with logic gates.
so how exactly do they work?
• 11-26-2013, 01:07 PM
JosAH
Re: how do Bitsets work?
BitSets are more stupid than gates: they're just a set of bits where each bit has an index (as with arrays) and a value, 0 or 1. The class allows you to manipulate the bits in several ways, that's all.

kind regards,

Jos
• 11-26-2013, 01:23 PM
outsorts
Re: how do Bitsets work?
they don't look useful at all for me , but just for knowledge could you give me an example of what happens in an "and operation" with 2 BitSets?
• 11-26-2013, 01:41 PM
JosAH
Re: how do Bitsets work?
They can be quite useful: I use bit sets for a list of SMS messages (coming from home alarm units); every message has an index number; a bit set represents all present messages at a certain time T_i; if at time T_i+1 the (new) bit set has one or more bits set to 1, not present in the set for time T_i, I know that new messages have arrived from the home alarm units ... I use the andNot( ... ) method for this purpose.

kind regards,

Jos
• 11-26-2013, 02:05 PM
gimbal2
Re: how do Bitsets work?
Quote:

Originally Posted by JosAH
They can be quite useful: I use bit sets for a list of SMS messages (coming from home alarm units); every message has an index number; a bit set represents all present messages at a certain time T_i; if at time T_i+1 the (new) bit set has one or more bits set to 1, not present in the set for time T_i, I know that new messages have arrived from the home alarm units ... I use the andNot( ... ) method for this purpose.

Very clever. I'd just use a previous and a current integer counter to be honest ;)
• 11-26-2013, 03:14 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
Very clever. I'd just use a previous and a current integer counter to be honest ;)

That doesn't work: those SMS messages get an index number; if the latest 10 message were, say, 1, 2, 3, 4 ... 10 but now the 10 latest messages are 6, 7, 8, 9 ... 15, those bit sets tell me that 11, 12, 13, 14, 15 are the new messages ...

kind regards,

Jos
• 11-26-2013, 03:36 PM
gimbal2
Re: how do Bitsets work?
Ah okay, I missed the part where you know exactly which messages are new :) Indeed my solution would only tell you that there are new ones and then you still have to find them.

VERY clever.
• 11-26-2013, 03:49 PM
Tolls
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
VERY clever.

Oh don't.
He can barely get through the door with his ego as it is!
• 11-26-2013, 03:53 PM
gimbal2
Re: how do Bitsets work?
Quote:

Originally Posted by Tolls
Oh don't.
He can barely get through the door with his ego as it is!

I like to see him struggle and kick as he tries though.
• 11-26-2013, 03:55 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
Ah okay, I missed the part where you know exactly which messages are new :) Indeed my solution would only tell you that there are new ones and then you still have to find them.

VERY clever.

Think of the chip that receives those SMS messages as the core of a cell phone: it assigns pseudo random index values to each received message (it's the index in a small array in the memory of the SIM chip).

kind regards,

Jos
• 11-26-2013, 03:56 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by Tolls
Oh don't.
He can barely get through the door with his ego as it is!

Gimbal is a nice guy and you are note; so there :-P

*grmbl*

Jos
• 11-26-2013, 03:58 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
I like to see him struggle and kick as he tries though.

You and Tolls are not nice to me anymore; mommy!

*grmbl*

Jos
• 11-26-2013, 04:02 PM
gimbal2
Re: how do Bitsets work?
There you go, now you're all caught up with the thread :p

I'm your friendly neighborhood ahole! F you in the A, and have a nice day!
• 11-26-2013, 04:14 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
There you go, now you're all caught up with the thread :p

I'm your friendly neighborhood ahole! F you in the A, and have a nice day!

My finger is dangling above the rotating knives machine button ...

<diabolical laughter/>

Jos
• 11-26-2013, 05:01 PM
gimbal2
Re: how do Bitsets work?
To do what? Cut an apple?
• 11-26-2013, 05:02 PM
Tolls
Re: how do Bitsets work?
Quote:

Originally Posted by JosAH
My finger is dangling above the rotating knives machine button ...

<diabolical laughter/>

Jos

<imp-on-shoulder>do it...Do It...DO IT!</imp-on-shoulder>
• 11-26-2013, 05:26 PM
jim829
Re: how do Bitsets work?
It's amazing how this forum leads one to learn. I knew how BitSet was implemented and could either be a single primitive or an array of primitives depending on the size of the bit set. But when I was looking at the code, it still did a bit shift of the specified index, without scaling, regardless of the index size. Here's the set method from the BitSet class:
Code:

```public void set(int bitIndex) {         if (bitIndex < 0)             throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);         int wordIndex = wordIndex(bitIndex);         expandTo(wordIndex);         words[wordIndex] |= (1L << bitIndex); // Restores invariants         checkInvariants();     }```
After plowing thru my books (including a Sun published SCJP guide), I had to check out the language spec to find out that only the 5 or 6 low order bits (depending on int vs long conversion) are used in the shift distances. I could not find this in my books nor in the tutorials. Ref: Chapter*15.*Expressions.

Regards,
Jim
• 11-26-2013, 05:47 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by gimbal2
To do what? Cut an apple?

This exlplains it all in detail: Monty Python- Architect Sketch - YouTube

kindest regards,

Jos
• 11-26-2013, 06:15 PM
JosAH
Re: how do Bitsets work?
Quote:

Originally Posted by jim829
It's amazing how this forum leads one to learn. I knew how BitSet was implemented and could either be a single primitive or an array of primitives depending on the size of the bit set. But when I was looking at the code, it still did a bit shift of the specified index, without scaling, regardless of the index size. Here's the set method from the BitSet class:
Code:

```public void set(int bitIndex) {         if (bitIndex < 0)             throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);         int wordIndex = wordIndex(bitIndex);         expandTo(wordIndex);         words[wordIndex] |= (1L << bitIndex); // Restores invariants         checkInvariants();     }```
After plowing thru my books (including a Sun published SCJP guide), I had to check out the language spec to find out that only the 5 or 6 low order bits (depending on int vs long conversion) are used in the shift distances. I could not find this in my books nor in the tutorials. Ref: Chapter*15.*Expressions.

It's defined in the JLS (see paragraph 15.19).

kind regards,

Jos
• 11-26-2013, 06:16 PM
gimbal2
Re: how do Bitsets work?
Quote:

Originally Posted by jim829
It's amazing how this forum leads one to learn.

That's because it is visited by extremely cool, intelligent and inspiring people like JosAH.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last