Somos un Número n y nuestra tarea es multiplicar el número por 4 usando el Operador bit a bit.
Ejemplos:
Input : 4 Output :16 Input :5 Output :20
Explicación Caso 1: – n=4 el binario de 4 es 100 y ahora cambia dos bits a la derecha, luego 10000 ahora el número es 16 que se multiplica 4*4=16 ans.
Enfoque :- (n<<2) desplazar dos bits a la derecha
C++
// C++ program to multiply a number with // 4 using Bitwise Operator #include <bits/stdc++.h> using namespace std; // function the return multiply a number // with 4 using bitwise operator int multiplyWith4(int n) { // returning a number with multiply // with 4 using2 bit shifting right return (n << 2); } // derive function int main() { int n = 4; cout << multiplyWith4(n) << endl; return 0; }
Java
// Java program to multiply a number // with 4 using Bitwise Operator class GFG { // function the return // multiply a number // with 4 using bitwise // operator static int multiplyWith4(int n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code public static void main(String[] args) { int n = 4; System.out.print(multiplyWith4(n)); } } // This code is contributed by Smitha.
Python 3
# Python 3 program to multiply # a number with 4 using Bitwise # Operator # function the return multiply # a number with 4 using bitwise # operator def multiplyWith4(n): # returning a number with # multiply with 4 using2 # bit shifting right return (n << 2) # derive function n = 4 print(multiplyWith4(n)) # This code is contributed # by Smitha
C#
// C# program to multiply a number // with 4 using Bitwise Operator using System; class GFG { // function the return // multiply a number // with 4 using bitwise // operator static int multiplyWith4(int n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code public static void Main(String[] args) { int n = 4; Console.Write(multiplyWith4(n)); } } // This code is contributed by Smitha.
PHP
<?php // PHP program to multiply // a number with 4 using // Bitwise Operator // function the return // multiply a number // with 4 using bitwise // operator function multiplyWith4($n) { // returning a number // with multiply with // 4 using2 bit // shifting right return ($n << 2); } // Driver Code $n = 4; echo multiplyWith4($n),"\n"; // This code is contributed by Ajit. ?>
Javascript
<script> // javascript program to multiply a number // with 4 using Bitwise Operator // function the return // multiply a number // with 4 using bitwise // operator function multiplyWith4(n) { // returning a number // with multiply with // 4 using 2 bit shifting // right return (n << 2); } // Driver Code var n = 4; document.write(multiplyWith4(n)); // This code is contributed by Amit Katiyar </script>
Producción :-
16
Generalización: en general, podemos multiplicar con una potencia de 2 usando operadores bit a bit. Por ejemplo, supongamos que deseamos multiplicar por 16 (que es 2 4 ), podemos hacerlo desplazando a la izquierda por 4.
Publicación traducida automáticamente
Artículo escrito por Shubham_Raj_Kumawat y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA