Created
January 29, 2015 10:50
-
-
Save thomascrha/06050ea0d9484e804237 to your computer and use it in GitHub Desktop.
Flipping Bits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Problem Statement | |
You will be given a list of 32-bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. unset bits must be set, and set bits must be unset). | |
Input Format | |
The first line of the input contains the list size T. T lines follow each line having an integer from the list. | |
Constraints | |
1 ≤ T ≤ 100*/ | |
function createBinaryString (numInteger) { | |
for (var numIndex = 0, numString = ""; | |
numIndex < 32; | |
numIndex++, numString += String(numInteger >>> 31), numInteger <<= 1); | |
return numString; | |
} | |
function flipBits(stringBinary){ | |
String.prototype.setCharAt = function(idx, chr) { | |
if(idx > this.length - 1){ | |
return this.toString(); | |
} else { | |
return this.substr(0, idx) + chr + this.substr(idx + 1); | |
} | |
}; | |
var str = stringBinary; | |
for(var i =0;i<=str.length;i++){ | |
if(str.charAt(i) == '0'){ | |
str = str.setCharAt(i,"1"); | |
}else if (str.charAt(i) == '1'){ | |
str = str.setCharAt(i,"0");} | |
}return str; | |
} | |
function main(){ | |
var listLen = parseInt(readLine()); | |
if(1 <= listLen <= 100){ | |
for(var i = 1;i<=listLen;i++){ | |
var num = parseInt(readLine()); | |
var output = flipBits(createBinaryString(num)); | |
console.log(parseInt(output ,2)); | |
} | |
}else{return null;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment