Open In App

PHP | gmp_setbit() Function

Last Updated : 31 Oct, 2023
Suggest changes
Share
Like Article
Like
Report

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:

  • $num: It is a required parameter. This parameter gives the value to be modified. This parameter can be a GMP number resource in PHP 5.5 and earlier, a GMP object in PHP 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
  • $index: It is a required parameter. This parameter provides the index to be set. Here index 0 represents the least significant bit.
  • $set_state: This parameter sets the bits, if "True" it sets the bit to 1/on and if "False", it will clear the bit that is set the bit to 0/off.

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 // 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 // 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

Similar Reads