A binary numeral system is defined by a base, or radix, of two when discussing various number systems. This means that every value is expressed solely using the two digits, 0 and 1. The term "bit" serves as a shorthand for a binary digit, referring to each individual digit within a binary number. Like the decimal system that we commonly utilize, binary functions as a positional number system, relying on powers of two instead of powers of ten.
In computer science, binary numbers represent data utilizing only two symbols: 0 and 1. This system is fundamental to the functionality of contemporary computers because electrical circuits can be easily constructed to possess two stable states: high voltage (often denoted by 1) and low voltage (usually indicated by 0).
Here are some essential details about binary numbers:
- As a base-2 number system, binary bases the value of each digit on both its position and a base of two. The 1 (2^0) is indicated by the number farthest to the right, the 2 (2^1) by the digit immediately to the left, and so on.
- Binary digits are used on computers to represent many types of data, including text, images, numbers, and even software. Each data point consists of a sequence of 1s and 0s.
- The acronym bit represents any one of the binary digits. A single bit may store only two values, 0 and 1.
- Bit groups: Groups of eight bits are utilized to represent a larger variety of values and are sometimes referred to as bytes.
Computers utilize binary digits as their foundational method for processing information. Grasping this concept is essential for anyone who is keen on computer science or the functioning of digital devices, despite the fact that it might not be inherently intuitive for humans.
The following example illustrates the process of counting using binary number system notation. The table presented below displays the binary equivalents of the decimal numbers ranging from 1 to 10.
| Decimal Numbers | Binary Representation |
|---|---|
1 |
0000 |
2 |
0001 |
3 |
0010 |
4 |
0011 |
5 |
0100 |
6 |
0101 |
7 |
0110 |
8 |
0111 |
9 |
1000 |
10 |
1010 |
Binary in JavaScript
In JavaScript, when a number literal is assigned to a variable without any additional specifications, it is interpreted as a decimal number (base 10) by default.
Code:
const a = 10;
console.log(a); // 10
In the preceding illustration, the variable a holds the value of decimal 10, which does not correspond to the binary representation of 2. In ECMAScript 5 (ES5), there is no mechanism to denote a binary numeral. However, beginning with ECMAScript 6 (ES6), to indicate a binary number, it is necessary to precede it with a leading zero followed by either a lowercase or uppercase "b".
Code:
// ES6+
const a = 0b10;
console.log(a); // 2
const b = 0B10;
console.log(b); // 2
The JavaScript engine interprets a number as binary when it detects a prefix of 0b or 0B.
Using any digit other than 0 or 1 following the prefix 0b or 0B will result in a Syntax Error.
Code:
const a = 0b123; // SyntaxError: Invalid or unexpected token
Number System Conversions
Multiple number systems exist, including hexadecimal, decimal, octal, and binary. Each of these systems serves specific purposes effectively. The capability to convert between these numerical representations is essential. In this segment, we will explore the conversion process between binary and decimal systems.
Decimal to Binary Conversion:
To convert a decimal number into binary, we repeatedly divide the decimal value by two, taking note of the remainders at each step. By reading the collected remainders in reverse sequence, we obtain the binary equivalent of the original decimal number.
Algorithm:
25 ÷ 2 = 12 remainder 1
12 ÷ 2 = 6 remainder 0
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
By examining the remainders from the end to the beginning, we obtain the binary representation: 11001.
In addition to this, we will also explore a JavaScript program that converts a decimal number into its binary equivalent.
Code 1: Using toString method
function decimalToBinary(decimal) {
let binaryString = "";
let remainder;
// Loop until the decimal number becomes 0
while (decimal > 0) {
// Get the remainder of the division by 2
remainder = decimal % 2;
// Prepend the remainder (0 or 1) to the binary string
binaryString = remainder + binaryString;
// Divide the decimal number by 2 (integer division)
decimal = Math.floor(decimal / 2);
}
return binaryString;
}
console.log(decimalToBinary(5));
Output:
Code 2: Using Bit Manipulation
function decimalToBinary(decimal) {
let binaryString = "";
let remainder;
// Loop until the decimal number becomes 0
while (decimal > 0) {
// Get the remainder of the division by 2
remainder = decimal % 2;
// Prepend the remainder (0 or 1) to the binary string
binaryString = remainder + binaryString;
// Divide the decimal number by 2 (integer division)
decimal = Math.floor(decimal / 2);
}
return binaryString;
}
console.log(decimalToBinary(5));
Output:
Output:
Conclusion
To summarize, this article highlights the importance of binary numeral systems within the broader context of numerical systems and computer science. It explains that binary, a fundamental component for the representation of digital information, utilizes only two digits—0 and 1—to encode values. Additionally, it discusses the concept of bit manipulation and introduces methods such as toString for transforming decimal integers into their binary counterparts.