[Question] How to read a 33 bits field?
enricovittorini opened this issue · 6 comments
enricovittorini commented
Hello all,
Can someone guide me on how to read 33 bits? The bitX() is limited to 32
Thanks!
enricovittorini commented
can someone help?
enricovittorini commented
hi! is this a live project?
keichi commented
Apologies, I missed this one. Currently, there are no built-in methods to read a 33-bit field. A workaround would be to combine a 32-bit field and a 1-bit field manually.
enricovittorini commented
Thanks @keichi would you show me an example how to do it?
Thanks
keichi commented
Here's an example:
const Parser = require("binary-parser").Parser;
const parser = new Parser()
.bit32("a")
.bit1("b");
const buf = Buffer.from("aaaaaaaa80", "hex");
const res = parser.parse(buf)
console.log((BigInt(res.a) << BigInt(1) | BigInt(res.b)).toString(16)); // -> 155555555
Note that you need to use BigInt
since bitwise operations on Number
automatically convert the operands and results to 32-bit integers.
keichi commented
I will close this issue for now. Please reopen if needed.