Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of bitset

 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May I kow the use of a bitset?

I find it difficult to understand what is bitset even after going through the API

Thanks in Advance.


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It represents binary bits (if you don't know what a bit is, check this). For example, imagine that you have the following byte : 00001100 (=12 in decimal). You can represent it like this :

The ouput is {2, 3}, which tells you that bits 2 and 3 are on.

Imagine that you have another byte 00001000, and you want to "and" its value with the first byte. The BitSet class allows you to do the basic logical functions "and", "or", "xor".

The output is now {3}, because bit 2 was turned off during the "and" operation.

(I don't remember having used this class ever before)
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bits are obviously numbered from right to left, starting at 0, so the least significant bit in that example is no 0 and the most significant bit is no 7.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic