# Thread: how do Bitsets work?

1. Member
Join Date
May 2013
Posts
4
Rep Power
0

## 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?

2. ## 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

3. Member
Join Date
May 2013
Posts
4
Rep Power
0

## 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?

4. ## 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

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: how do Bitsets work?

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 ;)

6. ## Re: how do Bitsets work?

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

7. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

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

8. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

## Re: how do Bitsets work?

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

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: how do Bitsets work?

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.

10. ## Re: how do Bitsets work?

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. ## Re: how do Bitsets work?

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

12. ## Re: how do Bitsets work?

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

13. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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!

14. ## Re: how do Bitsets work?

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

15. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: how do Bitsets work?

To do what? Cut an apple?

16. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

## Re: how do Bitsets work?

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>

17. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,837
Rep Power
10

## 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:
Java 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

18. ## Re: how do Bitsets work?

Originally Posted by gimbal2
To do what? Cut an apple?
This exlplains it all in detail: Monty Python- Architect Sketch - YouTube

kindest regards,

Jos

19. ## Re: how do Bitsets work?

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:
Java 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

20. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: how do Bitsets work?

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.

Page 1 of 2 12 Last