• PHP Video Tutorials

PHP - gmp_​setbit() Function



Definition and Usage

The gmp_​setbit() function sets the bit index for the given GMP number.

Description

The gmp_​setbit() sets the bit for the given index in the GMP number.

Syntax

gmp_setbit ( GMP $gmpnumber , int $index [, bool $bit_on = TRUE ] ) : void

Parameters

Sr.No Parameter & Description
1

gmpnumber

The gmpnumber you want to change by setting the bit.The gmpnumber can be a GMP resource number , a gmp object or a numeric string.

2

index

The index that needs to be set. The index starts from 0.

3

bit_on

The default value for bit_on is true. If true or 1 set the bit, if false or 0 clear the bit.

Return Values

PHP gmp_setbit() function does not return any value.

PHP Version

This function will work from PHP Version greater than 5.0.0.

Example 1

Working of gmp_setbit() −

<?php
   $a = gmp_init("5");
   echo "The number 5 binary value is :" .gmp_strval($a, 2);
   echo "<br/><br/>";
   gmp_setbit($a, 1);
   echo "After setting bit at index 1 : ".gmp_strval($a, 2);
?>

This will produce following result −

The number 5 binary value is :101
After setting bit at index 1 : 111

Example 2

Working of gmp_setbit() −

<?php
   $a = gmp_init(150);
   echo "The number is :" .gmp_strval($a);
   echo "<br/><br/>";
   gmp_setbit($a, 0);
   echo "After setting bit at index 1 : ".gmp_strval($a);
?>

This will produce following result −

The number is :150
After setting bit at index 1 : 151
php_function_reference.htm
Advertisements