Dado un número no negativo n y dos valores l y r . El problema es alternar los bits en el rango de l a r en la representación binaria de n , es decir, alternar los bits del l-ésimo bit más a la derecha al r -ésimo bit más a la derecha. Una operación de alternancia cambia un bit 0 a 1 y un bit 1 a 0 .
Restricción: 1 <= l <= r <= número de bits en la representación binaria de n .
Ejemplos:
Entrada: n = 17, l = 2, r = 3
Salida: 23
Explicación: (17) 10 = (10 00 1) 2
(23) 10 = (10 11 1) 2
Los bits en el rango 2 a 3 en el la representación binaria de 17 se alterna.Entrada: n = 50, l = 2, r = 5
Salida: 44
Enfoque: Los siguientes son los pasos:
- Calcular num como = ((1 << r) – 1) ^ ((1 << (l-1)) – 1) o como ((1 <<r)-l). Esto producirá un número num que tiene un número r de bits y los bits en el rango de l a r son los únicos bits establecidos.
- Ahora, realice n = n ^ num . Esto alternará los bits en el rango de l a r en n .
C++
// C++ implementation to toggle bits in // the given range #include <bits/stdc++.h> using namespace std; // function to toggle bits in the given range unsigned int toggleBitsFromLToR(unsigned int n, unsigned int l, unsigned int r) { // calculating a number 'num' having 'r' // number of bits and bits in the range l // to r are the only set bits int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); // toggle bits in the range l to r in 'n' // and return the number // Besides this, we can calculate num as: num=(1<<r)-l . return (n ^ num); } // Driver program to test above int main() { unsigned int n = 50; unsigned int l = 2, r = 5; cout << toggleBitsFromLToR(n, l, r); return 0; }
Java
// Java implementation to toggle bits in // the given range import java.io.*; class GFG { // Function to toggle bits in the given range static int toggleBitsFromLToR(int n, int l, int r) { // calculating a number 'num' having 'r' // number of bits and bits in the range l // to r are the only set bits int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); // toggle bits in the range l to r in 'n' // and return the number // Besides this, we can calculate num as: // num=(1<<r)-l . return (n ^ num); } // driver program public static void main(String[] args) { int n = 50; int l = 2, r = 5; System.out.println(toggleBitsFromLToR(n, l, r)); } } // Contributed by Pramod Kumar
Python3
# Python implementation # to toggle bits in # the given range # function to toggle bits # in the given range def toggleBitsFromLToR(n, l, r): # calculating a number # 'num' having 'r' # number of bits and # bits in the range l # to r are the only set bits num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1) # toggle bits in the # range l to r in 'n' # Besides this, we can calculate num as: num=(1<<r)-l . # and return the number return (n ^ num) # Driver code n = 50 l = 2 r = 5 print(toggleBitsFromLToR(n, l, r)) # This code is contributed # by Anant Agarwal.
C#
// C# implementation to toggle bits // in the given range using System; namespace Toggle { public class GFG { // Function to toggle bits in the given range static int toggleBitsFromLToR(int n, int l, int r) { // calculating a number 'num' having 'r' // number of bits and bits in the range l // to r are the only set bits int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); // toggle bits in the range l to r in 'n' // Besides this, we can calculate num as: // num=(1<<r)-l . // and return the number return (n ^ num); } // Driver Code public static void Main() { int n = 50; int l = 2, r = 5; Console.Write(toggleBitsFromLToR(n, l, r)); } } } // This code is contributed by Sam007.
PHP
<?php // PHP implementation // to toggle bits in // the given range // function to toggle bits // in the given range function toggleBitsFromLToR($n, $l, $r) { // calculating a number // 'num' having 'r' // number of bits and // bits in the range l // to r are the only // set bits $num = ((1 << $r) - 1) ^ ((1 << ($l - 1)) - 1); // toggle bits in the // range l to r in 'n' //Besides this, we can calculate num as: $num=(1<<$r)-$l . // and return the number return ($n ^ $num); } // Driver Code $n = 50; $l = 2; $r = 5; echo toggleBitsFromLToR($n, $l, $r); // This code is contributed by anuj_67 ?>
Javascript
<script> // Javascript implementation to toggle bits in // the given range // function to toggle bits in the given range function toggleBitsFromLToR(n, l, r) { // calculating a number 'num' having 'r' // number of bits and bits in the range l // to r are the only set bits var num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); // toggle bits in the range l to r in 'n' // and return the number //Besides this, we can calculate num as: num=(1<<r)-l . return (n ^ num); } // Driver program to test above var n = 50; var l = 2, r = 5; document.write( toggleBitsFromLToR(n, l, r)); </script>
44
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Enfoque 2:
iterar sobre el rango dado de L a R y verificar si el i-ésimo bit está configurado o no. si el i-ésimo bit está activado, desactívelo; de lo contrario, actívelo.
C++
// C++ implementation to toggle bits in // the given range #include <bits/stdc++.h> using namespace std; // Function to toggle bits in the given range int toggleBitsFromLToR(int N, int L, int R) { int res = N; for (int i = L; i <= R; i++) { // Set bit if ((N & (1 << (i - 1))) != 0) { // XOR will set 0 to already set // bits(a^a=0) res = res ^ (1 << (i - 1)); } // unset bits else { // OR will set'0'bits to 1 res = res | (1 << (i - 1)); } } return res; } // Driver code int main() { int n = 50; int l = 2, r = 5; cout << toggleBitsFromLToR(n, l, r); return 0; } // This code is contributed by phasing17
Java
// Java implementation to toggle bits in // the given range import java.io.*; class GFG { // Function to toggle bits in the given range static int toggleBitsFromLToR(int N, int L, int R) { int res = N; for (int i = L; i <= R; i++) { // Set bit if ((N & (1 << (i - 1))) != 0) { // XOR will set 0 to already set // bits(a^a=0) res = res ^ (1 << (i - 1)); } // unset bits else { // OR will set'0'bits to 1 res = res | (1 << (i - 1)); } } return res; } // Driver method public static void main(String[] args) { int n = 50; int l = 2, r = 5; System.out.println(toggleBitsFromLToR(n, l, r)); } } // Contributed by Ocean Bhardwaj
Python3
# Python3 implementation to toggle bits in # the given range # Function to toggle bits in the given range def toggleBitsFromLToR(N, L, R): res = N for i in range(L, R + 1): # Set bit if ((N & (1 << (i - 1))) != 0): # XOR will set 0 to already set # bits(a^a=0) res = res ^ (1 << (i - 1)) # unset bits else: # OR will set'0'bits to 1 res = res | (1 << (i - 1)) return res # Driver code n = 50 l = 2 r = 5 print(toggleBitsFromLToR(n, l, r)) # This code is contributed by phasing17
C#
// C# implementation to toggle bits in // the given range using System; class GFG { // Function to toggle bits in the given range static int toggleBitsFromLToR(int N, int L, int R) { int res = N; for (int i = L; i <= R; i++) { // Set bit if ((N & (1 << (i - 1))) != 0) { // XOR will set 0 to already set // bits(a^a=0) res = res ^ (1 << (i - 1)); } // unset bits else { // OR will set'0'bits to 1 res = res | (1 << (i - 1)); } } return res; } // Driver Code public static void Main(string[] args) { int n = 50; int l = 2, r = 5; // Function call Console.WriteLine(toggleBitsFromLToR(n, l, r)); } } // This code is Contributed by phasing17
Javascript
// JavaScript implementation to toggle bits in // the given range // Function to toggle bits in the given range function toggleBitsFromLToR(N, L, R) { let res = N; for (let i = L; i <= R; i++) { // Set bit if ((N & (1 << (i - 1))) != 0) { // XOR will set 0 to already set // bits(a^a=0) res = res ^ (1 << (i - 1)); } // unset bits else { // OR will set'0'bits to 1 res = res | (1 << (i - 1)); } } return res; } // Driver code let n = 50; let l = 2, r = 5; console.log(toggleBitsFromLToR(n, l, r)); // This code is contributed by phasing17
44
Complejidad de Tiempo: O(R – L + 1)
Espacio Auxiliar: O(1)
Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA