How to make BitSet to be not simplified [JAVA]











up vote
3
down vote

favorite
1












I am using BitSet to represent possible hours where lectures are filled in, the case is that when you put to false the corner bits, they are simplified, this means that they are not anymore in the BitSet. How can I ask BitSet to not simplify?



To make my explanation clearer this is the code:



   for(Map.Entry<GrupAssig, BitSet> entry : bitsetPerGrup.entrySet()){

BitSet bitset = entry.getValue();

//n franges per dia
int numFranges = UnitatDocent.getNumFranges();
int indexDia = this.dia.id() * numFranges;

bitset.clear(indexDia, indexDia+numFranges);
}


Imagine that the bitset has 60 bits by default, and numFranges=12 and this.dia.id()=4. This would make the last twelve bits set to 0. The result I get is:



111111111111111111111111111111111111111111111111



But if this.dia.id()=3 I get:



11111111111111111111111111111111111100000000000011111111111



And you can print the BitSet this way:



    public static void printBitset(BitSet b) {
StringBuilder s = new StringBuilder();
for( int i = 0; i < b.length(); i++ )
{
s.append( b.get( i ) == true ? 1 : 0 );
}

System.out.println( s );
}


Which demonstrates what I am saying.



Thank you.










share|improve this question
























  • What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
    – Michael Krause
    Nov 10 at 18:27












  • Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
    – Louis Wasserman
    Nov 10 at 18:31










  • Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
    – Marc43
    Nov 10 at 18:34










  • Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
    – Marc43
    Nov 10 at 18:39












  • Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
    – Michael Krause
    Nov 10 at 18:55















up vote
3
down vote

favorite
1












I am using BitSet to represent possible hours where lectures are filled in, the case is that when you put to false the corner bits, they are simplified, this means that they are not anymore in the BitSet. How can I ask BitSet to not simplify?



To make my explanation clearer this is the code:



   for(Map.Entry<GrupAssig, BitSet> entry : bitsetPerGrup.entrySet()){

BitSet bitset = entry.getValue();

//n franges per dia
int numFranges = UnitatDocent.getNumFranges();
int indexDia = this.dia.id() * numFranges;

bitset.clear(indexDia, indexDia+numFranges);
}


Imagine that the bitset has 60 bits by default, and numFranges=12 and this.dia.id()=4. This would make the last twelve bits set to 0. The result I get is:



111111111111111111111111111111111111111111111111



But if this.dia.id()=3 I get:



11111111111111111111111111111111111100000000000011111111111



And you can print the BitSet this way:



    public static void printBitset(BitSet b) {
StringBuilder s = new StringBuilder();
for( int i = 0; i < b.length(); i++ )
{
s.append( b.get( i ) == true ? 1 : 0 );
}

System.out.println( s );
}


Which demonstrates what I am saying.



Thank you.










share|improve this question
























  • What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
    – Michael Krause
    Nov 10 at 18:27












  • Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
    – Louis Wasserman
    Nov 10 at 18:31










  • Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
    – Marc43
    Nov 10 at 18:34










  • Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
    – Marc43
    Nov 10 at 18:39












  • Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
    – Michael Krause
    Nov 10 at 18:55













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I am using BitSet to represent possible hours where lectures are filled in, the case is that when you put to false the corner bits, they are simplified, this means that they are not anymore in the BitSet. How can I ask BitSet to not simplify?



To make my explanation clearer this is the code:



   for(Map.Entry<GrupAssig, BitSet> entry : bitsetPerGrup.entrySet()){

BitSet bitset = entry.getValue();

//n franges per dia
int numFranges = UnitatDocent.getNumFranges();
int indexDia = this.dia.id() * numFranges;

bitset.clear(indexDia, indexDia+numFranges);
}


Imagine that the bitset has 60 bits by default, and numFranges=12 and this.dia.id()=4. This would make the last twelve bits set to 0. The result I get is:



111111111111111111111111111111111111111111111111



But if this.dia.id()=3 I get:



11111111111111111111111111111111111100000000000011111111111



And you can print the BitSet this way:



    public static void printBitset(BitSet b) {
StringBuilder s = new StringBuilder();
for( int i = 0; i < b.length(); i++ )
{
s.append( b.get( i ) == true ? 1 : 0 );
}

System.out.println( s );
}


Which demonstrates what I am saying.



Thank you.










share|improve this question















I am using BitSet to represent possible hours where lectures are filled in, the case is that when you put to false the corner bits, they are simplified, this means that they are not anymore in the BitSet. How can I ask BitSet to not simplify?



To make my explanation clearer this is the code:



   for(Map.Entry<GrupAssig, BitSet> entry : bitsetPerGrup.entrySet()){

BitSet bitset = entry.getValue();

//n franges per dia
int numFranges = UnitatDocent.getNumFranges();
int indexDia = this.dia.id() * numFranges;

bitset.clear(indexDia, indexDia+numFranges);
}


Imagine that the bitset has 60 bits by default, and numFranges=12 and this.dia.id()=4. This would make the last twelve bits set to 0. The result I get is:



111111111111111111111111111111111111111111111111



But if this.dia.id()=3 I get:



11111111111111111111111111111111111100000000000011111111111



And you can print the BitSet this way:



    public static void printBitset(BitSet b) {
StringBuilder s = new StringBuilder();
for( int i = 0; i < b.length(); i++ )
{
s.append( b.get( i ) == true ? 1 : 0 );
}

System.out.println( s );
}


Which demonstrates what I am saying.



Thank you.







java bit bitset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:32

























asked Nov 10 at 18:20









Marc43

992310




992310












  • What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
    – Michael Krause
    Nov 10 at 18:27












  • Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
    – Louis Wasserman
    Nov 10 at 18:31










  • Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
    – Marc43
    Nov 10 at 18:34










  • Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
    – Marc43
    Nov 10 at 18:39












  • Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
    – Michael Krause
    Nov 10 at 18:55


















  • What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
    – Michael Krause
    Nov 10 at 18:27












  • Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
    – Louis Wasserman
    Nov 10 at 18:31










  • Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
    – Marc43
    Nov 10 at 18:34










  • Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
    – Marc43
    Nov 10 at 18:39












  • Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
    – Michael Krause
    Nov 10 at 18:55
















What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
– Michael Krause
Nov 10 at 18:27






What do you mean by corner bits? The ones at the range extremes? What do you mean by simplify?
– Michael Krause
Nov 10 at 18:27














Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
– Louis Wasserman
Nov 10 at 18:31




Why do you care about the implementation details? It doesn't make any difference to the results of the methods you can call.
– Louis Wasserman
Nov 10 at 18:31












Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
– Marc43
Nov 10 at 18:34




Yes, I refer to the range extremes. By simplifying I mean that they are erased, given a bitset of size 60 bits, if you set the first ten bits to false, they are not anymore in the set. But if you set the middle bits to false, they stay there.
– Marc43
Nov 10 at 18:34












Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
– Marc43
Nov 10 at 18:39






Louis, I thought that too, but if you do what I said (setting the ten first bits to false, for example) and the you do: bitset.get(0) you will get true, and this is not what should happend for what I need.
– Marc43
Nov 10 at 18:39














Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
– Michael Krause
Nov 10 at 18:55




Posting your code would help. One thing I can say is that the length() method may not do what you think it will do because it's based off of the BitSet's cardinality. In other words, length will look for a 1 at the greatest index to determine length. If all bits are set to false, length() will return 0.
– Michael Krause
Nov 10 at 18:55












1 Answer
1






active

oldest

votes

















up vote
3
down vote













Here's the documentation for BitSet.length:



length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.


If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.



For instance the following code produces "0000011000":



import java.util.BitSet;

public class Main {

public static void main(String args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}





share|improve this answer























  • Then imagine that the problem happens for the first bits, this would not work
    – Marc43
    Nov 10 at 22:18






  • 1




    I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
    – Ryan C
    Nov 10 at 22:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242039%2fhow-to-make-bitset-to-be-not-simplified-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













Here's the documentation for BitSet.length:



length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.


If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.



For instance the following code produces "0000011000":



import java.util.BitSet;

public class Main {

public static void main(String args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}





share|improve this answer























  • Then imagine that the problem happens for the first bits, this would not work
    – Marc43
    Nov 10 at 22:18






  • 1




    I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
    – Ryan C
    Nov 10 at 22:21















up vote
3
down vote













Here's the documentation for BitSet.length:



length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.


If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.



For instance the following code produces "0000011000":



import java.util.BitSet;

public class Main {

public static void main(String args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}





share|improve this answer























  • Then imagine that the problem happens for the first bits, this would not work
    – Marc43
    Nov 10 at 22:18






  • 1




    I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
    – Ryan C
    Nov 10 at 22:21













up vote
3
down vote










up vote
3
down vote









Here's the documentation for BitSet.length:



length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.


If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.



For instance the following code produces "0000011000":



import java.util.BitSet;

public class Main {

public static void main(String args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}





share|improve this answer














Here's the documentation for BitSet.length:



length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.


If you need to print out a certain number of bits (e.g. 60) then use a constant instead of ".length()" for your loop. You can call ".get(index)" on any index regardless of the length and it will give you the result for that bit.



For instance the following code produces "0000011000":



import java.util.BitSet;

public class Main {

public static void main(String args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 22:29

























answered Nov 10 at 19:57









Ryan C

644110




644110












  • Then imagine that the problem happens for the first bits, this would not work
    – Marc43
    Nov 10 at 22:18






  • 1




    I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
    – Ryan C
    Nov 10 at 22:21


















  • Then imagine that the problem happens for the first bits, this would not work
    – Marc43
    Nov 10 at 22:18






  • 1




    I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
    – Ryan C
    Nov 10 at 22:21
















Then imagine that the problem happens for the first bits, this would not work
– Marc43
Nov 10 at 22:18




Then imagine that the problem happens for the first bits, this would not work
– Marc43
Nov 10 at 22:18




1




1




I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
– Ryan C
Nov 10 at 22:21




I worked pretty heavily with the BitSet class a few years ago, and I don't believe that this problem exists for the first bits. If you think it does, please show a simple example that demonstrates this.
– Ryan C
Nov 10 at 22:21


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242039%2fhow-to-make-bitset-to-be-not-simplified-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ