PHP | gmp_scan1() Function

The gmp_scan1() is an inbuilt function which is used to scan “1” in the GMP number(GNU Multiple Precision : For large numbers) starting from given index which move towards most significant bits in the number.

Syntax:

gmp_scan1($num, $index)

Parameters: This function accepts two parameters as explained below:



  • $num: This parameter is a GMP number and is mandatory to be passed. This parameter can be a GMP object in PHP version 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: This parameter represents the index or position in the bitwise representation of the number $num from where we want to start the search.

Return Value: The function returns the position where we find “1” in the number.

Examples:

Input : gmp_scan1("101111101", 6)
Output : 8

Input : gmp_scan1("111001111", 2)
Output : 3

Below programs illustrate the gmp_scan1() function in PHP:

Program 1: Program to find the position of “1” bit in GMP number when numeric strings as GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to find position of "1" bit in GMP
// number passed as arguments
  
// strings as GMP numbers
$num = "10110001";
$pos = 2;
  
  
echo gmp_scan1($num, $pos) . "\n";
  
?>

chevron_right


Output:

4

Program 2: Program to find the position of “1” bit in GMP number when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find position of "1" bit in GMP
// number
  
//creating GMP numbers using gmp_init()
$num = gmp_init(10001111101);
$pos = 2;
  
echo gmp_scan1($num, $pos) . "\n";
  
?>

chevron_right


Output:

3

Reference:
http://php.net/manual/en/function.gmp-scan1.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.