Hexadecimal and Binary Conversion PDF

Document Details

FruitfulMesa

Uploaded by FruitfulMesa

Kwara State University

Tags

number system conversion hexadecimal conversion binary conversion computer science

Summary

This document explains how to convert between hexadecimal and binary number systems. It provides examples and tables to illustrate the conversion process, including segmenting bit streams and decimal equivalencies. It also introduces the concept of integers and floating-point numbers.

Full Transcript

Reading the remainders upwards as shown by the arrow give 1101001~2~ as the solution. **1.3 Hexadecimal Numbers (base 16)** Hexadecimal numbers have 16 different states/numerals. It uses a combination of the first ten numerals of the decimal numbers and the first six alphabets. This shown in table...

Reading the remainders upwards as shown by the arrow give 1101001~2~ as the solution. **1.3 Hexadecimal Numbers (base 16)** Hexadecimal numbers have 16 different states/numerals. It uses a combination of the first ten numerals of the decimal numbers and the first six alphabets. This shown in table 2.3 **State** **Hexadecimal** **Decimal Equivalent** **Binary Equivalent** ----------- ----------------- ------------------------ ----------------------- 1 0 0 0000 2 1 1 0001 3 2 2 0010 4 3 3 0011 5 4 4 0100 6 5 5 0101 7 6 6 0110 8 7 7 0111 9 8 8 1000 10 9 9 1001 11 A 10 1010 12 B 11 1011 13 C 12 1100 14 D 13 1101 15 E 14 1110 16 F 15 1111 A prefix of 0x before a number shows it is a hexadecimal number e.g. 0x3A7. The conversion of a binary number to hexadecimal involves dividing the binary number into a section of four bits and each nibble (4 bits stream) converted to its hexadecimal equivalent using table 2.4. For example a conversion of 11010011100100101011 to hex will involve the following Step 1: Segment the bit stream to 4 bits 1101 0011 1001 0010 1011 ---------------------------------------------- --------- ------ ------ ------ ------ Step 2: Convert each bit to Hex D 3 9 2 B Step 3: Join the hex together with 0x prefix 0xD392B Conversion to decimal will involve a numbering all the numerals, starting from 0 for the least significant bit. As an example table 2.4 shows the conversion of 0xD392B to decimal. **Power of Base** 4 3 2 1 0 **Decimal Equivalent** ------------------- --- --- --- --- --- ------------------------------------------------------------------------ **Binary Number** D 3 9 2 B D(13) x 16^4^ + 3 x 16^3^+ 9 x 16^2^+2 x 16^1^+ B(11) x 16^0^ = 866603 The conversion of decimal to hexadecimal, just like the conversion from decimal to binary, involves series of division (this time by 16) and remainders written from the last to the first gives the hexadecimal number. For example conversion of 734593 to hex is: Divide Remainder 16 734593 ---- -------- -------- 16 45912 1 16 2869 8 16 179 5 16 11 3 0 11 (B) From the table, 734593~10~ = 0xB3581. **1.4 Integers and Floating Point Numbers** There are a lot of different types of numbers and each is different from another, yet they may share some common characteristics. To help keep track of and understand the similarities and differences between numbers, mathematicians have developed a grouping system that categorizes and describes numbers based on their characteristics. Some of the most common groups in the system are: Natural numbers, Whole numbers, Integers, Rationals, Real numbers **Integers** An integer is a whole number (is not a fractional number) that can be positive, negative, or zero. Examples of integers are: -6, 2, 8, -95, and 2015. Examples of numbers that are not integers are: -2.37, 5/7, 3.142,.07, and 45.1. The set of integers, denoted ***Z***, is formally defined as follows: ***Z*** = {\..., -3, -2, -1, 0, 1, 2, 3 \...}. Integers are represented in computers using two notations: two's complement notation and the excess notation, both of which are based on binary numbers. In the two's notation, a fixed number of bits are used to represent the integers. The number of bits used determines the range of integers that the notation can represent. Today's computers use 32 bits to represent integers, but we shall be using a 4 bit system to explain how it works. The most significant bit (that is the rightmost bit is used to represent the sign of the number, a 0 is used to represent positive, while a 1 depicts a negative. The procedure for obtaining the 2's complement of a positive number is: First to get its binary equivalent, secondly flip all the bits (convert all the 0 to 1, and the 1 to 0) and finally add 1 to the last bit on the right. **Bit pattern** **Value represented** ----------------- ----------------------- 0111 7 0110 6 0101 5 0100 4 0011 3 0010 2 0001 1 0000 0 1111 -1 1110 -2 1101 -3 1100 -4 1011 -5 1010 -6 1001 -7 1000 -8 A four bit system can only represent the integers from +7 to -8, this is why todays computers use more bits to represent integers. For example, find how a computer would represent a -14 using its two's complement. Step 1: Obtain the binary equivalent of 14, this is 1101 Step 2: Add the sign bit, 01101 (it is zero for the positive 14) Step 3: Flip all the bits, this results in 10010 Step 4: Add 1 to the last bit, this results in 10011. Therefore the two's complement representation for -14 is 10011. To represent -14, a minimum of five bits is required. Integers can be represented also using another method known as the excess notation. To get this notation system, the bit pattern length to be used is identified and the different binary bit pattern listed from 0000.... Upwards. We then identify the point where the most significant bit changes sign, and assign 0 to that point. All bit patterns upwards of that point is positive, while those below represents negative integers. Table 2.7 shows a four bit length used to represent integers using the excess notation, the shaded portion represents the part with 1 as the most significant bit and therefore representing the positive integers. Excess Notation Integer Represented ----------------- --------------------- 1111 7 1110 6 1101 5 1100 4 1011 3 1010 2 1001 1 1000 0 0111 -1 0110 -2 0101 -3 0100 -4 0011 -5 0010 -6 0001 -7 0000 -8 **Floating Point Numbers** Floating point numbers refers to numbers that are not whole, but contain a part that is less than one (a fractional part). There is usually a decimal point between the part of the number greater than one and the part that is less. These numbers are represented to the computer in a form called floating point notation. The highest order bit is designated as the sign bit, a zero in this bit signifies a positive number, while a 1 is used to represent a negative number. The remaining bit is divided into two parts, the exponent (used to hold the part greater than one) and the mantissa (which holds the part less than one). For example, in an eight bit system, the first bit is used as a sign bit, the next 3 are the exponent and the last four used to hold the mantissa. For example, if a floating point notation contains 01011101, what is the binary number represented? What is the decimal equivalent of the notation? Break up the given bit stream to the three segments Sign Bit Exponent Mantissa ---------- ---------- ---------- 0 101 1101 The given stream represents a positive bit stream of 101.1101. To obtain the decimal equivalent, a conversion to decimal using the table below is necessary Sign bit Exponent Mantissa -------------------- ---------- ------------------------------------------------------------------------------------- ---------- --- ---- ---- ---- ---- Binary Number 0 1 0 1 1 1 0 1 Power of Base 2 1 0 -1 -2 -3 -4 Decimal Equivalent \+ 1 x 2^2^+ 0 x 2^1^+1 x 2^0^+ 1 x 2^-1^ + 1 x 2^-2^ + 0 x 2^-3^ + 1 x 2^-4^ = 5.8125 Therefore the bit stream 101.1101 represents 5.8125. The representation in the binary notation can introduce error into the system due to the limited number of bits used to hold both the exponent and the mantissa. This error is known as truncation error or round-off error. This error is reduced by using more bits to represent the mantissa field and in today's computers use at least 32 bits for storing values in the floating point notation instead of the 8 bits used in the examples. **Representation of Non-Numeric Data, Records and Registers** **1.0 Introduction** In the previous unit, number bases were introduced to be the number of numerals or states that the number can contain. The common bases that are used to represent data to the computer system are the base 2 (binary), base 10 (decimal) and base 16 (hexadecimal). This unit will be focusing on the representation of non-numeric data such as alphabets and special characters, and also records and registers will be introduced. **1.1 Representation of Non-numeric Data** Non-numeric data refers data that are not numbers and cannot be arranged sequentially based on their values. These cannot be added nor subtracted like numeric data and examples include alphabets, special characters, images and sound. The number bases explained earlier can be used to represent numeric data to computers, but the representation of non-numeric data makes use of a coded format. The coded format used for each type is different and can be varied. This unit will focus on the representation of alphabets and special characters. The American National Standards Institute (ANSI) can up with the American Standard Code for Information Interchange (ASCII) which have been adopted widely and used to code the representation of sletters. In this code seven bits are used to represent the upper and lower case alphabets, punctuation marks and other control functions on the keyboard such as the carriage return, tabs and backspace. Show in Table 3.1 is the ASCII representation for alphabets and special characters. From the sequence shown, the codes for the English letters are in the same sequence as their lexical order. As such the codes for alphabets can be determined if the codes for the lower and upper case of 'A' are known. C:\\Users\\Michael\\Documents\\Standard-ASCII-Table.jpg Table 3.1: Table of ASCII Codes Example: What will be the ASCII code (in binary) to represent CODeL! From the table, you can obtain the decimal code for each of the characters of the word CODel! Then convert each of the decimal code to binary. Note that the case of the letter matters as upper case codes defers from lower case codes. Letter C O D E l ! --------- --------- --------- --------- --------- --------- --------- Decimal 67 79 68 101 108 33 Binary 1000011 1001111 1000100 1100101 1101100 0100001 Therefore the ASCII code for CODel!is100001110011111000100110010111011000100001. The ASCII code is limited in the number of bits used in the coding. As such cannot be used to represent the alphabets of languages that have a large number of alphabets such as Chinese and Japanese. A more recent oding system known as the Unicode have been developed by leading computer manufacturers which uses 16 bits to represent each symbol, resulting in 65,536 unique different pattern that can handle languages with large number of alphabets. The International Organization for Standardization have also developed a code format that uses 32 bits for its encoding. This will be able to represent billions of different symbols. **1.2 Computer Records and Registers** Generally, a record is a combination of various [data](http://www.webopedia.com/TERM/D/data.html) [objects](http://www.webopedia.com/TERM/O/object.html) such as some [integers](http://www.webopedia.com/TERM/I/integer.html), [floating-point number](http://www.webopedia.com/TERM/F/floating_point_number.html)s, and [character string](http://www.webopedia.com/TERM/C/character_string.html)s that are arranged for processing by a program. A file contains multiple records and can also be referred to as a data set. The programming language and/or the application used determines the organization of data in the record. Typically, records can be of fixed-length or be of variable length with the length information contained within the record. A register is a memory that is built into the central processing unit to speed up its operations by providing access to commonly used values. They are usually of small value and can be accessed very quickly. They are made of semiconductor devices whose contents can be read and written to at extremely high speeds but the contents are held only temporarily, usually while in use or only as long as the power supply is on. Registers are the fastest way for the system to manipulate data and are at the top of the memory hierarchy. Registers are normally measured by the number of bits they can contain. Examples include an 8-bit register (can store 8 bits of data), a 32-bit register (can store 32 bit of data). There are basically two kinds of registers, a set of 32 registers that is accessible to a programmer (known as the General Purpose Registers) and another set of 6 registers not accessible to programmers that is used in interpreting and executing instructions. Data and instructions must be put into the system. So we need registers for this. The basic computer registers with their names, size (for an 8085 microprocessor) and functions are listed below, the size indicate can vary depending on the processor in questsion. +-----------------+-----------------+-----------------+-----------------+ | **Register Sy | | | | | mbol** **Regi | | | | | ster Name** | | | | | **Number of B | | | | | its** **Descr | | | | | iption** | | | | | ------------- | | | | | -------- ------ | | | | | --------------- | | | | | - ------------- | | | | | ------- ------- | | | | | --------------- | | | | | --------------- | | | | | --------------- | | | | | --------------- | | | | | --------------- | | | | | --------- | | | | | AC | | | | | Accumu | | | | | lator | | | | | 16 | | | | | One of | | | | | the Processor R | | | | | egister that ho | | | | | lds operands an | | | | | d the result of | | | | | arithmetic ope | | | | | rations | | | | | DR | | | | | Data R | | | | | egister | | | | | 16 | | | | | Hold me | | | | | mory data | | | | | TR | | | | | Tempor | | | | | ary Register | | | | | 16 | | | | | Holds t | | | | | emporary Data | | | | | IR | | | | | Instru | | | | | ction Register | | | | | 16 | | | | | Holds t | | | | | he current inst | | | | | ruction code be | | | | | ing execute | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | AR | | | | | Addres | | | | | s Register | | | | | 12 | | | | | Holds m | | | | | emory address | | | | | PC | | | | | Progra | | | | | m Counter | | | | | 12 | | | | | Holds a | | | | | ddress of next | | | | | instruction, af | | | | | ter executing t | | | | | he current inst | | | | | ruction | | | | | INPR | | | | | Input | | | | | Register | | | | | 8 | | | | | Holds I | | | | | nput data | | | | | OUTR | | | | | Output | | | | | Register | | | | | 8 | | | | | Holds O | | | | | utput data | | | | +=================+=================+=================+=================+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | +-----------------+-----------------+-----------------+-----------------+

Use Quizgecko on...
Browser
Browser