Open In App

PHP | gmp_setbit() Function

The

gmp_setbit() function



is an inbuilt function in PHP which is used to set the bit index in given $num.

Syntax:



void gmp_setbit( GMP $num, int $index, bool $bit_on )

Parameters:

This function accepts three parameters as mentioned above and described below:

Return Value:

This function returns the

GMP

number resource in PHP 5.5 and earlier, or a GMP object in PHP 5.6 and later.

Program 1:

Program to illustrate

gmp_setbit()

function having index 0:




<?php
// PHP program to demonstrate the gmp_setbit() function
// with index 0
 
// It will create a gmp number
$num = gmp_init("2"); 
 
// gmp_strval will return the string value of a GMP number
// when the argument is numeric string and
// the second parameter is present
echo gmp_strval($num), ' -> 0b', gmp_strval($num, 2), "\n";
 
gmp_setbit($num, 0);  // 0b10 now becomes 0b11
 
echo gmp_strval($num), ' -> 0b', gmp_strval($num, 2);
?>

Output:

2 -> 0b10
3 -> 0b11

Program 2:

Program of

gmp_setbit()

function for clearing the bit:




<?php
 
// php program to illustrate gmp_setbit() function
// for clearing bit
 
// gmp_init() will create a gmp number
$num = gmp_init("3");
 
// gmp_strval will return the string value of a GMP number
// when the argument is numeric string and
// the second parameter is present
echo gmp_strval($num), ' -> 0b', gmp_strval($num, 2), "\n";
 
gmp_setbit($num, 0, false); // clearing bit at index 0
 
echo gmp_strval($num), ' -> 0b', gmp_strval($num, 2);
?>

Output :

3 -> 0b11
2 -> 0b10

Reference:

http://php.net/manual/en/function.gmp-setbit.php


Article Tags :