Calcule n módulo d sin operadores de división(/) y módulo(%), donde d es una potencia de 2 números.
Input: 6 4 Output: 2 Explanation: As 6%4 = 2 Input: 12 8 Output: 4 Explanation: As 12%8 = 4 Input: 10 2 Output: 0 Explanation:As 10%2 = 0
Let i th bit desde la derecha se establece en d. Para obtener n módulo d, solo necesitamos devolver 0 a i -1 (desde la derecha) bits de n tal como están y otros bits como 0.
Por ejemplo, si n = 6 (00..110) y d = 4 (00) ..100). El último bit establecido en d está en la posición 3 (desde el lado derecho). Entonces necesitamos devolver los últimos dos bits de n tal como están y otros bits como 0, es decir, 00..010.
Ahora hacerlo es muy fácil, adivinen….
Sí, has adivinado bien. Vea el programa a continuación.
C++
#include<iostream> using namespace std; // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … unsigned int getModulo(unsigned int n, unsigned int d) { return ( n & (d - 1) ); } // Driver Code int main() { unsigned int n = 6; // d must be a power of 2 unsigned int d = 4; cout<< n <<" modulo "<<d <<" is "<< getModulo(n, d); getchar(); return 0; } // this code is contributed by shivanisinghss2110
C
#include<stdio.h> // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … unsigned int getModulo(unsigned int n, unsigned int d) { return ( n & (d - 1) ); } // Driver Code int main() { unsigned int n = 6; // d must be a power of 2 unsigned int d = 4; printf("%u modulo %u is %u", n, d, getModulo(n, d)); getchar(); return 0; }
Java
// Java code for Compute modulus division by // a power-of-2-number class GFG { // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, static int getModulo(int n, int d) { return ( n & (d-1) ); } // Driver Code public static void main(String[] args) { int n = 6; /*d must be a power of 2*/ int d = 4; System.out.println(n+" modulo " + d + " is " + getModulo(n, d)); } } // This code is contributed // by Smitha Dinesh Semwal.
Python3
# Python code to demonstrate # modulus division by power of 2 # This function will # return n % d. # d must be one of: # 1, 2, 4, 8, 16, 32, … def getModulo(n, d): return ( n & (d-1) ) # Driver program to # test above function n = 6 #d must be a power of 2 d = 4 print(n,"modulo",d,"is", getModulo(n, d)) # This code is contributed by # Smitha Dinesh Semwal
C#
// C# code for Compute modulus // division by a power-of-2-number using System; class GFG { // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … static uint getModulo( uint n, uint d) { return ( n & (d-1) ); } // Driver code static public void Main () { uint n = 6; uint d = 4; /*d must be a power of 2*/ Console.WriteLine( n + " modulo " + d + " is " + getModulo(n, d)); } } // This code is contributed by vt_m.
PHP
<?php // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … function getModulo($n, $d) { return ( $n & ($d - 1) ); } // Driver Code $n = 6; // d must be a power of 2 $d = 4; echo $n ," modulo"," ", $d, " is ", " ",getModulo($n, $d); // This code is contributed by vt_m. ?>
Javascript
<script> // This function will return n % d. // d must be one of: 1, 2, 4, 8, 16, 32, … function getModulo(n,d) { return ( n & (d - 1) ); } // Driver Code n = 6; d = 4; document.write(n +" modulo "+ d + " is "+ getModulo(n, d)); // This code is contributed by simranarora5sos </script>
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