个性化阅读
专注于IT技术分析

如何关闭数字中的特定位?

点击下载

本文概述

给定数字n和值k, 请关闭n中的第k位。请注意, k = 1表示最右边的位。

例子:

Input:  n = 15, k = 1
Output: 14

Input:  n = 14, k = 1
Output: 14
The rightmost bit was already off, so no change.

Input:  n = 15, k = 2
Output: 13

Input:  n = 15, k = 3
Output: 11

Input:  n = 15, k = 4
Output: 7

Input:  n = 15, k>= 5
Output: 15

这个想法是使用按位<<, &和〜运算符。使用表达式”〜(1 <<(k – 1))”, 我们得到一个数字, 其中除第k个位外, 所有位均已设置。如果我们使用n对这个表达式进行按位&运算, 我们将得到一个数字, 除了第k个位数为0外, 该位数与n相同。

以下是上述想法的实现。

C++

#include <iostream>
using namespace std;
  
//Returns a number that has all bits same as n
//except the k'th bit which is made 0
int turnOffK( int n, int k)
{
     //k must be greater than 0
     if (k <= 0) return n;
  
     //Do & of n with a number with all set bits except
     //the k'th bit
     return (n & ~(1 <<(k - 1)));
}
  
//Driver program to test above function
int main()
{
     int n = 15;
     int k = 4;
     cout <<turnOffK(n, k);
     return 0;
}

Java

//Java program to turn off a particular bit in a number
import java.io.*;
  
class TurnOff 
{
     //Function to returns a number that has all bits same as n
     //except the k'th bit which is made 0
     static int turnOffK( int n, int k)
     {
         //k must be greater than 0
         if (k <= 0 ) 
             return n;
   
         //Do & of n with a number with all set bits except
         //the k'th bit
         return (n & ~( 1 <<(k - 1 )));
     }
      
     //Driver program
     public static void main (String[] args) 
     {
         int n = 15 ;
         int k = 4 ;
         System.out.println(turnOffK(n, k));
     }
}
//Contributed by Pramod Kumar

Python3

# Returns a number that
# has all bits same as n
# except the k'th bit
# which is made 0
  
def turnOffK(n, k):
  
     # k must be greater than 0
     if (k <= 0 ): 
         return n
   
     # Do & of n with a number
     # with all set bits except
     # the k'th bit
     return (n & ~( 1 <<(k - 1 )))
  
   
# Driver code
n = 15
k = 4
print (turnOffK(n, k))
  
# This code is contributed
# by Anant Agarwal.

C#

//C# program to turn off a 
//particular bit in a number
using System;
  
class GFG
{
      
     //Function to returns a number 
     //that has all bits same as n
     //except the k'th bit which is 
     //made 0
     static int turnOffK( int n, int k)
     {
         //k must be greater than 0
         if (k <= 0) 
             return n;
  
         //Do & of n with a number 
         //with all set bits except
         //the k'th bit
         return (n & ~ (1 <<(k - 1)));
     }
      
     //Driver Code
     public static void Main () 
     {
         int n = 15;
         int k = 4;
         Console.Write(turnOffK(n, k));
     }
}
  
//This code is contributed by Nitin Mittal.

PHP

<?php
//PHP program to turn off a 
//particular bit in a number
  
//Returns a number that has
//all bits same as n except 
//the k'th bit which is made 0
function turnOffK( $n , $k )
{
      
     //k must be greater than 0
     if ( $k <= 0)
         return $n ;
  
     //Do & of n with a number
     //with all set bits except
     //the k'th bit
     return ( $n & ~(1 <<( $k - 1)));
}
  
//Driver Code
$n = 15;
$k = 4;
echo turnOffK( $n , $k );
  
//This code is contributed by nitin mittal
?>

输出如下:

7

行使:编写一个函数turnOnK()将第k位打开。

本文作者:拉胡尔·贾恩(Rahul Jain)。如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

赞(0)
未经允许不得转载:srcmini » 如何关闭数字中的特定位?

评论 抢沙发

评论前必须登录!