Multiply a number by 4 using bitwise operator
In this article, we will explore how to multiply a given number by 4 using bitwise operators in programming.
Bitwise operators perform operations at the bit level, allowing us to perform some arithmetic operations
efficiently. In particular, we will use the left shift operator (<<
) to achieve the
multiplication by 4.
Problem Statement and Explanation with Suitable Example
The problem is to multiply a given integer num
by 4 using bitwise operators. To understand how this
works, let's take an example:
Example
Let's take num = 6
.
To multiply 6 by 4, we can simply use the standard multiplication operator (*
) and get the result as 6 * 4 = 24
. However, we want to achieve the same result using bitwise operators.
Pseudocode
Start
Input an integer num.
Left shift num by 2 (equivalent to multiplying by 2^2, i.e., 4).
Store the result in a variable result.
Output the result as (num x 4 = result).
End
Explanation
The key operation in this algorithm is the left shift operation (<<
). When you shift a binary
number to the left by n
positions, it is equivalent to multiplying the number by 2^n.
For example, let's consider num = 6
, which is represented in binary as 110
.
To multiply num
by 4, we will left shift num
by 2 positions:
num = 110
num << 2
Result: 11000 (which is 24 in decimal)
As you can see, the result after the left shift is 11000
, which is equal to 24
in decimal.
Therefore, using the left shift operator, we have effectively multiplied num
by 4.
Code Solution
// C Program
// Multiply a number by 4 using bitwise operator
#include <stdio.h>
void multiplyBy4(int num)
{
// Shift given number to left by 2
int result = ((num) << 2);
// Display calculated result
printf(" (%d x 4) %d\n", num, result);
}
int main()
{
// Test
multiplyBy4(6);
multiplyBy4(10);
multiplyBy4(7);
multiplyBy4(-3);
return 0;
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
/*
Java program
Multiply a number by 4 using bitwise operator
*/
public class Multiplication
{
public void multiplyBy4(int num)
{
// Shift given number to left by 2
int result = ((num) << 2);
// Display calculated result
System.out.println(" (" + num + " x 4) " + result);
}
public static void main(String[] args)
{
Multiplication task = new Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Multiply a number by 4 using bitwise operator
*/
class Multiplication
{
public: void multiplyBy4(int num)
{
// Shift given number to left by 2
int result = ((num) << 2);
// Display calculated result
cout << " (" << num << " x 4) " << result << endl;
}
};
int main()
{
Multiplication *task = new Multiplication();
// Test
task->multiplyBy4(6);
task->multiplyBy4(10);
task->multiplyBy4(7);
task->multiplyBy4(-3);
return 0;
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
// Include namespace system
using System;
/*
Csharp program
Multiply a number by 4 using bitwise operator
*/
public class Multiplication
{
public void multiplyBy4(int num)
{
// Shift given number to left by 2
int result = ((num) << 2);
// Display calculated result
Console.WriteLine(" (" + num + " x 4) " + result);
}
public static void Main(String[] args)
{
Multiplication task = new Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
package main
import "fmt"
/*
Go program
Multiply a number by 4 using bitwise operator
*/
func multiplyBy4(num int) {
// Shift given number to left by 2
var result int = ((num) << 2)
// Display calculated result
fmt.Println("(", num, "x 4 )", result)
}
func main() {
// Test
multiplyBy4(6)
multiplyBy4(10)
multiplyBy4(7)
multiplyBy4(-3)
}
Output
( 6 x 4 ) 24
( 10 x 4 ) 40
( 7 x 4 ) 28
( -3 x 4 ) -12
<?php
/*
Php program
Multiply a number by 4 using bitwise operator
*/
class Multiplication
{
public function multiplyBy4($num)
{
// Shift given number to left by 2
$result = (($num) << 2);
// Display calculated result
echo(" (".$num.
" x 4) ".$result.
"\n");
}
}
function main()
{
$task = new Multiplication();
// Test
$task->multiplyBy4(6);
$task->multiplyBy4(10);
$task->multiplyBy4(7);
$task->multiplyBy4(-3);
}
main();
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
/*
Node JS program
Multiply a number by 4 using bitwise operator
*/
class Multiplication
{
multiplyBy4(num)
{
// Shift given number to left by 2
var result = ((num) << 2);
// Display calculated result
console.log(" (" + num + " x 4) " + result);
}
}
function main()
{
var task = new Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
main();
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
# Python 3 program
# Multiply a number by 4 using bitwise operator
class Multiplication :
def multiplyBy4(self, num) :
# Shift given number to left by 2
result = ((num) << 2)
# Display calculated result
print(" (", num ," x 4) ", result)
def main() :
task = Multiplication()
# Test
task.multiplyBy4(6)
task.multiplyBy4(10)
task.multiplyBy4(7)
task.multiplyBy4(-3)
if __name__ == "__main__": main()
Output
( 6 x 4) 24
( 10 x 4) 40
( 7 x 4) 28
( -3 x 4) -12
# Ruby program
# Multiply a number by 4 using bitwise operator
class Multiplication
def multiplyBy4(num)
# Shift given number to left by 2
result = ((num) << 2)
# Display calculated result
print(" (", num ," x 4) ", result, "\n")
end
end
def main()
task = Multiplication.new()
# Test
task.multiplyBy4(6)
task.multiplyBy4(10)
task.multiplyBy4(7)
task.multiplyBy4(-3)
end
main()
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
/*
Scala program
Multiply a number by 4 using bitwise operator
*/
class Multiplication()
{
def multiplyBy4(num: Int): Unit = {
// Shift given number to left by 2
var result: Int = ((num) << 2);
// Display calculated result
println(" (" + num + " x 4) " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiplication = new Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
/*
Swift 4 program
Multiply a number by 4 using bitwise operator
*/
class Multiplication
{
func multiplyBy4(_ num: Int)
{
// Shift given number to left by 2
let result: Int = ((num) << 2);
// Display calculated result
print(" (", num ," x 4) ", result);
}
}
func main()
{
let task: Multiplication = Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
main();
Output
( 6 x 4) 24
( 10 x 4) 40
( 7 x 4) 28
( -3 x 4) -12
/*
Kotlin program
Multiply a number by 4 using bitwise operator
*/
class Multiplication
{
fun multiplyBy4(num: Int): Unit
{
// Shift given number to left by 2
val result: Int = ((num) shl 2);
// Display calculated result
println(" (" + num + " x 4) " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiplication = Multiplication();
// Test
task.multiplyBy4(6);
task.multiplyBy4(10);
task.multiplyBy4(7);
task.multiplyBy4(-3);
}
Output
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
Resultant Output Explanation
Let's run the provided C code and understand the output:
Output:
(6 x 4) 24
(10 x 4) 40
(7 x 4) 28
(-3 x 4) -12
The output shows the result of multiplying the given numbers by 4 using bitwise operations.
- For
num = 6
, the result is6 x 4 = 24
, which is correct. - For
num = 10
, the result is10 x 4 = 40
, which is correct as well. - For
num = 7
, the result is7 x 4 = 28
, which is also correct. - For
num = -3
, the result is-3 x 4 = -12
, which is the correct result. In C, the left shift operator works with signed integers as well, and it effectively multiplies the negative number by 4.
Time Complexity of the Code
The time complexity of the code is constant (O(1)
). This is because the left shift operation is
performed in a fixed number of steps (2 positions in this case) regardless of the size of the input number. The
bitwise operations take constant time, so the time complexity of the entire code remains constant.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment