Skip to content

Commit efa21c8

Browse files
author
Ram swaroop
committed
multiply with 3.5 done
1 parent 07a629b commit efa21c8

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/me/ramswaroop/bits/Multiply.java

+26-10
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
public class Multiply {
1818

1919
/**
20-
* Multiplies a number with 8 by performing 3 left shifts.
20+
* Multiplies {@param n} with 3.5 and returns only the
21+
* integral part of the number and omits the fractional part.
2122
*
2223
* @param n
2324
* @return
2425
*/
25-
public static long multiplyBy8(long n) {
26-
return (n << 3);
26+
public static long multiplyWith3point5(long n) {
27+
return (n << 1) + n + (n >> 1); // n*2 + n + n/2 = 7n/2 = 3.5n
2728
}
2829

2930
/**
@@ -34,16 +35,31 @@ public static long multiplyBy8(long n) {
3435
* @param n
3536
* @return
3637
*/
37-
public static long multiplyBy7(long n) {
38+
public static long multiplyWith7(long n) {
3839
return (n << 3) - n;
3940
}
4041

42+
/**
43+
* Multiplies a number with 8 by performing 3 left shifts.
44+
*
45+
* @param n
46+
* @return
47+
*/
48+
public static long multiplyWith8(long n) {
49+
return (n << 3);
50+
}
51+
4152
public static void main(String a[]) {
42-
System.out.println(multiplyBy7(4));
43-
System.out.println(multiplyBy7(6));
44-
System.out.println(multiplyBy7(7));
45-
System.out.println(multiplyBy8(4));
46-
System.out.println(multiplyBy8(6));
47-
System.out.println(multiplyBy8(7));
53+
System.out.println(multiplyWith3point5(3));
54+
System.out.println(multiplyWith3point5(4));
55+
System.out.println(multiplyWith3point5(6));
56+
System.out.println(multiplyWith3point5(-7));
57+
58+
System.out.println(multiplyWith7(6));
59+
System.out.println(multiplyWith7(7));
60+
61+
System.out.println(multiplyWith8(4));
62+
System.out.println(multiplyWith8(6));
63+
System.out.println(multiplyWith8(7));
4864
}
4965
}

src/me/ramswaroop/practice/MethodOverloading.java

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
class MethodOverloading {
1111

12+
static void go(float x) {
13+
System.out.print("float ");
14+
}
15+
1216
static void go(Long x) {
1317
System.out.print("Long ");
1418
}

0 commit comments

Comments
 (0)