"Sputnik" help  
Sputnik Help
BinaryGetBit(<binary>, <index>)


The BinaryGetBit function retrieves the value of the bit at the specified index in the <binary>.


<binary> The binary from which to retrieve the bit.
<index> The index of the bit to retrieve (0-based).

Return Value

Success: Returns the bit at the specified index.
Failure: Returns false.


The binary manipulation functions provided in this library allow you to work with individual bits within a binary data structure. A bit is the smallest unit of information in computing and can have two possible values: 0 or 1. The functions enable you to retrieve, set, toggle, invert, fill, and clear individual bits within a binary. In Sputnik, the binary data is stored in bytes, with each byte capable of holding 8 bits. These functions provide a convenient way to perform low-level bit manipulation operations on binary data.


BinarySetBit, BinaryClearBit, BinaryFillBit, BinaryInvertBit


Reading bits from binary:

my $binary = pack("B*", "10101101");
say BinaryGetBit($binary, 0);
say BinaryGetBit($binary, 1);
say BinaryGetBit($binary, 2);
say BinaryGetBit($binary, 3);
say BinaryGetBit($binary, 4);
say BinaryGetBit($binary, 5);
say BinaryGetBit($binary, 6);
say BinaryGetBit($binary, 7);
// true
// false
// true
// false
// true
// true
// false
// true

Notice the true/false is exactly the same as the 1s and 0s.

Here is an easier way to print it:

my $binary = pack("B*", "10101101");
my $unpacked = "";
for (my $i = 0; $i < 8; $i++)
    $unpacked .= BinaryGetBit($binary, $i) ? "1" : "0";
say $unpacked;
// 10101101

That rebuilds the original string.

Cheryl (uberfox@hotmail.com)