• No results found

Javascript BITWISE

N/A
N/A
Protected

Academic year: 2021

Share "Javascript BITWISE"

Copied!
25
0
0

Bezig met laden.... (Bekijk nu de volledige tekst)

Hele tekst

(1)

Javascript BITWISE

En eventueel een subtitel om de nieuwsgierigheid aan te wakkeren

(2)

BITWISE GAAT OVER BINAIRE BEWERKINGEN

(3)

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

(4)

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

(5)

BITWISE “OF” bewerking |

(6)

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

(7)

BITWISE “AND” bewerking |

(8)

BITWISE “NOT” bewerking |

(dec) ~7 = ~0111 = 1000 = (dec) 8

(dec) ~3 = ~11 = 00 = (dec) 0

(9)

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)

(10)

BITWISE “<<” en “>>” SHIFT

<< Left shift x = 5 << 1 0101 << 1 1010 10

(11)

BITWISE “<<” en “>>” SHIFT

>>Right shift x = 5 >> 1 0101 >> 1 0010 2

(12)

BITWISE “NOT” bewerking ~ (signed bit

(13)

BITWISE – script

(14)

BITWISE – floating functies

(15)

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.

(16)

BITWISE – floating functies

(17)

BITWISE – floating functies

(18)

BITWISE – floating functies

(19)

BITWISE – functies

(20)

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;

(21)

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

(22)

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;

(23)

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));

(24)

// 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)));

(25)

// 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);

Referenties

GERELATEERDE DOCUMENTEN

Although the majority of respondents believed that medical reasons were the principal motivating factor for MC, they still believed that the involvement of players who promote

Let us follow his line of thought to explore if it can provide an answer to this thesis’ research question ‘what kind of needs does the television program Say Yes to the

population the next year, probably because more foxes move in to contest the vacant area than were there in the first place. 19 , culling won't target individual foxes that

The actor playing Bond talks himself up with every word uttered but makes no mention, of course, of recent release 'Dream House' ─ surely a contender for The Worst Film Ever

Notwithstanding the relative indifference toward it, intel- lectual history and what I will suggest is its necessary complement, compara- tive intellectual history, constitute an

Omdat andere kos- ten, vooral ruwvoer, gedaald zijn is de arbeidsop- brengst “maar” f 24,- gedaald ten opzichte van verleden jaar..

To summarise what has been written above: various parameters have been selected for elaborate analysis. These parameters were selected on the fact that in Belarusian and/or

b) Also, the rare use of the antechamber–a feature used only in royal tombs from China and Korea (Barnes 1993) –in the Kyūshū tradition, can be connected to the fact