Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As we can construct a bitset object by a Buffer, Can we convert the bitset to Buffer #44

Open
blyadav511 opened this issue Nov 12, 2020 · 4 comments

Comments

@blyadav511
Copy link

As we can construct a bitset object by a Buffer, Can we convert the bitset to Buffer

let bitset = require("bitset");
let buf = Buffer.from([100, 97, 98]);
let bs = bitset(buf);
bs.toString();
'11000100110000101100100'

Can we convert a bitset into buffer?
let bs1 = new bitset();
bs1.setRange(4,9,1);
{ data: [ 1008, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], _: 0 }
bs1.toString();
'1111110000'

Now can this be converted into the buffer?
eg. add the padding 0's left and make it's size multiple of 8 then convert to byte array or Buffer.

@infusion
Copy link
Collaborator

infusion commented Nov 13, 2020

You must note, that this only works for non-negated bitsets. If _ != 0, you must truncate it yourself at a certain point.
For "positive" bitsets (no NOT operation was applied), you could come up with something like

// Remove unnecessary zeros
while (bs.data.length > 1 && bs.data[bs.data.length - 1] === 0) {
   bs.data.pop();
}

// Pad with zeros to a multiple of 8
while (bs.data.length % 8 != 0) {
   bs.data.push(0);
}

const buf = Buffer.from(bs.data.reverse());

If you don't need the minimal multiple of 8, remove the first loop.

@blyadav511
Copy link
Author

@infusion,
bs.data is a list of integer and each index of list stores 32 bits. So push/pop operation adds/removes 32 bits from the bitset.

eg.
et bs1 = new bitset();
bs1.setRange(4,9,1); => 1111110000 => 1008
{ data: [ 1008 ] }

bs1.setRange(32,33,1); => 11 00000000000000000000001111110000
{ data: [ 1008, 3 ] }

setRange
bs1.data.push(1);
{ data: [ 1008, 3, 1]}

1 00000000000000000000000000000011 00000000000000000000001111110000

note: bs.toString() removes initial zeros.

@infusion
Copy link
Collaborator

bs.data is a list of integer and each index of list stores 32 bits. So push/pop operation adds/removes 32 bits from the bitset.

I know, I designed it like this.

eg.
et bs1 = new bitset();
bs1.setRange(4,9,1); => 1111110000 => 1008
{ data: [ 1008 ] }

bs1.setRange(32,33,1); => 11 00000000000000000000001111110000
{ data: [ 1008, 3 ] }

setRange
bs1.data.push(1);
{ data: [ 1008, 3, 1]}

1 00000000000000000000000000000011 00000000000000000000001111110000

note: bs.toString() removes initial zeros.

I know. What has that to do with your initial question?

@blyadav511
Copy link
Author

let bitset = require("bitset");
let buf = Buffer.from([100, 97, 98]);
<Buffer 64 61 62>
buf.toString();

'dab' // contains a string as "dba"

let bs = bitset(buf);
bs.toString();
'11000100110000101100100' // considering each element of buffer as 8 bit

//convert back to buffer
while (bs.data.length > 1 && bs.data[bs.data.length - 1] === 0) {bs.data.pop();}
while (bs.data.length % 8 != 0) {bs.data.push(0);}
let buf2 = Buffer.from(bs.data.reverse());

<Buffer 00 00 00 00 00 00 00 64>
buf2.toString();
'\u0000\u0000\u0000\u0000\u0000\u0000\u0000d'

So we are not getting same buffer... @infusion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants