"Sputnik" help  
Sputnik Help
BinarySetBit(<binary>, <index>, [<setTo>])

Description

The BinarySetBit function sets the value of the bit at the specified index in the <binary>. It can be used to explicitly set the bit to a specific value or toggle the bit.

Parameters

<binary> The binary in which to set the bit.
<index> The index of the bit to set (0-based).
[<setTo>] Optional boolean value indicating the value to which the bit should be set. If not provided, the bit is set to true (Default: true).

Return Value

Success: Returns if the bit was successfully set or toggled.
Failure: Returns false.

Remarks

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.

Related

BinaryGetBit, BinaryClearBit, BinaryFillBit, BinaryInvertBit

Example

Flipping a bit to true (1):

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

Flipping a bit to false (0):

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

Here is an easier way to get the string back:

my $binary = pack("B*", "10101101");
BinarySetBit($binary, 0, false);
say unpack("B*", $binary, @UnpackSingle);
// PRINTS
// 00101101

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org