Javascript BITWISE
En eventueel een subtitel om de nieuwsgierigheid aan te wakkeren
BITWISE GAAT OVER BINAIRE BEWERKINGEN
BITWISE GAAT OVER BINAIRE BEWERKINGEN
00110100 = 4 + 16 + 32 = 52 Decimaal naar binair:
3 = 1 + 2 -> 11 -> 0000 0011
11 = 1 + 2 + 8 -> 1011 -> 0000 1011
BITWISE “OF” bewerking |
Rekenregel: OF 1 OR 1 = 1
1 OR 0 = 1 0 OR 1 = 1 0 OR 0 = 0
3 = 0011
11 = 1011
Onder elkaar 1011
= (tabel) 8 + 0 + 2 + 1 = (dec) 11
BITWISE “OF” bewerking |
BITWISE “AND”
bewerking |
Rekenregel: AND 1 AND 1 = 1
1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0
3 = 0011
11 = 1011
Onder elkaar 0011
= (tabel) 0 + 0 + 2 + 1 = (dec) 3
BITWISE “AND” bewerking |
BITWISE “NOT” bewerking |
(dec) ~7 = ~0111 = 1000 = (dec) 8
(dec) ~3 = ~11 = 00 = (dec) 0
BITWISE “XOR” bewerking Exclusief OR ^
ALLEEN ALS één van beide “1” is dan is het resultaat “1”
10^2 = 0010 XOR 1010 = 1000 (dec 8)
BITWISE “<<” en “>>” SHIFT
<< Left shift x = 5 << 1 0101 << 1 1010 10
BITWISE “<<” en “>>” SHIFT
>>Right shift x = 5 >> 1 0101 >> 1 0010 2
BITWISE “NOT” bewerking ~ (signed bit
BITWISE – script
BITWISE – floating functies
BITWISE – floating functies
IEEE 754 is een standaard van de IEEE die een manier beschrijft om getallen met zwevende komma voor te stellen in het geheugen van een binaire rekenmachine (computer). Een belangrijke doelstelling van de standaard is, berekeningen waarin dergelijke getallen voorkomen snel te laten verlopen.
Deze standaard moet ondersteund worden door het computersysteem, en niet zozeer door de hardware zelf. De meeste architecturen ondersteunen de IEEE 754-standaard. Er zijn er enkele (oudere) die de standaard niet ondersteunen zoals IBM System/370 en de DEC VAX.
BITWISE – floating functies
BITWISE – floating functies
BITWISE – floating functies
BITWISE – functies
Most of us know know that there are some bitwise operators in JS. Every number has it's own binary representation, used by those operators. To check dec number's binary value, we use .toString() method with base argument - '2' for binary:
var number = 3;
var result = number.toString(2);
// In case we want to change the number directly, not // a variable with a number value, we need to use two // dots - it's equivalent of 14.0.toString where we can // show that decimal part of the number is equal to 0.
var result2 = 14..toString(2);
document.getElementById('result').innerHTML = result + ', ' + result2;
1 juni 2016
Javascript BITWISE
21
// helper function for displaing results var output = function(operator, result) {
document.getElementById(operator).innerHTML = result;
}
// variables var a = 5;
var b = 13;
// a | b - OR
// if in any of the given numbers corresponding bit // is '1', then the result is '1'
output('or', a|b); // 13 // a & b - AND
// if in both of the given numbers corresponding bit // is '1', then the result is '1'
output('and', a&b); // 5 // a ^ b - XOR
// if in one of the given numbers (not both) // corresponding bit is '1', then the result is '1' output('xor', a^b); // 8
// ~a - NOT
// inverts all the bits output('not', ~a); // -6 // a >> b - RIGHT SHIFT
// shift binary representation of 'a' for 'b' // bits to the right, discarding bits shifted off output('rs', a>>b); // 0
// a << b - LEFT SHIFT
// shift binary representation of 'a' for 'b'
// bits to the right, shifting in zeros from the right output('ls', a<<b); // 40960
// a >>> b - ZERO FILLED RIGHT SHIFT
Sometime we even use Bitwise OR as equivalent of Math.floor():
var data = 2.2352524535;
var result = data | 0;
document.getElementById('result').innerHTML = result;
It has the same effect as double NOT operator (my favorite rounding solution since I first heard about it on Damian Wielgosik's workshop couple of years ago).
var data = 2.2352524535;
var result = ~~data;
document.getElementById('result').innerHTML = result;
What about other real life examples of bit chaking? For instance, we can convert colors from RGA to Hex format
/ helper function for displaying results var output = function(operator, result) {
document.getElementById(operator).innerHTML = result;
}
// variables
var color = {r: 186, g: 218, b: 85};
// RGB to HEX
var rgb2hex = function(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).substr(1);
}
output('color', rgb2hex(color.r, color.g, color.b));
// helper function for displaying results var output = function(operator, result) {
document.getElementById(operator).innerHTML = result;
}
// variables var a = 9285;
var b = 3569;
// a ^ ((a ^ b) & -(a < b)) - which one, a or b, is bigger?
output('max', a ^ ((a ^ b) & -(a < b)));
// b ^ ((a ^ b) & -(a < b)) - which one, a or b, is smaller?
output('min', b ^ ((a ^ b) & -(a < b)));
// helper function for displaying results var output = function(operator, result) {
document.getElementById(operator).innerHTML = result;
}
// variables var a = 10;
var b = 99;
// a^=b, b^=a, a^=b - swap variables using XOR operation, // details: http://en.wikipedia.org/wiki/XOR_swap_algorithm a^=b, b^=a, a^=b;
output('a', a);
output('b', b);