Trouble figuring out Javascript & operator - javascript

So I am new to Javascript, in my first Intro to Javascript class. I just found what the & operator does and came across this definition:
The & operator returns a one in each bit position for which the corresponding bits of both operands are ones.
I was also able to find descriptions of == and === on this website on a question that has been previously answered. On this link here: Wikipedia Bitwise_operation#AND
It explains that 1 & 1 is the same as 1 x 1, simple multiplication. So my question is then why is 10 & 5 == 0 and 10 & 6 == 2
Wouldn't it be 10 & 5 == 50 and 10 & 6 == 60?
What am I failing to understand?

It's only the binary bits in each position (the 1s and 0s) that are multiplied.
For example, with 10 & 5:
10 = 1010 in binary
5 = 0101 in binary
Now multiply each digit against the other digit in the same position:
(1 x 0) (0 x 1) (1 x 0) (0 x 1)
= 0000
= 0 in decimal
console.log(10 & 5)
With 10 & 6:
10 = 1010 in binary
6 = 0110 in binary
Now multiply each digit against the other digit in the same position:
(1 x 0) (0 x 1) (1 x 1) (0 x 0)
= 0010
= 2 in decimal
console.log(10 & 6)

It’s equivalent to multiplication per bit.
0 & 0 === 0
0 & 1 === 0
1 & 1 === 1
So your example of 10 & 5 is:
1010
& 0101
= 0000

If you switch from base 10 to base 2, which is required when comparing numbers bitwise, then it is clearer :
10 & 5 becomes 1010 & 0101 which equals 0000 in base 2, 0 in base 10
10 & 6 becomes 1010 & 0110 which equals 0010 in base 2, 2 in base 10
Hope this helps!

So 10 should be something like this 1010
and 5 should be something like this 0101
Now, if you find & or And for both of them, You should get something like 0000 which is zero
Similarly for 6, it should be 0110
which should give & or And for both of them as 0010 which happens to be 2
Note: for And we have the following rule
0 & 0 === 0
0 & 1 === 0
1 & 1 === 1
Try going through w3c article: https://www.w3schools.com/jsref/jsref_operators.asp

Related

Why does -3 >> 1 equal -2?

In JS, we have the following situation:
<< operator:
3 << 1 // 6
5 << 1 // 10
7 << 1 // 14
-3 << 1 // -6
-5 << 1 // -10
-7 << 1 // -14
>> operator:
3 >> 1 // 1
5 >> 1 // 2
7 >> 1 // 3
-3 >> 1 // -2
-5 >> 1 // -3
-7 >> 1 // -4
As you can see, for the << operator, and for values less than 2**32, we have abs(X << Y) === abs(-X << Y).
Why doesn't this keep true for the >> operator?
Because you are rotating the binary representation of those numbers. And the negative numbers are stored as 2's complement binary
So (using just 8-bits for illustration purposes):
-3 = 11111101
Which if you rotate with >> which is sign propagating you will get:
11111110 = -2
Because the sign propagating shift copies the sign bit to the left-most bit.
With the positive numbers it's easier:
3 = 00000011
After shifting with >> (since it's positive, you are shifting in zeros)
00000001 = 1
It's because minus has greater precedence than shift operator.
So, -3 >> 1 will run as (-3) >> (1) but not as -(3 >> 1).

Why does var bug = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 equal 15 [duplicate]

console.log(0.5 | 0); // 0
console.log(-1 | 0); // -1
console.log(1 | 0); // 1
Why does 0.5 | 0 return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?
This is a bitwise or.
Since bitwise operations only make sense on integers, 0.5 is truncated.
x | 0 is x, if x is an integer.
Bit comparison is so simple it's almost incomprehensible ;) Check out this "nybble"
8 4 2 1
-------
0 1 1 0 = 6 (4 + 2)
1 0 1 0 = 10 (8 + 2)
=======
1 1 1 0 = 14 (8 + 4 + 2)
Bitwise ORing 6 and 10 will give you 14:
alert(6 | 10); // should show 14
Terribly confusing!
A single pipe is a bit-wise OR.
Performs the OR operation on each pair
of bits. a OR b yields 1 if either a
or b is 1.
JavaScript truncates any non-integer numbers in bitwise operations, so its computed as 0|0, which is 0.
This example will help you.
var testPipe = function(input) {
console.log('input => ' + input);
console.log('single pipe | => ' + (input | 'fallback'));
console.log('double pipe || => ' + (input || 'fallback'));
console.log('-------------------------');
};
testPipe();
testPipe('something');
testPipe(50);
testPipe(0);
testPipe(-1);
testPipe(true);
testPipe(false);
This is a Bitwsie OR (|).
The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded.
So, in our case decimal number is converted to interger 0.5 to 0.
= 0.5 | 0
= 0 | 0
= 0

Why and 1 ( &1) bitwise operation always return 0 or 1

I just started learning about bit wise operation and want to ask why and 1 ( &1) bitwise operation always return 0 or 1 .
0 & 0 === 0
0 & 1 === 0
1 & 0 === 0
1 & 1 === 1
therefore any number & 1 will always be either 0 or 1
in binary ... any number
xxxxxxxxxxxxx0
or
xxxxxxxxxxxxx1
where x can be 0 or 1
1 in binary is
00000000000001
so
xxxxxxxxxxxxx1 &
00000000000001 ==
00000000000001
xxxxxxxxxxxxx0 &
00000000000001 ==
00000000000000
When you perform a & 1 it will always return 0 or 1 depending upon the the last binary digit of a.
Rules:
0 & 0 = 0
0 & 1 = 0
1 & 1 = 1
For example:
a = 5 //5 = 0101
b = a & 1 = 1 //(0101 & 0001)
a = 6 //6 = 0110
b = a & 1 = 0 //(0110 & 0001)
This is a bitwise operation. Suppose you take 2 & 1. That would be 10 and 01 in binary. Bitwise AND will give 00. BitWise operations with 1 will give 1 or 0 always because 1 has only a significant unit's place in binary. So it cannot return any value other than a 0 or a 1.
This can be used to check if an integer is odd or even, returning a 0 for False and 1 for True.
is_odd: 1 for odd , 0 for even
odd = number & 1
is_even: 1 for even , 0 for odd
even = number & 1 ^ 1

How to simplify modulus arithmetic?

I have
let f = x => x % 4 === 0 ? 0 : 4 - x % 4
But that's a piece of garbage function. Help.
x is never going to be negative.
Here's a sort of Table of Truth, or something.
x x % 4 4 - (x % 4) f(x)
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
I'm trying to find some correlations here, but it's late and I don't think my brain is working correctly. zzz
What I'm seeing in the f(x) column is a sort of reverse modulus, whereby the outputs cycle from 032103210... instead of 01230123...
I'm sensing some use of Math.max or Math.min in combination with Math.abs might help … There's probably an x * -1 in there somewhere too …
Can you help me write f such that it doesn't suck so badly ?
Moving Redu's use of bitwise operators a bit ahead:
f(x) = -x & 3
Table of Truths™
x x -x 3 -x&3 -x&3
- ---- ----- ---- ---- ----
0 0000 0000 0011 0000 0
1 0001 -0001 0011 0011 3
2 0010 -0010 0011 0010 2
3 0011 -0011 0011 0001 1
4 0100 -0100 0011 0000 0
5 0101 -0101 0011 0011 3
6 0110 -0110 0011 0010 2
7 0111 -0111 0011 0001 1
8 1000 -1000 0011 0000 0
9 1001 -1001 0011 0011 3
var i,
f = x => -x & 3;
for (i = 0; i < 20; i++) {
console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Original solution with negative value and a negative offset of 3 then modulo and add the same offset again.
f(x) = (-x - 3) % 4 + 3
var i,
f = x => (-x - 3) % 4 + 3;
for (i = 0; i < 20; i++) {
console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Something like this will definitely do:
(4 - (x % 4)) % 4
Here's some more truth:
x x % 4 4 - (x % 4) (4 - (x % 4)) % 4
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
I thought you don't want to use modulo. Then here is your code.
var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)
x x % 4 4 - (x % 4) f(x)
0 0 4 0
1 1 3 3
2 2 2 2
3 3 1 1
4 0 4 0
5 1 3 3
6 2 2 2
7 3 1 1
8 0 4 0
9 1 3 3
Explanation: I just had to recall the back old days of truth tables which helped me to solve out this problem. So now we have inputs and output. Since we work in modulo 4 we are interested only in the last two bits.
Input Output
0 : 0 0 0 0
1 : 0 1 1 1
2 : 1 0 1 0
3 : 1 1 0 1
So if we look, the output 2^1 digit is XOR of input 2^0 and 2^1 hence 2 * (x & 2 ? ~x & 1 : x & 1) and the output 2^0 digit is in fact input 2^0 digit. Which is (x & 1) hence... var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)
Note: (foo XOR bar = foo ? !bar : bar)
u v
y z z w
x x & 2 ? ~x y & 1 x & 1 2 * z w + v f(x)
-- ------ ------ --- ------- ------ ------ ----- ----- ----
0 0000 0000 F -0001 0001 0000 0 0 0
1 0001 0000 F -0010 0000 0001 2 3 3
2 0010 0010 T -0011 0001 0000 2 2 2
3 0011 0010 T -0100 0000 0001 0 1 1
4 0100 0000 F -0101 0001 0000 0 0 0
5 0101 0000 F -0110 0000 0001 2 3 3
7 0110 0010 T -0111 0001 0000 2 2 2
8 0111 0010 T -1000 0000 0001 0 1 1
9 1000 0000 F -1001 0001 0000 0 0 0

Javascript Date() milliseconds difference not adding up

I've created this JSFiddle to illustrate my question.
It seems that 10 - 00 = 8 in this example.
The simple question: Why does #ha contain 8? Have I misunderstood something?
var a4 = new Date(2012,00,00,00,00,00,0010);
var b4 = new Date(2012,00,00,00,00,00,0000);
var c4 = a4-b4;
var d4 = document.getElementById("ha");
d4.innerHTML=c4;
When an integer literal starts with 0 it is interpreted as an octal literal, not a decimal literal. The literal 0010 has the same value as the literal 8 . Remove the leading zeroes.
using a leading 0 on a number is a signal to the processor that you're using an octal (base 8) number the same way that 0x indicates hex.
0010 - 0 == 010 == 8 in base ten
10 - 0 == 10 == 10 in base ten
0x10 - 0 == 0x10 == 16 in base ten

Categories