5 – 03 L Binary V1 RENDER V1

From a high level perspective, you’ve been able to see that limiting CPU instructions can make your code run faster. As humans, we think about interacting with a computer in terms of a programming language like C++. However, computers only understand instructions in binary. So everything you write will be seen by the computer as a series of zeros and ones. Let’s dive a little deeper into how the computer uses binary to store information in memory. Understanding how your C++ program stores information can help you avoid unnecessary access to memory which will waste valuable CPU cycles. Let’s think about how you could represent a variable in binary. Binary relies on powers of two to describe a value with the least significant digit usually on the right. For each position in the binary representation, you take the binary value of one or zero and multiply it by the power of two associated with that position and using zero-based numbering. Finally, you add the results together to get the decimal value. Now let’s go through a few examples by looking at the binary representation of the numbers one through five. The decimal number zero is also zero in binary. To figure this out, you take zero times two_to_the_power_of_zero which equals zero. The decimal number one is one in binary. You can see this because one times two_to_the_power_of_zero equals one. One, zero in binary equals the decimal number two. Now you’re going to need it two to the first power as well. This is because one times two to the first power plus one times two to_the_zeroth_power equals two. Following the same rules, one, one in binary is three in decimal. This is because one times two_to_the_first_power plus one times two_to_the_zeroth_power equals three. One, zero, zero in binary is four in decimal. One times two_to_the_second_power plus zero times two_to_the_first_power plus zero times two_to_the_zeroth_power equals four. And finally, one, zero, one is five in decimal. Following the same process, you would calculate one times two_to_the_power_of_two plus zero times two_to_the_power_of_one plus one times two_to_the_power_of_zero which equals five. You can represent any decimal digit using binary. For example, the number 25 would be one, one, zero, zero, one. And here, you can see how to calculate this to get that result. The computer represents all variables in binary, not just integers. This means, even characters and floating point numbers are represented in binary. But your computer can’t store an arbitrary amount of zeros and ones at a time. Instead, memory is stored in collections of eight binary digits where each digit is called a bit. You can think of your computer’s memory in terms of these eight-bit slots, and each eight-bit slot is called one byte. In C++, the smallest variable would be an eight-bit or one-byte character. A 16-bit integer has two bytes and a 32-bit integer uses four bytes. A float also uses four bytes. In C++, your variables will always use the amount of memory specified by the variable type. For example, the number 3,378 only needs 12 bits. But represented as a 16-bit integer, it will need two bytes with extra zeros padded to the left. And as a 32-bit integer, it will have much more zero padding which basically means the most significant bits will be zero. In C++, you can declare a 16, 32, or even a 64-bit integer. All this means is that the integer is using two, four, or eight bytes of memory. You can start to appreciate how the number of bits available becomes a constraint. If an integer can only use 32 bits, then the maximum value would have all bits equal to one. The decimal equivalent is 4,294,967,295 and that number is the maximum value that a 32-bit integer can store, and is even less if you’re using signed numbers. Now that you have an understanding of how to represent variables in binary, you’ll learn what C++ does to store and extract this information. Remember that part of optimizing your code involves limiting the number of times your program reads and writes to memory. You’ll need some insight into when and why these read and write operations happen.

%d 블로거가 이것을 좋아합니다: