Binary Numbers and Logic By Bill T. April 21, 1998 Introduction ----------------------------------------------------------------------------- Having a basic understanding of how a computer works is essential to successfull programming. Merely jumping in blindly and trying to hack some code together is not even a remotely good practice. Lacking the basic knowledge leads to bugged and bloated programs, not to mention long hours of coding, testing and debugging. Wouldn't it be nice if everyone took the time to properly write a fast and efficient program, instead of rushing it out the door?? This lesson teaches the very basics of computers, binary numbers and boolean logic. These are simple concepts, and if you do not already understand them, you would be well advised to do so... Note: This tutorial may actually lessen your knowledge of these topics... This is not my intent, but could be the result nevertheless... For a much more in-depth (and better) discussion, go to: Binary Numbers: http://webster.ucr.edu/Page_asm/ArtofAssembly/CH01/CH01-1.html#HEADING1-34 Boolean Logic: http://webster.ucr.edu/Page_asm/ArtofAssembly/CH02/CH02-1.html#HEADING1-3 Binary Numbers ----------------------------------------------------------------------------- Decimal Numbers: In order to understand binary numbers, we must first refresh our knowledge of the decimal number system. If you do not already know this information, assembly programming is probably not for you....instead, go and play some video games, they're very entertaining. Anyways... The decimal number system is base-10, meaning its digits are based upon powers of 10. It also means that each digit can represent one of 10 different values (0-9). For example, the number 443556 can represented as: 443556 = 4x10^5 + 4x10^4 + 3x10^3 + 5x10^2 + 5x10^1 + 6x10^0 = 400,000 + 40,000 + 3,000 + 500 + 50 + 6 Binary Numbers: The binary number system is base-2; its digits are based upon powers of 2. Each digit can represent one of 2 different values (0,1). To convert binary to decimal is simple: 0100 1010b = 0x2^7 + 1x2^6 + 0x2^5 + 0x2^4 + 1x2^3 + 0x2^2 + 1x2^1 + 0x2^0 = 64 + 8 + 2 = 74d Converting decimal to binary is slightly more complex: You'll need to work backwards, finding the greatest power of 2 which is less than the number you wish to convert. Then continue for each lesser power of 2 until 2^0. For Example, convert 1998 to binary: 1x2^10 = 1024 1998-1024 = 974 1x2^9 = 512 974-512 = 462 1x2^8 = 256 462-256 = 206 1x2^7 = 128 206-128 = 78 1x2^6 = 64 78-64 = 14 ... 1x2^3 = 8 14-8 = 6 1x2^2 = 4 6-4 = 2 1x2^1 = 2 2-2 = 0 1998d = 0111 1100 1110b In a computer, all data is stored in binary form. Each binary digit in a computers is known as a bit. A byte is equal to 8 bits, while a word is 16 bits, and a dword is 32 bits. Hexadecimal Numbers: Hexadecimal (Hex) numbers are base-16. They are a concise way of representing binary numbers. It represents each byte of data as a base-16 number. Because there are only 10 numerals, the letters A-F are used for the remaining 6 digits. Following is a table of binary numbers and their hexadecimal equivalents: Bin Hex Bin Hex Bin Hex Bin Hex 0000 0 0100 4 1000 8 1100 C 0001 1 0101 5 1001 9 1101 D 0010 2 0110 6 1010 A 1110 E 0011 3 0111 7 1011 B 1111 F Therefore: 0010 1001 1010b = 29Ah As you can see, 29A is much easier to read and write than it is in binary form. Plus, the conversion is trivial, so it makes sense to use hex numbers when dealing with computers... Boolean Logic ----------------------------------------------------------------------------- Everyone should remeber logic from math class.. If not heres a brief introduction to it. Boolean logic is a math system closed over the values 0 and 1. I will not give a complete discussion of boolean logic, just a few minor points that may be useful for programming. AND OR XOR NOT X Y Z X Y Z X Y Z X Z 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 ----------------------------------------------------------------------------- Copyright (C) 1998 Bill T. (billasm@usa.net)