-3
var n = prompt("Digite um numero");
if(n & 1){
alert("Impar");
} else {
alert("Par");
}
alert(n);
This code block is very simple, but the focus is on this operator &
. What’s he good for? and in the code above he’s doing what n & 1
?
-3
var n = prompt("Digite um numero");
if(n & 1){
alert("Impar");
} else {
alert("Par");
}
alert(n);
This code block is very simple, but the focus is on this operator &
. What’s he good for? and in the code above he’s doing what n & 1
?
-2
According to the MDN:
Bit-to-bit operators are operators treated as 32 sequence bits ( zeros and ones ), preferably as decimal, hexadecimal, or octal numbers. For example, the decimal number 9 had as 1001 binary representation. Bit-to-bit operators perform operations on such binary representations, but return values numeric in the Javascript pattern
.
console.log(5 & 13); // 0101 & 1101 = 0101
// expected output: 5;
console.log(parseInt("0101",2) & parseInt("1101",2));
// expected output: 5;
console.log(5 & 13 & 3); // 0101 & 1101 & 0011 = 0001
// expected output: 1;
console.log(5 | 13); // 0101 | 1101 = 1101
// expected output: 13
....Operator...... Use......... Description
Bitwise AND........ a & b... Returns 1 at each bit position for which the
......................................... corresponding bit of both were 1s.
Source: Bitwise Operators
Browser other questions tagged javascript html
You are not signed in. Login or sign up in order to post.