Dado un número entero N , la tarea es encontrar un número entero M formado tomando el bit establecido más a la derecha en N , es decir, el único bit establecido en M será el bit establecido más a la derecha en N y el resto de los bits no estarán establecidos.
Ejemplos:
Entrada: N = 7
Salida: 1
7 = 111, el número formado por el último bit establecido es 001, es decir, 1.
Entrada: N = 10
Salida: 2
10 = 1010 -> 0010 = 2
Entrada: N = 16
Salida: 16
Acercarse:
- Almacene x = n & (n – 1) que desactivará el primer bit establecido desde la derecha en n .
- Ahora, actualice n = n ^ x para establecer el bit modificado y desactive todos los demás, que es el número entero requerido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the integer formed // by taking the rightmost set bit in n int firstSetBit(int n) { // n & (n - 1) unsets the first set // bit from the right in n int x = n & (n - 1); // Take xor with the original number // The position of the 'changed bit' // will be set and rest will be unset return (n ^ x); } // Driver code int main() { int n = 12; cout << firstSetBit(n); return 0; }
Java
// Java implementation of the approach class geeks { // Function to return the integer formed // by taking the rightmost set bit in n public static int firstSetBit(int n) { // n & (n - 1) unsets the first set // bit from the right in n int x = n & (n-1); // Take xor with the original number // The position of the 'changed bit' // will be set and rest will be unset return (n ^ x); } // Driver Code public static void main (String[] args) { int n = 12; System.out.println(firstSetBit(n)); } } // This code is contributed by // sanjeev2552
Python3
# Python3 implementation of the approach # Function to return the integer formed # by taking the rightmost set bit in n def firstSetBit(n): # n & (n - 1) unsets the first set # bit from the right in n x = n & (n - 1) # Take xor with the original number # The position of the 'changed bit' # will be set and rest will be unset return (n ^ x) # Driver code n = 12 print(firstSetBit(n)) # This code is contributed by mohit kumar 29
C#
// C# implementation of the approach using System; class geeks { // Function to return the integer formed // by taking the rightmost set bit in n public static int firstSetBit(int n) { // n & (n - 1) unsets the first set // bit from the right in n int x = n & (n-1); // Take xor with the original number // The position of the 'changed bit' // will be set and rest will be unset return (n ^ x); } // Driver Code public static void Main () { int n = 12; Console.WriteLine(firstSetBit(n)); } } // This code is contributed by // anuj_67..
Javascript
<script> // JavaScript implementation of the approach // Function to return the integer formed // by taking the rightmost set bit in n function firstSetBit(n) { // n & (n - 1) unsets the first set // bit from the right in n let x = n & (n - 1); // Take xor with the original number // The position of the 'changed bit' // will be set and rest will be unset return (n ^ x); } // Driver code let n = 12; document.write(firstSetBit(n)); // This code is contributed by Manoj. </script>
Producción:
4
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por bankanikhil29 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA