Converting BitSet to Byte[]











up vote
3
down vote

favorite












I have a BitSet, which needs to be converted to a Byte. However, by using BitSet.toByteArray(), I don't get the correct output. I have tried converting the Byte to its binary form in order to check whether the Bitset and the binary form of the Byte are similiar.



public static void generate() {

BitSet temp1 = new BitSet(64);

for (int i = 0; i < 64; i++) {
if(i % 8 != 0 && i < 23) {
temp1.set(i, true);
}
}

StringBuilder s = new StringBuilder();
for (int i = 0; i < 64; i++) {
s.append(temp1.get(i) == true ? 1 : 0);
}

System.out.println(s);

byte tempByteKey1 = temp1.toByteArray();

for (byte b : tempByteKey1) {
System.out.print(Integer.toBinaryString(b & 255 | 256).substring(1));
}

}


Output:



Bitset: 0111111101111111011111100000000000000000000000000000000000000000
Converted Byte: 1111111011111110011111100000000000000000000000000000000000000000


They are both 64 bits, but the first 0 in the BitSet is placed somewhere else after the conversion. Why is this happening, and how can I fix it?










share|improve this question


















  • 1




    the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
    – Carlos Heuberger
    2 days ago












  • But they both have six trues as low order and seven trues as high order
    – user8231110
    2 days ago






  • 1




    what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
    – Carlos Heuberger
    2 days ago












  • That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
    – user8231110
    2 days ago










  • ??? it is stored as originally , just the way you represent it differs as how toBinaryString does
    – Carlos Heuberger
    2 days ago

















up vote
3
down vote

favorite












I have a BitSet, which needs to be converted to a Byte. However, by using BitSet.toByteArray(), I don't get the correct output. I have tried converting the Byte to its binary form in order to check whether the Bitset and the binary form of the Byte are similiar.



public static void generate() {

BitSet temp1 = new BitSet(64);

for (int i = 0; i < 64; i++) {
if(i % 8 != 0 && i < 23) {
temp1.set(i, true);
}
}

StringBuilder s = new StringBuilder();
for (int i = 0; i < 64; i++) {
s.append(temp1.get(i) == true ? 1 : 0);
}

System.out.println(s);

byte tempByteKey1 = temp1.toByteArray();

for (byte b : tempByteKey1) {
System.out.print(Integer.toBinaryString(b & 255 | 256).substring(1));
}

}


Output:



Bitset: 0111111101111111011111100000000000000000000000000000000000000000
Converted Byte: 1111111011111110011111100000000000000000000000000000000000000000


They are both 64 bits, but the first 0 in the BitSet is placed somewhere else after the conversion. Why is this happening, and how can I fix it?










share|improve this question


















  • 1




    the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
    – Carlos Heuberger
    2 days ago












  • But they both have six trues as low order and seven trues as high order
    – user8231110
    2 days ago






  • 1




    what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
    – Carlos Heuberger
    2 days ago












  • That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
    – user8231110
    2 days ago










  • ??? it is stored as originally , just the way you represent it differs as how toBinaryString does
    – Carlos Heuberger
    2 days ago















up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a BitSet, which needs to be converted to a Byte. However, by using BitSet.toByteArray(), I don't get the correct output. I have tried converting the Byte to its binary form in order to check whether the Bitset and the binary form of the Byte are similiar.



public static void generate() {

BitSet temp1 = new BitSet(64);

for (int i = 0; i < 64; i++) {
if(i % 8 != 0 && i < 23) {
temp1.set(i, true);
}
}

StringBuilder s = new StringBuilder();
for (int i = 0; i < 64; i++) {
s.append(temp1.get(i) == true ? 1 : 0);
}

System.out.println(s);

byte tempByteKey1 = temp1.toByteArray();

for (byte b : tempByteKey1) {
System.out.print(Integer.toBinaryString(b & 255 | 256).substring(1));
}

}


Output:



Bitset: 0111111101111111011111100000000000000000000000000000000000000000
Converted Byte: 1111111011111110011111100000000000000000000000000000000000000000


They are both 64 bits, but the first 0 in the BitSet is placed somewhere else after the conversion. Why is this happening, and how can I fix it?










share|improve this question













I have a BitSet, which needs to be converted to a Byte. However, by using BitSet.toByteArray(), I don't get the correct output. I have tried converting the Byte to its binary form in order to check whether the Bitset and the binary form of the Byte are similiar.



public static void generate() {

BitSet temp1 = new BitSet(64);

for (int i = 0; i < 64; i++) {
if(i % 8 != 0 && i < 23) {
temp1.set(i, true);
}
}

StringBuilder s = new StringBuilder();
for (int i = 0; i < 64; i++) {
s.append(temp1.get(i) == true ? 1 : 0);
}

System.out.println(s);

byte tempByteKey1 = temp1.toByteArray();

for (byte b : tempByteKey1) {
System.out.print(Integer.toBinaryString(b & 255 | 256).substring(1));
}

}


Output:



Bitset: 0111111101111111011111100000000000000000000000000000000000000000
Converted Byte: 1111111011111110011111100000000000000000000000000000000000000000


They are both 64 bits, but the first 0 in the BitSet is placed somewhere else after the conversion. Why is this happening, and how can I fix it?







java arrays bitset






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









user8231110

446




446








  • 1




    the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
    – Carlos Heuberger
    2 days ago












  • But they both have six trues as low order and seven trues as high order
    – user8231110
    2 days ago






  • 1




    what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
    – Carlos Heuberger
    2 days ago












  • That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
    – user8231110
    2 days ago










  • ??? it is stored as originally , just the way you represent it differs as how toBinaryString does
    – Carlos Heuberger
    2 days ago
















  • 1




    the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
    – Carlos Heuberger
    2 days ago












  • But they both have six trues as low order and seven trues as high order
    – user8231110
    2 days ago






  • 1




    what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
    – Carlos Heuberger
    2 days ago












  • That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
    – user8231110
    2 days ago










  • ??? it is stored as originally , just the way you represent it differs as how toBinaryString does
    – Carlos Heuberger
    2 days ago










1




1




the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
– Carlos Heuberger
2 days ago






the bit order is being printed in reverse by toBinaryString as you are printing the BitSet. You start with the less significant bit, toBinaryString starts with the most significant one.(see how the zero is in first position on first output, and at 8th position on second output)
– Carlos Heuberger
2 days ago














But they both have six trues as low order and seven trues as high order
– user8231110
2 days ago




But they both have six trues as low order and seven trues as high order
– user8231110
2 days ago




1




1




what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
– Carlos Heuberger
2 days ago






what are you counting??? BitSet: 01111111 01111111 01111110 0...; toBinaryString: 11111110 11111110 01111110 0... => each corresponding 8 bits are reversed. Or, using chars for bits, BitSet: hgfedcba ponmlkji ... , toBinaryString: abcdefgh ijklmnop ...
– Carlos Heuberger
2 days ago














That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
– user8231110
2 days ago




That makes sense, but is it possible to store it as it originally was i.e. without the reversing?
– user8231110
2 days ago












??? it is stored as originally , just the way you represent it differs as how toBinaryString does
– Carlos Heuberger
2 days ago






??? it is stored as originally , just the way you represent it differs as how toBinaryString does
– Carlos Heuberger
2 days ago



















active

oldest

votes











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%2f53402433%2fconverting-bitset-to-byte%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402433%2fconverting-bitset-to-byte%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

Berounka

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Sphinx de Gizeh