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?
java arrays bitset
add a comment |
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?
java arrays bitset
1
the bit order is being printed in reverse bytoBinaryString
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 howtoBinaryString
does
– Carlos Heuberger
2 days ago
add a comment |
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?
java arrays bitset
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
java arrays bitset
asked 2 days ago
user8231110
446
446
1
the bit order is being printed in reverse bytoBinaryString
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 howtoBinaryString
does
– Carlos Heuberger
2 days ago
add a comment |
1
the bit order is being printed in reverse bytoBinaryString
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 howtoBinaryString
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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