Dado un número entero, establezca el bit no establecido más a la izquierda. El bit desarmado más a la izquierda es el primer bit desarmado después del bit de armado más significativo. Si todos los bits (después del bit establecido más significativo) están establecidos, devuelva el número.
Ejemplos:
Input : 10 Output : 14 10 = 1 0 1 0 // 10 binary 14 = 1 1 1 0 // after set left most unset bit Input : 15 Output : 15 15 = 1 1 1 1 // 15 binary 15 = 1 1 1 1 // because all bits are set
Enfoque:-
1. Devuelva el número si todos los bits están configurados.
2. Atraviese todo el bit para obtener el último bit desarmado.
3. Tomar OR con el número original y el bit desarmado.
A continuación se muestra la implementación del enfoque.
C++
// C++ program to set the leftmost unset bit #include <iostream> using namespace std; // set left most unset bit int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp=n, count=0; temp>0; temp>>=1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function int main() { int n = 10; cout << setleftmostunsetbit(n); return 0; }
Java
// Java program to set // the leftmost unset bit import java.io.*; class GFG { // set left most unset bit static int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp = n, count = 0; temp > 0; temp >>= 1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function public static void main (String[] args) { int n = 10; System.out.println(setleftmostunsetbit(n)); } } // This code is contributed by Ansu Kumari
Python3
# Python program to set the leftmost unset bit # Set left most unset bit def setleftmostunsetbit(n): # if number contain all # 1 then return n if not (n & (n + 1)): return n # Find position of leftmost unset bit pos, temp, count = 0, n, 0 while temp: # if temp L.S.B is zero # then unset bit pos is # change if not (temp & 1): pos = count count += 1; temp>>=1 # return OR of number and # unset bit pos return (n | (1 << (pos))) # Driver Function n = 10 print(setleftmostunsetbit(n)) # This code is contributed by Ansu Kumari
C#
// C# program to set // the leftmost unset bit using System; class GFG { // set left most unset bit static int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp = n, count = 0; temp > 0; temp >>= 1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function public static void Main () { int n = 10; Console.WriteLine(setleftmostunsetbit(n)); } } // This code is contributed by vt_m
PHP
<?php // php program to set the // leftmost unset bit // set left most unset bit function setleftmostunsetbit($n) { // if number contain all // 1 then return n if (($n & ($n + 1)) == 0) return $n; // Find position of leftmost // unset bit. $pos = 0; for ($temp = $n, $count = 0; $temp > 0; $temp >>= 1, $count++) // if temp L.S.B is zero // then unset bit pos is // change if (($temp & 1) == 0) $pos = $count; // return OR of number // and unset bit pos return ($n | (1 << ($pos))); } // Driver code $n = 10; echo setleftmostunsetbit($n); //This code is contributed by mits ?>
Javascript
<script> // Javascript program to set the // leftmost unset bit // Set left most unset bit function setleftmostunsetbit(n) { // If number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. let pos = 0; for(let temp = n, count = 0; temp > 0; temp >>= 1, count++) // If temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // Return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Code let n = 10; document.write(setleftmostunsetbit(n)); // This code is contributed by Manoj. </script>
Producción:
14
Complejidad del tiempo: O(log 2 n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por DevanshuAgarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA