给定整数n,则输出no。以二进制形式表示的前导零。
前导零是数字二进制形式的第一个非零数字之前的任何0数字。
例子:
Input : 16
Output :27
As Binary(16) = (00000000000000000000000000010000)
Input :33
Output :26
As Binary(16)=(00000000000000000000000000100001)
解决方案1:天真的方法是转换no。转换为二进制形式,然后计算否。前导零。它使用昂贵的除法运算。
C++
// C++ program of number of leading zeros in
// binary representation of a given number
#include
using namespace std;
// Function to count the no. of leading zeros
int countZeros(unsigned int x)
{
// Keep shifting x by one until leftmost bit
// does not become 1.
int total_bits = sizeof(x) * 8;
int res = 0;
while ( !(x & (1 << (total_bits - 1))) )
{
x = (x << 1);
res++;
}
return res;
}
// Main function
int main()
{
int x = 101;
cout << countZeros(x);
return 0;
}
Java
// Java program of number of leading zeros in
// binary representation of a given number
class GFG
{
static byte sizeofInt = 8;
// Function to count the no. of leading zeros
static int countZeros(int x)
{
// Keep shifting x by one until leftmost bit
// does not become 1.
int total_bits = sizeofInt * 8;
int res = 0;
while ((x & (1 << (total_bits - 1))) == 0)
{
x = (x << 1);
res++;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int x = 101;
System.out.println(countZeros(x));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program of number of
# leading zeros in binary
# representation of a given number
# Function to count the
# no. of leading zeros
def countZeros(x):
# Keep shifting x by one until
# leftmost bit does not become 1.
total_bits = 32
res = 0
while ((x & (1 << (total_bits - 1))) == 0):
x = (x << 1)
res += 1
return res
# Driver Code
x = 101
print(countZeros(x))
# This code is contributed
# by Mohit Kumar
C#
// C# program of number of leading zeros in
// binary representation of a given number
using System;
class GFG
{
static byte sizeofInt = 8;
// Function to count the
// no. of leading zeros
static int countZeros(int x)
{
// Keep shifting x by one until
// leftmost bit does not become 1.
int total_bits = sizeofInt * 8;
int res = 0;
while ((x & (1 << (total_bits - 1))) == 0)
{
x = (x << 1);
res++;
}
return res;
}
// Driver Code
public static void Main(String[] args)
{
int x = 101;
Console.WriteLine(countZeros(x));
}
}
// This code is contributed by Rajput-Ji
C++
// C++ program of number of leading zeros in
// binary representation of a given number
#include
using namespace std;
// Function to count the no. of leading zeros
int countZeros(int x)
{
unsigned y;
int n = 32;
y = x >> 16;
if (y != 0) {
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0) {
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0) {
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0) {
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Main function
int main()
{
int x = 101;
cout << countZeros(x);
return 0;
}
Java
// Java program of number of leading zeros in
// binary representation of a given number
import java.io.*;
class GFG {
// Function to count the no. of leading zeros
static int countZeros(int x)
{
int y;
int n = 32;
y = x >> 16;
if (y != 0) {
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0) {
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0) {
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0) {
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Main function
public static void main (String[] args) {
int x = 101;
System.out.println (countZeros(x));
}
//This code is contributed by @Tushil.
}
Python3
# Python3 program of number of leading zeros in
# binary representation of a given number
# Function to count the no. of leading zeros
def countZeros(x):
n = 32;
y = x >> 16;
if (y != 0):
n = n - 16;
x = y;
y = x >> 8;
if (y != 0):
n = n - 8;
x = y;
y = x >> 4;
if (y != 0):
n = n - 4;
x = y;
y = x >> 2;
if (y != 0):
n = n - 2;
x = y;
y = x >> 1;
if (y != 0):
return n - 2;
return n - x;
# Main function
def main():
x = 101;
print(countZeros(x))
if __name__ == '__main__':
main()
C#
// C# program of number of leading zeros in
// binary representation of a given number
using System;
class GFG
{
// Function to count the no. of
// leading zeros
static int countZeros(int x)
{
int y;
int n = 32;
y = x >> 16;
if (y != 0)
{
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0)
{
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0)
{
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0)
{
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Driver Code
static public void Main ()
{
int x = 101;
Console.WriteLine(countZeros(x));
}
}
// This code is contributed by ajit
PHP
> 16;
if ($y != 0)
{
$n = $n - 16;
$x = $y;
}
$y = $x >> 8;
if ($y != 0)
{
$n = $n - 8;
$x = $y;
}
$y = $x >> 4;
if ($y != 0)
{
$n = $n - 4;
$x = $y;
}
$y = $x >> 2;
if ($y != 0) {
$n = $n - 2;
$x = $y;
}
$y = $x >> 1;
if ($y != 0)
return $n - 2;
return $n - $x;
}
// Driver Code
$x = 101;
echo countZeros($x);
// This code is contributed
// by Akanksha Rai
输出:
25
解决方案2:一种有效的方法是使用按位右移运算来实现相同的目的。算法中的步骤为:
设x为我们的编号。然后
unsigned y;
int n = 32;
y = x >>16; if (y != 0) {n = n -16; x = y;}
y = x >> 8; if (y != 0) {n = n - 8; x = y;}
y = x >> 4; if (y != 0) {n = n - 4; x = y;}
y = x >> 2; if (y != 0) {n = n - 2; x = y;}
y = x >> 1; if (y != 0) return n - 2;
return n - x;
以上方法仅以12到20条指令执行。
C++
// C++ program of number of leading zeros in
// binary representation of a given number
#include
using namespace std;
// Function to count the no. of leading zeros
int countZeros(int x)
{
unsigned y;
int n = 32;
y = x >> 16;
if (y != 0) {
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0) {
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0) {
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0) {
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Main function
int main()
{
int x = 101;
cout << countZeros(x);
return 0;
}
Java
// Java program of number of leading zeros in
// binary representation of a given number
import java.io.*;
class GFG {
// Function to count the no. of leading zeros
static int countZeros(int x)
{
int y;
int n = 32;
y = x >> 16;
if (y != 0) {
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0) {
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0) {
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0) {
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Main function
public static void main (String[] args) {
int x = 101;
System.out.println (countZeros(x));
}
//This code is contributed by @Tushil.
}
Python3
# Python3 program of number of leading zeros in
# binary representation of a given number
# Function to count the no. of leading zeros
def countZeros(x):
n = 32;
y = x >> 16;
if (y != 0):
n = n - 16;
x = y;
y = x >> 8;
if (y != 0):
n = n - 8;
x = y;
y = x >> 4;
if (y != 0):
n = n - 4;
x = y;
y = x >> 2;
if (y != 0):
n = n - 2;
x = y;
y = x >> 1;
if (y != 0):
return n - 2;
return n - x;
# Main function
def main():
x = 101;
print(countZeros(x))
if __name__ == '__main__':
main()
C#
// C# program of number of leading zeros in
// binary representation of a given number
using System;
class GFG
{
// Function to count the no. of
// leading zeros
static int countZeros(int x)
{
int y;
int n = 32;
y = x >> 16;
if (y != 0)
{
n = n - 16;
x = y;
}
y = x >> 8;
if (y != 0)
{
n = n - 8;
x = y;
}
y = x >> 4;
if (y != 0)
{
n = n - 4;
x = y;
}
y = x >> 2;
if (y != 0)
{
n = n - 2;
x = y;
}
y = x >> 1;
if (y != 0)
return n - 2;
return n - x;
}
// Driver Code
static public void Main ()
{
int x = 101;
Console.WriteLine(countZeros(x));
}
}
// This code is contributed by ajit
的PHP
> 16;
if ($y != 0)
{
$n = $n - 16;
$x = $y;
}
$y = $x >> 8;
if ($y != 0)
{
$n = $n - 8;
$x = $y;
}
$y = $x >> 4;
if ($y != 0)
{
$n = $n - 4;
$x = $y;
}
$y = $x >> 2;
if ($y != 0) {
$n = $n - 2;
$x = $y;
}
$y = $x >> 1;
if ($y != 0)
return $n - 2;
return $n - $x;
}
// Driver Code
$x = 101;
echo countZeros($x);
// This code is contributed
// by Akanksha Rai
输出:
25
时间复杂度:此方法的时间复杂度为O(1)
空间复杂度:此方法的空间复杂度为O(1)