What is gmp_setbit in PHP?

gmp_setbit is a PHP function that sets the bit’s index in the first parameter passed to the function.

Syntax

gmp_setbit(GMP $num, int $index, bool $value = true): void

Parameters

  • num: The value that needs to be modified.

num can be a numeric string, GMP object, or of the integer type.

  • index: The bit’s index needed to be set.

Here, if the index equals zero, it means that the index represents the least significant bit.

  • value: A value of the Boolean type. The value is kept True if we need to the bet. Otherwise, it returns False.

Return value

The function returns a GMP number.

Code

<?php
// gmp_init creates a gmp number
$number = gmp_init("2");
echo gmp_strval($number), ' -> 0b', gmp_strval($number, 2), "\n";
gmp_setbit($number, 0);
?>

Expected output

2 -> 0b10