Skip to content

Commit 5e4fe36

Browse files
author
Ram swaroop
committed
abs without branching : comments added
1 parent ed52e7d commit 5e4fe36

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/me/ramswaroop/bits/AbsWithoutBranching.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public class AbsWithoutBranching {
1111

1212
/**
1313
* Returns the absolute value of any integer.
14+
* <p/>
15+
* EXPLANATION:
16+
* For example, consider int takes 4 bits:
17+
* So for input = -5, we have
18+
* -5 = 1011
19+
* mask = 1111
20+
* mask + n = 1010
21+
* (mask + n)^mask = 0101 (which is 5)
1422
*
1523
* @param n
1624
* @return
@@ -27,12 +35,3 @@ public static void main(String a[]) {
2735
System.out.println(abs(-0));
2836
}
2937
}
30-
31-
/**
32-
* For example, consider int takes 4 bits:
33-
* So for input = -5, we have
34-
* -5 = 1011
35-
* mask = 1111
36-
* mask + n = 1010
37-
* (mask + n)^mask = 0101 (which is 5)
38-
*/

0 commit comments

Comments
 (0)