forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlippingBits.java
49 lines (44 loc) · 1.02 KB
/
FlippingBits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.rampatra.bits;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/5/15
* @time: 3:50 PM
*/
public class FlippingBits {
/**
* Returns the number by flipping/inverting its bits using
* XOR operation with 1......11 (size of int i.e, 32 1's).
*
* @param n
* @return
*/
public static int getNumberByFlippingBits(int n) {
return n ^ 0xffffffff; // equivalent to 11....1 (32 times) in binary
}
/**
* Returns the number by flipping/inverting its bits using
* the NOT operator.
*
* @param n
* @return
*/
public static int getNumberByFlippingBits_V1(int n) {
return ~n;
}
public static void main(String[] args) {
System.out.println(getNumberByFlippingBits(5));
System.out.println(getNumberByFlippingBits_V1(5));
}
}
/**
* EXPLANATION:
* <p>
* For input: 5
* <p>
* Binary: 000.....101
* Inverted: 111.....010 (which is the 2's compliment of -6)
* <p>
* Therefore, result is -6.
*/