Reputation: 1723
I have the following string in JavaScript:
"1011100111100110101110110"
.
I want to perform an operation on it, that will return its complement (aka, all the ones replaced by zeros and all the zeroes replaced by ones).
The javascript documentation says the NOT operator will do exactly that, but in my tests, it does not give me what I expect.
I guess my types are wrong to start off with.
This is my test code:
var nMyNumber = "1011100111100110101110110";
var sBinString = nMyNumber.toString(2);
console.log("Number: " + sBinString);
var reverse = ~sBinString;
console.log("Complement: " + reverse);
Upvotes: 1
Views: 1406
Reputation: 2972
How about a simpler approach ->
var nMyNumber = "1011100111100110101110110";
nMyNumber = nMyNumber.split("");
var myNumber = [];
for(var i=0;i<nMyNumber.length;i++) {
if(nMyNumber[i] == 0) {
myNumber.push(1);
}
if(nMyNumber[i] == 1) {
myNumber.push(0);
}
}
myNumber.join("");
console.log(myNumber);
Upvotes: -1
Reputation: 115262
The ~
Bitwise operator doesn't give you the expected output since which performs NOT operation on each bit in a signed 32bit representation and the result is converted back to the normal number so you don't get the expected result.
From MDN docs :
The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format. Two's complement format means that a number's negative counterpart (e.g. 5 vs. -5) is all the number's bits inverted
Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6.
Even though it's not going to work as expected, there are some bugs in your code:
nMyNumber.toString(2)
won't do anything, which returns the same string. So you need to convert into number using parseInt
with a radix of 2
(for binary).reverse
would hold a number which logs as in a decimal format, to convert into binary use toString(2)
(where 2 stands for the binary format, eg reverse.toString(2)
).Use String#replace
with a callback function and within the function return character based on the matched value.
var nMyNumber = "1011100111100110101110110";
console.log(
nMyNumber.replace(/[01]/g, function(m) {
return m == 0 ? '1' : 0;
})
)
// with ES6 arrow function
console.log(
nMyNumber.replace(/[01]/g, m => m == 0 ? '1' : 0)
)
Upvotes: 0
Reputation: 18838
Your casting is wrong. One solution can be using map
:
var nMyNumber = "1011100111100110101110110";
var reverse = nMyNumber.split('').map(x => x === "0" ? "1" : "0").join('');
console.log("Number: " + reverse);
Upvotes: 3
Reputation: 41271
If you actually want a number, first convert your string into one:
var x_as_binary_string = "1011100111100110101110110"
var x = parseInt(x_as_binary_string, 2);
Now to get this number's complement, use NOT as you said:
var complement = ~ x;
If you need it converted back into a binary string:
var complement_as_string = complement.toString(2);
Upvotes: 1
Reputation: 49921
Strings do not have complements. Numbers can have binary complements. If you want the string that would result from complementing the binary value that happens to be in a string, you will have to write your own function to do that. One way to do that would be to compute the value your binary string represents, complement that, and convert that new number to a binary string.
Upvotes: 0
Reputation: 5272
just do string replacement
var sBinString = nMyNumber
.replace(/1/g,'x')//convert '1' to temp char('x')
.replace(/0/g,'1')//convert '0' to '1'
.replace(/x/g,'0')//finally convert temp char to '0'
Upvotes: 4