Binary, Decimal and Hexadecimal Numbers

Slides:



Advertisements
Similar presentations
Number Systems and Codes
Advertisements

Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
How Computers Represent Numbers Friday, Week 5. Binary Code A series of 1’s and 0’s Place value is in powers of 2.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.

Introduction to Programming with Java, for Beginners
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Binary Number Systems.
Simple Data Type Representation and conversion of numbers
CS-280 Dr. Mark L. Hornick 1 ASCII table. 2 Displaying Numbers as Text Problem: display numerical values as text Consider the numerical value 0x5A held.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Chapter Contents.
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
The Teacher CP4 Binary and all that… CP4 Revision.
The Teacher CP4 Binary and all that… CP4 Revision.
CISC1100: Binary Numbers Fall 2014, Dr. Zhang 1. Numeral System 2  A way for expressing numbers, using symbols in a consistent manner.  " 11 " can be.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Very Basic Mathematical Concepts for Programmers
DECIMALBINARY a) b) c) d) e) f) Revision Exercise DECIMALBINARY a) b) c)
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
Data Representation. How is data stored on a computer? Registers, main memory, etc. consists of grids of transistors Transistors are in one of two states,
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
1 Bits, Bytes, Binary & Hex CIS TAG ▪ Byte Bit.
Base 16 (hexadecimal) Uses the decimal digits and the first letters of the alphabet to encode 4 binary bits (16=2 4 ) abcdef or ABCDEF.
Binary, Decimal and Hexadecimal Numbers Telerik Software Academy C# Fundamentals – Part 2.
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
Data Representation COE 308 Computer Architecture
Computer Science 210 Computer Organization
Compsci 210 Tutorial Two CompSci Semester Two 2016.
Bits, Data Types, and Operations
Programming and Data Structure
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Number Representation
Data Representation Binary Numbers Binary Addition
Multidimensional Arrays
CSCI 198: Lecture 4: Data Representation
CSCI 161: Lecture 4: Data Representation
EPSII 59:006 Spring 2004.
ECE 103 Engineering Programming Chapter 3 Numbers
CS1010 Programming Methodology
Data Representation COE 301 Computer Organization
LING 388: Computers and Language
Computer Science 210 Computer Organization
Cosc 2P12 Week 2.
COMS 161 Introduction to Computing
COMS 161 Introduction to Computing
Homework Homework Continue Reading K&R Chapter 2 Questions?
Data Representation ICS 233
Prof. Giancarlo Succi, Ph.D., P.Eng.
Cosc 2P12 Week 2.
Lecture 36 – Unit 6 – Under the Hood Binary Encoding – Part 2
Data Representation COE 308 Computer Architecture
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Binary, Decimal and Hexadecimal Numbers http://csharpfundamentals.telerik.com Numeral Systems Binary, Decimal and Hexadecimal Numbers Telerik Software Academy Learning & Development Team http://academy.telerik.com

Table of Contents Numeral Systems Representation of Numbers Binary and Decimal Numbers Hexadecimal Numbers Conversion between Numeral Systems Representation of Numbers Positive and Negative Integer Numbers Floating-Point Numbers Text Representation (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Conversion between Numeral Systems (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Decimal Numbers Decimal numbers (base 10) Represented using 10 numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Each position represents a power of 10: 401 = 4*102 + 0*101 + 1*100 = 400 + 1 130 = 1*102 + 3*101 + 0*100 = 100 + 30 9786 = 9*103 + 7*102 + 8*101 + 6*100 = = 9*1000 + 7*100 + 8*10 + 6*1 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Binary Numeral System Binary numbers are represented by sequence of bits (smallest unit of information – 0 or 1) Bits are easy to represent in electronics 1 0 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1

Binary Numbers Binary numbers (base 2) Represented by 2 numerals: 0 and 1 Each position represents a power of 2: 101b = 1*22 + 0*21 + 1*20 = 100b + 1b = 4 + 1 = = 5 110b = 1*22 + 1*21 + 0*20 = 100b + 10b = 4 + 2 = = 6 110101b = 1*25 + 1*24 + 0*23 + 1*22 + 0*21 + 1*20 = = 32 + 16 + 4 + 1 = = 53 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Binary to Decimal Conversion Multiply each numeral by its exponent: 1001b = 1*23 + 1*20 = 1*8 + 1*1 = = 9 0111b = 0*23 + 1*22 + 1*21 + 1*20 = = 100b + 10b + 1b = 4 + 2 + 1 = = 7 110110b = 1*25 + 1*24 + 0*23 + 1*22 + 1*21 = = 100000b + 10000b + 100b + 10b = = 32 + 16 + 4 + 2 = = 54 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Decimal to Binary Conversion Divide by 2 and append the reminders in reversed order: 500/2 = 250 (0) 250/2 = 125 (0) 125/2 = 62 (1) 62/2 = 31 (0) 500d = 111110100b 31/2 = 15 (1) 15/2 = 7 (1) 7/2 = 3 (1) 3/2 = 1 (1) 1/2 = 0 (1) (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Hexadecimal Numbers Hexadecimal numbers (base 16) Represented using 16 numerals: 0, 1, 2, ... 9, A, B, C, D, E and F Usually prefixed with 0x 0  0x0 8  0x8 1  0x1 9  0x9 2  0x2 10  0xA 3  0x3 11  0xB 4  0x4 12  0xC 5  0x5 13  0xD 6  0x6 14  0xE 7  0x7 15  0xF (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Hexadecimal Numbers (2) Each position represents a power of 16: 9786hex = 9*163 + 7*162 + 8*161 + 6*160 = = 9*4096 + 7*256 + 8*16 + 6*1 = = 38790 0xABCDEFhex = 10*165 + 11*164 + 12*163 + 13*162 + 14*161 + 15*160 = = 11259375 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Hexadecimal to Decimal Conversion Multiply each digit by its exponent 1F4hex = 1*162 + 15*161 + 4*160 = = 1*256 + 15*16 + 4*1 = = 500d FFhex = 15*161 + 15*160 = = 240 + 15 = = 255d (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Decimal to Hexadecimal Conversion Divide by 16 and append the reminders in reversed order 500/16 = 31 (4) 31/16 = 1 (F) 500d = 1F4hex 1/16 = 0 (1) (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Binary to Hexadecimal (and Back) Conversion The conversion from binary to hexadecimal (and back) is straightforward: each hex digit corresponds to a sequence of 4 binary digits: 0x0 = 0000 0x8 = 1000 0x1 = 0001 0x9 = 1001 0x2 = 0010 0xA = 1010 0x3 = 0011 0xB = 1011 0x4 = 0100 0xC = 1100 0x5 = 0101 0xD = 1101 0x6 = 0110 0xE = 1110 0x7 = 0111 0xF = 1111

Numbers Representation Positive and Negative Integers and Floating-Point Numbers (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Representation of Integers A short is represented by 16 bits 100 = 26 + 25 + 22 = = 00000000 01100100 An int is represented by 32 bits 65545 = 216 + 23 + 20 = = 00000000 00000001 00000000 00001001 A char is represented by 16 bits '0' = 48 = 25 + 24 = = 00000000 00110000 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Positive and Negative Numbers A number's sign is determined by the Most Significant Bit (MSB) Only in signed integers: sbyte, short, int, long Leading 0 means positive number Leading 1 means negative number Example: (8 bit numbers) 0XXXXXXXb > 0 e.g. 00010010b = 18 00000000b = 0 1XXXXXXXb < 0 e.g. 10010010b = -110 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Positive and Negative Numbers (2) The largest positive 8-bit sbyte number is: 127 (27 - 1) = 01111111b The smallest negative 8-bit number is: -128 (-27) = 10000000b The largest positive 32-bit int number is: 2 147 483 647 (231 - 1) = 01111…11111b The smallest negative 32-bit number is: -2 147 483 648 (-231) = 10000…00000b (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Representation of 8-bit Numbers Positive 8-bit numbers have the format 0XXXXXXX Their value is the decimal of their last 7 bits (XXXXXXX) Negative 8-bit numbers have the format 1YYYYYYY Their value is 128 (27) minus (-) the decimal of YYYYYYY 10010010b = 27 – 10010b = = 128 - 18 = -110 +127 = 01111111 ... +3 = 00000011 +2 = 00000010 +1 = 00000001 +0 = 00000000 -1 = 11111111 -2 = 11111110 -3 = 11111101 -127 = 10000001 -128 = 10000000 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Floating-Point Numbers Floating-point numbers representation (according to the IEEE 754 standard*): Example: 2 k - 1 n S P ... M Sign Exponent Mantissa * See http://en.wikipedia.org/wiki/Floating_point Bit 31 Bits [3 … 23 ] Bits [2 2 … ] 1 10000011 01010010100000000000000 Sign = - 1 Exponent = 4 Mantissa = 1,3222656 25

Text Representation in Computer Systems (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

How Computers Represent Text Data? A text encoding is a system that uses binary numbers (1 and 0) to represent characters Letters, numerals, etc. In the ASCII encoding each character consists of 8 bits (one byte) of data ASCII is used in nearly all personal computers In the Unicode (UTF-16) encoding each character consists of 16 bits (two bytes) Can represent many alphabets (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Character Codes – ASCII Table Binary Code Decimal Code Character 01000001 65 A 01000010 66 B 01000011 67 C 01000100 68 D 00100011 35 # 01100000 48 00110001 49 1 01111110 126 ~ Excerpt from the ASCII table 22 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Strings of Characters Strings are sequences of characters Null-terminated (like in C) Represented by array Characters in the strings can be: 8 bit (ASCII / windows-1251 / …) 16 bit (UTF-16) … \0 4 bytes length … 23

Numeral Systems http://csharpfundamentals.telerik.com

Exercises Write a program to convert decimal numbers to their binary representation. Write a program to convert binary numbers to their decimal representation. Write a program to convert decimal numbers to their hexadecimal representation. Write a program to convert hexadecimal numbers to their decimal representation. Write a program to convert hexadecimal numbers to binary numbers (directly). Write a program to convert binary numbers to hexadecimal numbers (directly). (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Exercises (2) Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤ 16). Write a program that shows the binary representation of given 16-bit signed integer number (the C# type short). * Write a program that shows the internal binary representation of given 32-bit signed floating-point number in IEEE 754 format (the C# type float). Example: -27,25  sign = 1, exponent = 10000011, mantissa = 10110100000000000000000.

Free Trainings @ Telerik Academy “C# Programming @ Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com