 1580598900

# Binary Numbers in JavaScript

Computers use binary numbers to store calculations. When we learn computer programming languages, we always encounter the calculation of binary numbers. This article introduces binary numbers in JavaScript to everyone. I hope it will be helpful to everyone.

1. Binary number representation

2. Binary integer in js

3. Bit operations in js

## 1. Binary number

This article assumes that you know that binary numbers are used to store and calculate numbers in computers, and that you are familiar with the representation of binary numbers.

In order to achieve different purposes, it is all to simplify the problem. Binary numbers are represented in the computer in different ways, such as original code, reverse code, complement code, and shift code.

Note: This article asks for simplified operations. Binary numbers use one byte-8 binary bits to simplify the description.

Let’s talk about the truth value first. We indicate that natural numbers include positive numbers, negative numbers, and 0. The following is the binary representation of 1 and -1. We call them true values.

``````+ 00000001 # +1
-00000001 # -1
``````

The range of truth values ​​that an 8-bit binary number can represent is [-2 ^ 8, + 2 ^ 8].

Since the computer can only store 0 and 1, it cannot store positive and negative, so the highest bit of the 8 binary bits is used to represent the sign, 0 is positive, 1 is negative, and the last seven bits are used to represent the absolute value of the true value. The method is called the original code notation, referred to as the original code. The original codes of 1 and -1 above are as follows:

``````0 0000001 # +1
1 0000001 # -1
``````

Since 10000000 means -0, this has no meaning. All this number is used to represent -128. All negative numbers are one more than integers.

Since the most significant bit is used to represent the symbol, the range that can be represented now is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

Inverse code is another method of representing numbers. The rule is that the inverse code of an integer is the same as the original code. The negative code of the negative number does not change the sign bit of the original code. The other bits are negated.

``````0 0000001 # +1
1 1111110 # -1
``````

The range of the reverse code is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

Two’s complement is another method of representation. It is a digital representation invented to simplify the operation and change subtraction into addition. The rule is that the complement of an integer is the same as the original code. The complement of a negative number is the addition of 1 to the end of its complement.

``````0 0000001 # +1
1 1111111 # -1
``````

The rule for quickly calculating the complement of a negative number is to find the first 1, 1 from its original code to the high order, and its low order is unchanged. The high order before 1 can be reversed bitwise. I don’t know if you can think of the clever principle.

The range represented by 8’s complement is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

## 2. Binary integer in js

Let’s talk about binary integers in js. A qualified jser should support only one number type in js, which is floating point type. The floating point number of js follows the IEEE 754 specification.

However, there is another type of data in js, which is an integer represented by 32 bits. As long as any number in js is bitwise operated, the operating system will convert it to integers. Try to enter the following in the console The code

``````2.1 | 0 # OR operation
>>> 2
``````

This shaping in js distinguishes between positive and negative numbers. According to the above knowledge, we infer that the range of integer representation in js is [-2 ^ 31, + 2 ^ 31-1], which is [-2147483648, +2147483647]. Output the following code in the console to verify our inference.

``````-2147483648 | 0
>>> -2147483648

-2147483649 | 0
>>> 2147483647

2147483647 | 0
>>> 2147483647

2147483648 | 0
>>> -2147483648
``````

From the above results, we can see that the sign is changed when the values ​​greater than and less than the lowest and highest values are converted.

## 3. Bit operations in js

The bit operators in js have the following. When performing these operations on numbers, the system will say that 64 floating-point numbers are converted to 32-bit integers.

& versus

| Or

~ Non

^ XOR

<< shift left

Arithmetic right shift (signed right shift)

Logical right shift (unsigned right shift)

The following examples illustrate the role of each operator. Before starting, let’s introduce some knowledge points that will be used.

### Native binary literal

Native binary literals were introduced in es6. The syntax of binary numbers starts with 0b. We will use this new feature. Currently the latest version of Chrome already supports it.

``````0b111 // 7
0b001 // 1
``````

### Number.prototype.toString

First let’s introduce a method that will be used below-the Number.prototype.toString method can convert numbers into strings. There is an optional parameter that determines the number to be displayed in the specified base. You can view 3 below. Binary representation of

``````3..toString (2)
>> 11
``````

### & versus

& Bitwise AND performs the AND operation on the same operand and operand. If both are 1, it is 1, and if one is 0, it is 0.

``````101
011
---
001
``````

The result of 101 and 011 is 001, and the verification is performed in js.

``````(0b101 & 0b011) .toString (2)
>>> "1"
``````

### | Or

| Bitwise or the same position as long as one is 1 is 1, and both are 0 is 0

``````101
001
---
101
``````

101 and 001 or the end result is 101, the following verification in js

``````(0b101 | 0b001) .toString (2)
>>> "101"

``````

### ~ Non

The ~ operator inverts each bit of the operand. If it is 1, it becomes 0. If it is 0, the edge is 1.

``````101
---
010
``````

The result of 101 bitwise NOT is 010, which is verified in js below

``````(~ 0b101) .toString (2)
>>> "-110"
``````

Ah, why is it wrong! !! !! As mentioned above, the numbers in js are signed. We have forgotten the sign of the most significant bit. In order to simplify, we reduced the 32 bits to 8 bits. Note that the most significant bit is the sign bit.

0 0000101

1 1111010 // non-post result

1 0000101 // negation

1 0000110 // ask for complement

1 1111010 is obviously a negative number, and it is a complement of a negative number. We find its original code, that is, complement it again. 1 0000110 is the true value of this number, which means that the result shows -110. Oh, O (∩_∩) O haha ~

In fact, the above AND and OR also operate on the sign bit. I don’t believe you try the following two, you can see that the sign bit is involved in the operation

``````(0b1 & -0b1)
>>> 1
(0b1-0b1)
>>> -1
``````

### ^ XOR

Let’s talk about XOR. This is more interesting. XOR, as the name implies, sees if the two bits are different-different, if the two bits are different, it is 1, and if the two bits are the same, it is 0

``````101
001
---
100
``````

101 and 001 XOR result is 100, verified in js

``````(0b101 ^ 0b001) .toString (2)
>>> "100"
``````

### << shift left

The rule of left shift is that each bit is shifted to the left by one, and the end is filled with 0. The effect is equivalent to × 2. In fact, the computer uses the shift operation to calculate the multiplication.

``````010
---
0100
``````

010 left one bit will become 100, verify in js below

``````(0b010 << 1) .toString (2)
>>> "100"
``````

### >> Arithmetic right shift (signed right shift)

Arithmetic right shift is also called signed right shift, that is, when shifting, the high order is complemented by its sign bit, the integer is supplemented by 0, and the negative number is supplemented by 1.

``````(0b111 >> 1) .toString (2)
>>> "11"
(-0b111 >> 1) .toString (2)
>>> "-100"
``````

Negative numbers do n’t seem right, let ’s see what ’s going on

-111 // true

1 0000111 // original code

1 1111001 // complement

1 1111100 // shift right

1 0000100 // original code after shifting

-100 // shifted truth value

### >>> Logical right shift (unsigned right shift)

Logical right shift is also referred to as unsigned right shift, that is, when shifting right, the high order bits are always filled with 0. There is no difference between integer and arithmetic right shift.

``````(0b111 >>> 1) .toString (2)
>>> "11"
``````

It’s different for negative numbers, it will become positive after shifting right

``````(-0b111 >>> 1) .toString (2)
>>> "1111111111111111111111111111100"
``````

So much for binary numbers, let’s talk about the problem at the beginning. The problem at the beginning can be broken down into the following problem. Because search will return -1 and find the index of the position, it will become the following problem.

``````! ~ -1
>>> ture
! ~ 0
>>> false
! ~ 1
>>> false
``````

The result of the NOT operation on numbers is equivalent to changing the sign, and the absolute value of its value is -1

``````~ -1
>>> 0
~ 0
>>> -1
~ 1
>>> -2
``````

In fact, it can be seen! The logic of x is to determine whether x is -1. My god ’s logic is really against the sky. I still advise everyone to write x === -1.

#javascript #binary-number

## Buddha Community 1622036598

## JavaScript compound assignment operators

JavaScript is unarguablly one of the most common things you’ll learn when you start programming for the web. Here’s a small post on JavaScript compound assignment operators and how we use them.

The compound assignment operators consist of a binary operator and the simple assignment operator.

The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).

It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.

They are:

• +=
• -+
• *=
• /=

You can also check my video tutorial compound assignment operators.

Let’s consider an example. Suppose price = 5 and we want to add ten more to it.

var price = 5;
price = price + 10;

We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.

price += 5;

Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.

Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.

price -= 5;
console.log(price);

Lets multiply price and show it.

price *=5;
console.log(price);

and finally we will divide it.

price /=5;
console.log(price);

If you have any doubts, comment below.

#javascript #javascript compound assignment operators #javascript binary operators #javascript simple assignment operator #doers javascript

1622207074

## What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

## Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

## What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

## JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

``````<script type="text/javascript">
document.write("Hello World!");
</script>
``````

## How to comment JavaScript code?

• For single line comment in JavaScript we have to use // (double slashes)
• For multiple line comments we have to use / * – – * /
``````<script type="text/javascript">

//single line comment

/* document.write("Hello"); */

</script>
``````

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript 1616670795

## Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers

1589255577

## The essential JavaScript concepts that you should understand

As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.

#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips 1580598900

## Binary Numbers in JavaScript

Computers use binary numbers to store calculations. When we learn computer programming languages, we always encounter the calculation of binary numbers. This article introduces binary numbers in JavaScript to everyone. I hope it will be helpful to everyone.

1. Binary number representation

2. Binary integer in js

3. Bit operations in js

## 1. Binary number

This article assumes that you know that binary numbers are used to store and calculate numbers in computers, and that you are familiar with the representation of binary numbers.

In order to achieve different purposes, it is all to simplify the problem. Binary numbers are represented in the computer in different ways, such as original code, reverse code, complement code, and shift code.

Note: This article asks for simplified operations. Binary numbers use one byte-8 binary bits to simplify the description.

Let’s talk about the truth value first. We indicate that natural numbers include positive numbers, negative numbers, and 0. The following is the binary representation of 1 and -1. We call them true values.

``````+ 00000001 # +1
-00000001 # -1
``````

The range of truth values ​​that an 8-bit binary number can represent is [-2 ^ 8, + 2 ^ 8].

Since the computer can only store 0 and 1, it cannot store positive and negative, so the highest bit of the 8 binary bits is used to represent the sign, 0 is positive, 1 is negative, and the last seven bits are used to represent the absolute value of the true value. The method is called the original code notation, referred to as the original code. The original codes of 1 and -1 above are as follows:

``````0 0000001 # +1
1 0000001 # -1
``````

Since 10000000 means -0, this has no meaning. All this number is used to represent -128. All negative numbers are one more than integers.

Since the most significant bit is used to represent the symbol, the range that can be represented now is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

Inverse code is another method of representing numbers. The rule is that the inverse code of an integer is the same as the original code. The negative code of the negative number does not change the sign bit of the original code. The other bits are negated.

``````0 0000001 # +1
1 1111110 # -1
``````

The range of the reverse code is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

Two’s complement is another method of representation. It is a digital representation invented to simplify the operation and change subtraction into addition. The rule is that the complement of an integer is the same as the original code. The complement of a negative number is the addition of 1 to the end of its complement.

``````0 0000001 # +1
1 1111111 # -1
``````

The rule for quickly calculating the complement of a negative number is to find the first 1, 1 from its original code to the high order, and its low order is unchanged. The high order before 1 can be reversed bitwise. I don’t know if you can think of the clever principle.

The range represented by 8’s complement is [-2 ^ 7, + 2 ^ 7-1], which is [-128, +127]

## 2. Binary integer in js

Let’s talk about binary integers in js. A qualified jser should support only one number type in js, which is floating point type. The floating point number of js follows the IEEE 754 specification.

However, there is another type of data in js, which is an integer represented by 32 bits. As long as any number in js is bitwise operated, the operating system will convert it to integers. Try to enter the following in the console The code

``````2.1 | 0 # OR operation
>>> 2
``````

This shaping in js distinguishes between positive and negative numbers. According to the above knowledge, we infer that the range of integer representation in js is [-2 ^ 31, + 2 ^ 31-1], which is [-2147483648, +2147483647]. Output the following code in the console to verify our inference.

``````-2147483648 | 0
>>> -2147483648

-2147483649 | 0
>>> 2147483647

2147483647 | 0
>>> 2147483647

2147483648 | 0
>>> -2147483648
``````

From the above results, we can see that the sign is changed when the values ​​greater than and less than the lowest and highest values are converted.

## 3. Bit operations in js

The bit operators in js have the following. When performing these operations on numbers, the system will say that 64 floating-point numbers are converted to 32-bit integers.

& versus

| Or

~ Non

^ XOR

<< shift left

Arithmetic right shift (signed right shift)

Logical right shift (unsigned right shift)

The following examples illustrate the role of each operator. Before starting, let’s introduce some knowledge points that will be used.

### Native binary literal

Native binary literals were introduced in es6. The syntax of binary numbers starts with 0b. We will use this new feature. Currently the latest version of Chrome already supports it.

``````0b111 // 7
0b001 // 1
``````

### Number.prototype.toString

First let’s introduce a method that will be used below-the Number.prototype.toString method can convert numbers into strings. There is an optional parameter that determines the number to be displayed in the specified base. You can view 3 below. Binary representation of

``````3..toString (2)
>> 11
``````

### & versus

& Bitwise AND performs the AND operation on the same operand and operand. If both are 1, it is 1, and if one is 0, it is 0.

``````101
011
---
001
``````

The result of 101 and 011 is 001, and the verification is performed in js.

``````(0b101 & 0b011) .toString (2)
>>> "1"
``````

### | Or

| Bitwise or the same position as long as one is 1 is 1, and both are 0 is 0

``````101
001
---
101
``````

101 and 001 or the end result is 101, the following verification in js

``````(0b101 | 0b001) .toString (2)
>>> "101"

``````

### ~ Non

The ~ operator inverts each bit of the operand. If it is 1, it becomes 0. If it is 0, the edge is 1.

``````101
---
010
``````

The result of 101 bitwise NOT is 010, which is verified in js below

``````(~ 0b101) .toString (2)
>>> "-110"
``````

Ah, why is it wrong! !! !! As mentioned above, the numbers in js are signed. We have forgotten the sign of the most significant bit. In order to simplify, we reduced the 32 bits to 8 bits. Note that the most significant bit is the sign bit.

0 0000101

1 1111010 // non-post result

1 0000101 // negation

1 0000110 // ask for complement

1 1111010 is obviously a negative number, and it is a complement of a negative number. We find its original code, that is, complement it again. 1 0000110 is the true value of this number, which means that the result shows -110. Oh, O (∩_∩) O haha ~

In fact, the above AND and OR also operate on the sign bit. I don’t believe you try the following two, you can see that the sign bit is involved in the operation

``````(0b1 & -0b1)
>>> 1
(0b1-0b1)
>>> -1
``````

### ^ XOR

Let’s talk about XOR. This is more interesting. XOR, as the name implies, sees if the two bits are different-different, if the two bits are different, it is 1, and if the two bits are the same, it is 0

``````101
001
---
100
``````

101 and 001 XOR result is 100, verified in js

``````(0b101 ^ 0b001) .toString (2)
>>> "100"
``````

### << shift left

The rule of left shift is that each bit is shifted to the left by one, and the end is filled with 0. The effect is equivalent to × 2. In fact, the computer uses the shift operation to calculate the multiplication.

``````010
---
0100
``````

010 left one bit will become 100, verify in js below

``````(0b010 << 1) .toString (2)
>>> "100"
``````

### >> Arithmetic right shift (signed right shift)

Arithmetic right shift is also called signed right shift, that is, when shifting, the high order is complemented by its sign bit, the integer is supplemented by 0, and the negative number is supplemented by 1.

``````(0b111 >> 1) .toString (2)
>>> "11"
(-0b111 >> 1) .toString (2)
>>> "-100"
``````

Negative numbers do n’t seem right, let ’s see what ’s going on

-111 // true

1 0000111 // original code

1 1111001 // complement

1 1111100 // shift right

1 0000100 // original code after shifting

-100 // shifted truth value

### >>> Logical right shift (unsigned right shift)

Logical right shift is also referred to as unsigned right shift, that is, when shifting right, the high order bits are always filled with 0. There is no difference between integer and arithmetic right shift.

``````(0b111 >>> 1) .toString (2)
>>> "11"
``````

It’s different for negative numbers, it will become positive after shifting right

``````(-0b111 >>> 1) .toString (2)
>>> "1111111111111111111111111111100"
``````

So much for binary numbers, let’s talk about the problem at the beginning. The problem at the beginning can be broken down into the following problem. Because search will return -1 and find the index of the position, it will become the following problem.

``````! ~ -1
>>> ture
! ~ 0
>>> false
! ~ 1
>>> false
``````

The result of the NOT operation on numbers is equivalent to changing the sign, and the absolute value of its value is -1

``````~ -1
>>> 0
~ 0
>>> -1
~ 1
>>> -2
``````

In fact, it can be seen! The logic of x is to determine whether x is -1. My god ’s logic is really against the sky. I still advise everyone to write x === -1.

#javascript #binary-number