Multiply a number by 8 using bitwise operator
Here given code implementation process.
// C Program
// Multiply a number by 8 using bitwise operator
#include <stdio.h>
void multiplyBy8(int num)
{
// Shift given number to left by 3
int result = ((num) << 3);
// Display calculated result
printf(" (%d x 8) %d\n", num, result);
}
int main()
{
// Test
multiplyBy8(6);
multiplyBy8(10);
multiplyBy8(7);
multiplyBy8(-3);
return 0;
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
/*
Java program
Multiply a number by 8 using bitwise operator
*/
public class Multiplication
{
public void multiplyBy8(int num)
{
// Shift given number to left by 3
int result = ((num) << 3);
// Display calculated result
System.out.println(" (" + num + " x 8) " + result);
}
public static void main(String[] args)
{
Multiplication task = new Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Multiply a number by 8 using bitwise operator
*/
class Multiplication
{
public: void multiplyBy8(int num)
{
// Shift given number to left by 3
int result = ((num) << 3);
// Display calculated result
cout << " (" << num << " x 8) " << result << endl;
}
};
int main()
{
Multiplication *task = new Multiplication();
// Test
task->multiplyBy8(6);
task->multiplyBy8(10);
task->multiplyBy8(7);
task->multiplyBy8(-3);
return 0;
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
// Include namespace system
using System;
/*
Csharp program
Multiply a number by 8 using bitwise operator
*/
public class Multiplication
{
public void multiplyBy8(int num)
{
// Shift given number to left by 3
int result = ((num) << 3);
// Display calculated result
Console.WriteLine(" (" + num + " x 8) " + result);
}
public static void Main(String[] args)
{
Multiplication task = new Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
package main
import "fmt"
/*
Go program
Multiply a number by 8 using bitwise operator
*/
func multiplyBy8(num int) {
// Shift given number to left by 3
var result int = ((num) << 3)
// Display calculated result
fmt.Println(" (", num, " x 8) ", result)
}
func main() {
// Test
multiplyBy8(6)
multiplyBy8(10)
multiplyBy8(7)
multiplyBy8(-3)
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
<?php
/*
Php program
Multiply a number by 8 using bitwise operator
*/
class Multiplication
{
public function multiplyBy8($num)
{
// Shift given number to left by 3
$result = (($num) << 3);
// Display calculated result
echo(" (".$num.
" x 8) ".$result.
"\n");
}
}
function main()
{
$task = new Multiplication();
// Test
$task->multiplyBy8(6);
$task->multiplyBy8(10);
$task->multiplyBy8(7);
$task->multiplyBy8(-3);
}
main();
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
/*
Node JS program
Multiply a number by 8 using bitwise operator
*/
class Multiplication
{
multiplyBy8(num)
{
// Shift given number to left by 3
var result = ((num) << 3);
// Display calculated result
console.log(" (" + num + " x 8) " + result);
}
}
function main()
{
var task = new Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
main();
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
# Python 3 program
# Multiply a number by 8 using bitwise operator
class Multiplication :
def multiplyBy8(self, num) :
# Shift given number to left by 3
result = ((num) << 3)
# Display calculated result
print(" (", num ," x 8) ", result)
def main() :
task = Multiplication()
# Test
task.multiplyBy8(6)
task.multiplyBy8(10)
task.multiplyBy8(7)
task.multiplyBy8(-3)
if __name__ == "__main__": main()
Output
( 6 x 8) 48
( 10 x 8) 80
( 7 x 8) 56
( -3 x 8) -24
# Ruby program
# Multiply a number by 8 using bitwise operator
class Multiplication
def multiplyBy8(num)
# Shift given number to left by 3
result = ((num) << 3)
# Display calculated result
print(" (", num ," x 8) ", result, "\n")
end
end
def main()
task = Multiplication.new()
# Test
task.multiplyBy8(6)
task.multiplyBy8(10)
task.multiplyBy8(7)
task.multiplyBy8(-3)
end
main()
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
/*
Scala program
Multiply a number by 8 using bitwise operator
*/
class Multiplication()
{
def multiplyBy8(num: Int): Unit = {
// Shift given number to left by 3
var result: Int = ((num) << 3);
// Display calculated result
println(" (" + num + " x 8) " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiplication = new Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
/*
Swift 4 program
Multiply a number by 8 using bitwise operator
*/
class Multiplication
{
func multiplyBy8(_ num: Int)
{
// Shift given number to left by 3
let result: Int = ((num) << 3);
// Display calculated result
print(" (", num ," x 8) ", result);
}
}
func main()
{
let task: Multiplication = Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
main();
Output
( 6 x 8) 48
( 10 x 8) 80
( 7 x 8) 56
( -3 x 8) -24
/*
Kotlin program
Multiply a number by 8 using bitwise operator
*/
class Multiplication
{
fun multiplyBy8(num: Int): Unit
{
// Shift given number to left by 3
val result: Int = ((num) shl 3);
// Display calculated result
println(" (" + num + " x 8) " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiplication = Multiplication();
// Test
task.multiplyBy8(6);
task.multiplyBy8(10);
task.multiplyBy8(7);
task.multiplyBy8(-3);
}
Output
(6 x 8) 48
(10 x 8) 80
(7 x 8) 56
(-3 x 8) -24
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