chewbacca chewbacca - 7 months ago 10
Perl Question

Do I need to pack/unpack data read from a binary file before performing bitwise operations?

I've read two bytes from a binary file into $data and now need to perform a bitwise and.

But when I do

if (($data & "\x100") != 0) {


I get the error Argument "@\0" isn't numeric in numeric ne (!=)

Am I supposed to convert $data first?

Answer

Why are you using strings for binary operations?

Use ($data & 0x100) != 0 and, if necessary, convert $data to a number.

Comments