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
Related
In Bitwise operation, what does | 0x80 do? I know (& 0xFF) is convert value to 8 bit integer but how about (| 0x80) ?
I have the following code:
const buf = createHash('sha256').update('test').digest()
for (let i = 0; i < n; i++) {
const ubyte = buf.readUInt8(i)
const shifted = (ubyte >> 1) | mask
destBuf.writeUInt8(shifted, i)
mask = (ubyte & 1) * 0x80 // mask is 0 or 128
}
Can anyone explain that for me?
0x... means that what comes next is an hexadecimal number.
0x80 is the hexadecimal representation of the number 128. In binary, this equals 10000000.
The | character is the bitwise or operator. Suppose you have a 8-bit number:
a = xxxxxxxx
with x being either a 0 or a 1. Now, masking this number with 0x80 means:
xxxxxxxx | 10000000 = 1xxxxxxx
So it basically means you will have a 1 for your leftmost significant bit, while keeping all the other bits the same.
Now, in your code you use this mask in the line:
const shifted = (ubyte >> 1) | mask
What this does is takes the number ubyte:
ubyte = xxxxxxxy // x and y can be either 1 or 0
It shifts it right by onw digit:
ubyte >> 1 = zxxxxxxx // y gets lost, and z is a 0 if ubyte was unsigned.
Now it masks this number with your mask. When the mask is 128, the result is:
(ubyte >> 1) | 10000000 = 1xxxxxxx
So you will have a 1 as your most significant bit, and the other bits are unchanged.
It combines the bits of both participating numbers with the logical "or":
const b= 0x7;
function tst(a,b){
console.log(a.toString(2).padStart(8)+" first number: "+a)
console.log(b.toString(2).padStart(8)+" second number: "+b)
console.log((a | b).toString(2).padStart(8)+" bitwise overlay: "+(a|b))
console.log("----")
}
[0x80,0x6A,0x70,0x8f].forEach(a=>tst(a,b))
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
This is a function where bytes comes from a string, and each letter's char code was grabbed to create an array of bytyes
function checksum(bytes) {
var a = 0;
var b = 0;
for (var i = 0; i < bytes.length; i++) {
a = (a + bytes[i]) % 0xff;
b = (b + a) % 0xff;
}
return (b << 8) | a;
}
I'm mostly confused at the role of % 0xff and b << 8. Could someone help me break this down?
a is equal to the sum of all the elements modulo 255
b is equal to the sum of all the values that a assumes modulo 255 (so 1 time the last element + 2 times the one before that ....)
The final value is a 16 bit number where the higher 8 bits are b and the lower 8 bits are a.
That is actually a Fletcher's checksum
https://en.wikipedia.org/wiki/Fletcher%27s_checksum
This function calculates a and b, and generates the checksum from it.
a is calculated as follows:
0+the value of the current char
a must be between 0 and 254, hence modulo 255 is applied
b is calculated as the cumulative value of a modulo 255
increase the value of b by a
b must be between 0 and 254, hence modulo 255 is applied
at the end the checksum is generated by concatenating a to b
take b's bits and move them 8 places to the left
set a to the right side of b
The result will have a length of 2 bytes (16 bits), where the first byte is b and the second a.
Example:
(c is the current char value)
c | a |b
----------------
8 |8 |8
13 |21 |29
5 |26 |55
0 |26 |81
180 |206 |287
100 |51 |83
cheksum = 51,83 = 0x3353 = 0011 0011 0101 0011
What is the meaning of this line of code
n = (n<<1) | ((d>=0.0004)?1:0);
Trying to understand code from here in function sigOff()
http://www.espruino.com/Remote+Control+Sockets
This snippet seems to use the bitwise OR (|) and left shift (<<) operators:
Bitwise OR: a | b;
Returns a one in each bit position for which the corresponding bits of either or both operands are ones.
Left shift: a << b;
Shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.
The left shift by 1 (<< 1) basically doubles the value of n.
Then, the or (|) basically "adds" 1 to the result to make it uneven, if d >= 0.0004.
If d < 0.0004, the result from the left shift isn't changed.
So, for n == 3 and d == 0.0004, this happens:
n << 1 // 6
(d>=0.0004)?1:0 // 1
6 | 1 // 7
For n == 5 and d == 0.0002, this happens:
n << 1 // 10
(d>=0.0004)?1:0 // 0
10 | 0 // 10
Thanks to everyone in advance:
alert((~1).toString(2));
This outputs: -10
But in PHP/Java it outputs 11111111111111111111111111111110
Am I missing something? Why does Javascript add a "-" to the output?
I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got.
The way you calculate the negative of 10 (in base 2) using two's complement is that you first invert all of the bits, giving you:
11111111111111111111111111111101
then you add 1, giving you:
11111111111111111111111111111110
I guess the same is happening in Javascript.
You can use the shift operator >>> to convert the number to an unsigned integer before converting to binary:
(~1 >>> 0).toString(2) // "11111111111111111111111111111110"
Short answer:
A bitwise NOT (~1) performs a 1's complement conversion of the
decimal which gives us -2
The .toString() function basically takes the decimal without the sign 2, converts it to binary 10 and adds a - sign which gives us -10.
A more detailed answer:
It's in the function .toString(). When you output a number via .toString():
If the numObj is negative, the sign is preserved. This is the case
even if the radix is 2; the string returned is the positive binary
representation of the numObj preceded by a - sign, not the two's
complement of the numObj.
Taken from the developer.mozilla.org we got this formula that calculates the 1's complement of an integer, this is used when you perform a NOT (~) on a decimal:
Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields
-6.
Maybe it's better explained with this table and an example:
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 10 Integer | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 10 1's Complement | 2 | 1 | 0 | -1 | -2 | -3 | -4 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 2 | | | | 0 | 1 | 10 | 11 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Result ~x.toString(2) | 10 | 1 | 0 | -1 | -10 | -11 | -100 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
Starting with Base 10 integer "2"
Base 10 integer "2" its 1's Complement is "-3". This is the same as performing a NOT (~)
.toString function take the unsigned value (= "3" in base 10 and = "11" in base 2)
.toString function adds a "-" symbol
.toString outputs "-11"
This assumes that you are working in 32 bits...
var valueToNot = parseInt("11110000", 2);
var notResult = 0xFFFFFFFF - valueToNot;
console.log(notResult.toString(2));
results in
11111111111111111111111100001111
Here's a solution to implement NOT in javascript. It ain't pretty but it works.
// Since ~ is the two's complement, then the one's complement is ~(num -1).
var num = 9;
num.toString(2); //returns 1001
~(num - 1).toString(2); //returns -1001
// WHAT the hell?? I guess the negative sign acts as a sign bit.
If you want to view the Binary String of a decimal after a NOT (bit Toggle), then use the following code.
// Programer: Larry Battle
// Purpose: Provide a bit toggle function for javascript.
var getStrCopy = function (str, copies) {
var newStr = str;
copies = (copies > 0) ? copies : 1;
while (--copies) {
newStr += str;
}
return newStr;
};
var convertDecToBase = function ( dec, base, length, padding ) {
padding = padding || '0' ;
var num = dec.toString( base );
length = length || num.length;
if (num.length !== length) {
if (num.length > length) {
throw new Error("convertDecToBase(): num(" + num + ") > length(" + length + ") too long.");
}
num = getStrCopy( padding, (length - num.length)) + num;
}
return num;
};
var formatBinaryStr = function( str ){
return str.replace( /\d{4}/g, '$& ' ).replace( /\s$/,'');
};
var toggleBits = function( dec, length, doFormat ){
length = length || 8;
var str = convertDecToBase( dec, 2, length || 8 );
var binaryStr = str.replace( /0/g, 'o' ).replace( /1/g, '0').replace( /o/g, '1' );
return ( doFormat ) ? formatBinaryStr( binaryStr ) : binaryStr ;
};
// The following requires Firebug or Google Chrome Dev Tools
clear();
console.log( toggleBits( 1 ) ); // returns "11111110"
console.log( toggleBits( 2 ) ); // returns "11111101"
console.log( toggleBits( 50, 16 ) );// returns "1111111111001101"
console.log( toggleBits( 15, 8, true ) ); // returns "1111 0000"
console.log( toggleBits( 520, 16, true ) ); //returns "1111 1101 1111 0111"