给定一个数字n和一个值k,打开n中的第k位。 例如:
null
Input: n = 4, k = 2Output: 6Input: n = 3, k = 3Output: 7Input: n = 64, k = 4Output: 72Input: n = 64, k = 5Output: 80
其思想是使用位< 下面是上述想法的实现。
C++
// CPP program to turn on a particular bit #include <iostream> using namespace std; // Returns a number that has all bits same as n // except the k'th bit which is made 1 int turnOnK( int n, int k) { // k must be greater than 0 if (k <= 0) return n; // Do | of n with a number with all // unset bits except the k'th bit return (n | (1 << (k - 1))); } // Driver program to test above function int main() { int n = 4; int k = 2; cout << turnOnK(n, k); return 0; } |
JAVA
// Java program to turn on a particular // bit class GFG { // Returns a number that has all // bits same as n except the k'th // bit which is made 1 static int turnOnK( int n, int k) { // k must be greater than 0 if (k <= 0 ) return n; // Do | of n with a number with // all unset bits except the // k'th bit return (n | ( 1 << (k - 1 ))); } // Driver program to test above // function public static void main(String [] args) { int n = 4 ; int k = 2 ; System.out.print(turnOnK(n, k)); } } // This code is contributed by Smitha |
Python 3
# Python 3 program to turn on a # particular bit # Returns a number that has all # bits same as n except the k'th # bit which is made 1 def turnOnK(n, k): # k must be greater than 0 if (k < = 0 ): return n # Do | of n with a number # with all unset bits except # the k'th bit return (n | ( 1 << (k - 1 ))) # Driver program to test above # function n = 4 k = 2 print (turnOnK(n, k)) # This code is contributed by # Smitha |
C#
// C# program to turn on a particular // bit using System; class GFG { // Returns a number that has all // bits same as n except the k'th // bit which is made 1 static int turnOnK( int n, int k) { // k must be greater than 0 if (k <= 0) return n; // Do | of n with a number // with all unset bits except // the k'th bit return (n | (1 << (k - 1))); } // Driver program to test above // function public static void Main() { int n = 4; int k = 2; Console.Write(turnOnK(n, k)); } } // This code is contributed by Smitha |
PHP
<?php // PHP program to turn on a particular bit // Returns a number that has // all bits same as n except // the k'th bit which is made 1 function turnOnK( $n , $k ) { // k must be greater than 0 if ( $k <= 0) return $n ; // Do | of n with a number with all // unset bits except the k'th bit return ( $n | (1 << ( $k - 1))); } // Driver Code $n = 4; $k = 2; echo turnOnK( $n , $k ); // This code is contributed by m_kit ?> |
Javascript
<script> // Javascript program to turn on a particular bit // Returns a number that has all // bits same as n except the k'th // bit which is made 1 function turnOnK(n, k) { // k must be greater than 0 if (k <= 0) return n; // Do | of n with a number // with all unset bits except // the k'th bit return (n | (1 << (k - 1))); } let n = 4; let k = 2; document.write(turnOnK(n, k)); // This code is contributed by suresh07. </script> |
输出:
6
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END