1. ## alert(x ^ y) ; // 1

Message box tell me 7 or 1. What is ^ doing? Thanks...
Code:

<script type="text/javascript">
var x = 2 ;
var y = 5 ;

alert(x ^ y) ; // 7
alert(y ^ x) ; // 7

//x ^= y means x = x ^ y
</script>
I change value of y;
Code:

<script type="text/javascript">
var x = 2 ;
var y = 3 ;

alert(x ^ y) ; // 1
alert(y ^ x) ; // 1

</script>

2. It's an XOR (exclusive OR) bitwise operator - the numbers are converted to binary, then XOR'd and the result is converted back into decimal:

2 ^ 5 == 010 ^ 101 == 111 == 7

2 ^ 3 == 10 ^ 11 == 01 == 1

Thanks.

I want to show 2 is equal to 010 in binary. Is it found with Javascript?
A message box will be seen and it will tell me "2 is to equal 010"

I find it with php:
Code:
<?php

echo decbin("7");  // 111

?>
Code:

<script type="text/javascript">

alert(<?php echo decbin("7") ?> ) ;  // 111

</script>

4. Code:
alert("2 is equal to " + Number(2).toString(2));
The argument to toString() is the radix, in this case 2 for binary.

5. AutisticCuckoo
Thanks. It does.
I want same result without using ^
I wrote this, but it don't do.
Code:

<script type="text/javascript">

function findIt () {

var inp1 = document.getElementById('inp1');

var inp2 = document.getElementById('inp2');

var div1 = document.getElementById('div1');

var div2 = document.getElementById('div2');

var div3 = document.getElementById('div3');

var n1 = Number(inp1.value).toString(2) ;

var n2 = Number(inp2.value).toString(2) ;

div1.innerHTML = n1 ;

div2.innerHTML = n2 ;

var a = n1.substring(0,inp1.length) ;

var b = n2.substring(0,inp2.length) ;

var len = (inp1.length > inp2.length) ? inp1.length: inp2.length ;

for(var i = 0 ; i< len; i++ ) {

if( a.charAt(i) == b.charAt(i) && a.charAt(i)==1 )  {  div3.insertBefore(0,div3.childNodes[0] ) ;}

if( a.charAt(i) == b.charAt(i) && a.charAt(i)==0 )  {  div3.insertBefore(0,div3.childNodes[0] ) ;}

if( a.charAt(i) != b.charAt(i) )  {  div3.insertBefore(1,div3.childNodes[0] ) ;}

}

}

</script>

Please, enter some number: <input type="text" id="inp1" onkeyup="findIt ()"><div id="div1"></div><br>

Please, enter some number: <input type="text" id="inp2"  onkeyup="findIt ()"><div id="div2"></div><br>

Result: <div id="div2">&nbsp;</div><br>

<button onclick="alert(inp1.value ^ inp2.value) ">click me </button>

6. You have two DIVS with id="div2" in there.

Also, the insertBefore() DOM function takes two Node objects as parameters: insertBefore(newNode, oldNode). If you are trying to insert '0' and '1' characters into the DIV, this is not the way to do it. Instead, prepend those characters to a string, and assign that string as the content of the DIV when it's finished:
Code:
div3.replaceChild(document.createTextNode(binaryString), div3.firstChild);
(where binaryString is the string you've created.)

7. How is conversion from binary to decimal? Is there short code? The codes I found, are long.

Code:
5 |  2
|----
4 |  2 | 2
|    |---
-____   2 | 1
1     -___
0
With PHP :
<?php

echo bindec("101") ; // 5

?>

With js?

8. parseInt("101", 2);

9. The primary purpose for the parseInt and parseFloat functions in javascript is conversion of numbers from different number bases to base 10. The second parameter passed to the function identifies the base of the first parameter. Where a second parameter is not specified those values in the first parameter that start with 0 are considered to be base 8 and those that start with 0x are considered to be base 16.

Use of these functions to extract numbers from strings (the most common thing they appear to be used for) is making use of a side effect of what the function does and generally leads to confusion where the function decides that the input value is not base 10.

10. Originally Posted by felgall
Where a second parameter is not specified those values in the first parameter that start with 0 are considered to be base 8 and those that start with 0x are considered to be base 16.
The recognition of octal values (starting with 0) cannot be relied upon.

According to the ECMA-262 specification, section 15.1.2.2 (parseInt):
If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed.
No mention of automatic octal detection.

In section 15.7.4.2 (Number.prototype.toString):
If radix is an integer from 2 to 36, but not 10, the result is a string, the choice of which is implementation-dependent.
This is from the 3rd edition, though, so it may have been changed in later revisions of the spec.

11. Many current browsers still assume the older standard where numbers starting with 0 are assumed to be octal. Dropping octal from the standard is still to recent to rely on its not being there - perhaps in another 10 years time we will be able to.

The most common problem that people have using parseInt is with dates having leading zeros is that the function drops the 8 or 9 in most browsers and since they don't realise that parseInt's main purpose is to convert number bases and that until the standard was changed recently that numbers starting with zero were presumed to be octal they have no clue as to why they have a problem with 8 and 9.

While you can't rely on numbers starting with zero being treated as octal (as they were under the old standard), you can't rely on the new standard yet either. The safest solution is to always specify the second parameter for both parseInt and parseFloat so that the function knows what number base you want rather than trying to work it out from the content of the first parameter.

12. My point was that you should always specify an explicit radix, to avoid nasty surprises.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•