Welcome to the fascinating world of binary, a system of numerical representation where all values are expressed in the binary numeral system. Also known as base 2, binary uses only two distinct symbols - 0s and 1s - to represent a vast range of numbers, making it an essential component of digital systems. So, you've decided to learn how to code in binary? Great! Let's dive into this deterministic world together, starting with the basics.

Before we delve into the nitty-gritty, let's briefly understand why binary matters. In simple terms, binary is the native language of computers. All the data you're currently interacting with, from reading this article to the music you're listening to, exists as binary data in the digital world. So, understanding binary can help you grasp the fundamentals of how computers work, making it an invaluable skill for anyone involved in computer science or related fields.

The Binary Number System: From Bits to Bajt
The binary number system is based on powers of 2. Knowing this, let's understand how to express numbers in binary:

Let's take the decimal number 13. The largest power of 2 less than or equal to 13 is 8, so the highest bit (bit-0) is 1. Subtracting 8 from 13, we're left with 5. The next largest power of 2 is 4 (bit-1 equals 1). Subtracting 4 leaves us with 1, with the next power being 2 (bit-2 equals 1). Finally, the last bit (bit-3) is 1 because powers of 2 no longer cover the remainder. Thus, 13 in binary is 1101.
The Least Significant Bit: Where It All Begins

In binary, the leftmost digit represents the highest power of 2, and the value decreases to the right. The rightmost digit, also known as the Least Significant Bit (LSB), represents the smallest power of 2, typically 2^0. So, whereas in decimal, the leftmost digit is the most significant, in binary, it's the opposite.
Let's consider 1011011 in binary:
- 2^6: 0
- 2^5: 1
- 2^4: 0
- 2^3: 1
- 2^2: 1
- 2^1: 0
- 2^0: 1

Using this method, we calculate 1011011 to be 99 in decimal. Remember, the binary number must be read from right to left to tally the sum correctly.
Negative Numbers in Binary: Two's Complement
Binary also has a scheme for representing negative numbers, known as Two's Complement. In Two's Complement, the leftmost bit represents the sign of the number. If this bit is 0, the number is positive; if it's 1, the number is negative.

For example, -13 in decimal is represented as 10001011 in 8-bit Two's Complement. Here, the leftmost bit (bit-7) is 1, indicating that the number is negative. To calculate the value, we subtract the binary number from 11111111 (255 in decimal) and add 1 to the result. In this case, it's (11111111 - 10001011) + 1, which indeed equals -13 in decimal.
Binary in Programming: Coding in a New Language









Now that we've covered the basics, let's translate this knowledge into programming. Most modern programming languages offer built-in functions to convert numbers to binary and vice versa, making it easy to incorporate into your code.
For instance, in Python, you can use the built-in function bin() to convert a number to its binary equivalent. For example, if you type print(bin(13)), it will print 0b1101, which is the binary representation of 13.
Binary Operations: Little Changes Make a Big Impact
When writing code in binary, the operations performed might appear different from those in decimal. However, they're fundamentally the same, just in a different base. To understand this, let's break down binary operations like addition and subtraction:
To add two binary numbers (e.g., 1011 + 101), start from the LSB and add digits bit by bit, carrying over from one column only when the sum is 2 or more. This method is similar to adding in decimal, nettoing the sum in each position before moving to the next column. Similarly, subtraction in binary mirrors decimal, using borrowing or borrowing to compensate for insufficient digits in some positions.
The BCD: The Usual Suspect for Older Systems
While binary provides an efficient means of representing numbers in computer systems, older systems and electronic devices often use a different representation: Binary Coded Decimal (BCD). In BCD, each decimal digit is represented by a 4-bit binary number. Despite being less space-efficient than binary, BCD simplifies arithmetic operations as they're closely related to conventional decimal operations, making them useful in older, decimal-based systems.
In the ever-evolving landscape of computer science, binary serves as a foundational concept. Understanding it not only aids your understanding of how computers work but also enhances your coding skills by widening the perspectives you can bring to your work. So, carry on exploring, experimenting, and learning this fascinating system. Happy coding!