'@HackerRank: FlippingBits. Wrong output
This one is really giving me a lot of hard time. So ultimately I decided to get some help from you guys. Perhaps, you can review it and tell me where I'm going wrong.
@ https://www.hackerrank.com/challenges/flipping-bits
Basically:
Input = Number
- Convert it to binary String.
- Flip the bits (Set all 0's and reset all 1's)
- Output the Number you get by doing it.
I have hard-coded a number for now, as it was mentioned in the question (= 2147483647)
Problem is: It is giving me an output of:
0
Code:
public class FlippingBits {
private static String flipBits(String binaryOrig){
String flippedString = "";
for(int i = 0; i<binaryOrig.length(); i++){
if(binaryOrig.charAt(i) == '1')
flippedString += '0';
else
flippedString += '1';
}
return flippedString;
}
private static long getNum(String flippedString){
long new_number = 0;
for(int i = 0; i < flippedString.length(); i++){
new_number = (2*new_number) + (flippedString.charAt(i) - '0');
}
return new_number;
}
public static void main(String[] args) {
long num = 2147483647;
String numBinaryString = Long.toBinaryString(num);
String flippedString = flipBits(numBinaryString);
System.out.println(Long.toString(getNum(flippedString)));
}
};
Could anyone tell me what's wrong! This is after trying for 3-4 hours. Sorry if it's trivial.
Thanks in advance.
EDIT: An excerpt from the Question:
"Take 1 for example, as unsigned 32-bits is
00000000000000000000000000000001
and doing the flipping we get
11111111111111111111111111111110
which in turn is 4294967294"
So, I'm having a feeling it has to do something with long being 64-bits. !?
Solution 1:[1]
The way you are converting the number to a string:
Long.toBinaryString(num)
Will not prefix the number with 0s, so for instance 3 will be 11 instead of 00000000000000000000000000000011. Because of this your flipping method won't work the way the guys from hackerrank want it to work.
Notice that the question states the integers are 32-bit long (somewhere and not very clearly but oh well) so you need to take that into account.
@Edit: the easiest way to go around padding this would be:
numBinaryString = String.format("%32s",numBinaryString).replace(" ", "0")
Solution 2:[2]
Nothing wrong with the code. If you try to convert the long number 2147483647 to a binary number you will get 1111111111111111111111111111111.
And when you flip it, you get zero which is correct.
Apart from this, instead of manually creating decimal from binary number using getNum, you could use:
System.out.println(Long.parseLong(flippedString, 2));
I would suggest first you try with smaller number like 10 and see how you get 5 out of it.
Solution 3:[3]
You have got some answers already but let me give you an alternative way.
Use a long to store the value and then you can XOR it with 0xffffffffL (11111111111111111111111111111111 in binary). That will give you the correct answer.
For example:
00000000000000000000000000000001
11111111111111111111111111111111 ^
--------------------------------
11111111111111111111111111111110
Below you can find the code. Note that I use custom IO methods.
long n = in.readLong() ^ 0xffffffffL;
out.print(n + "\n");
Solution 4:[4]
public static long flippingBits(long n)
{
var binary = Convert.ToString(n,2).PadLeft(32,'0');
var reverse = "";
foreach(var item in binary){
if(item == '1'){
reverse = reverse + '0';
}
else{
reverse = reverse + '1';
}
}
return Convert.ToInt64(reverse,2);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | SMA |
| Solution 3 | Orestis |
| Solution 4 | Sai |
