Number Systems and Bitwise Operation

Slides:



Advertisements
Similar presentations
NUMBER SYSTEM. How to convert hexadecimal numbers to decimal numbers? 230 Working from right to left, MULTIPLY each position with 8 raised to the power.
Advertisements

DATA REPRESENTATION CONVERSION.
Company LOGO Edit your slogan here DKT 122/3 DIGITAL SYSTEM 1 WEEK #3 NUMBER SYSTEMS, OPERATION & CODES (PART 2)
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.
Chapter 1 Number Systems and Codes William Kleitz Digital Electronics with VHDL, Quartus® II Version Copyright ©2006 by Pearson Education, Inc. Upper Saddle.
Converting Binary to Octal
Number Systems and Arithmetic
COE 202: Digital Logic Design Number Systems Part 1
Number Systems and Codes In PLC
Number Systems Lecture 02.
Revision Introductory Lesson
Binary and Hexadecimal Numbers
Number Systems.
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Conversion of Number System Conversion Among Bases The possibilities: Hexadecimal DecimalOctal Binary
Number Systems. Today Decimal Hexadecimal Binary –Unsigned Binary –1’s Complement Binary –2’s Complement Binary.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
Computer Number Systems. d n-1 d n-2 d n d 2-m d 1-m d -m Conventional Radix Number r is the radixd i is a digit d i Є {0, 1, ….., r – 1 } -m ≤
Number systems, Operations, and Codes
Numbering System Base Conversion. Number systems Decimal – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary – 0, 1 Octal – 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal system.
Number Systems and Codes
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Octal to Decimal Hexadecimal DecimalOctal Binary.
Chapter 1  Number Systems Decimal System Binary System Octal System Hexadecimal System  Binary weighted cods Signed number binary order  1’s and 2’s.
AEEE2031 Data Representation and Numbering Systems.
DECIMALBINARY a) b) c) d) e) f) Revision Exercise DECIMALBINARY a) b) c)
Binary01.ppt Decimal Decimal: Base 10 means 10 Unique numerical digits ,00010,000 Weight Positions 3,
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Introduction to Microprocessors Chapter 2. Decimal or Base 10 Numbers  Have ten different digits (0-9)  It is a weighted number system. Each position.
WEEK #2 NUMBER SYSTEMS, OPERATION & CODES (PART 1)
Number Representation and Arithmetic Circuits
Number Systems. Topics  The Decimal Number System  The Binary Number System  Converting from Binary to Decimal  Converting from Decimal to Binary.
Chapter 5 NUMBER REPRESENTATION AND ARITHMETIC CIRCUITS.
CHAPTER 3 BINARY NUMBER SYSTEM. Computers are electronic machines which operate using binary logic. These devices use two different values to represent.
1 Digital Logic Design (41-135) Chapter 5 Number Representation & Arithmetic Circuits Younglok Kim Dept. of Electrical Engineering Sogang University Spring.
CPE 201 Digital Design Lecture 2: Digital Systems & Binary Numbers (2)
Unit 1 Introduction Number Systems and Conversion.
Positional Number Systems Decimal, Binary, Octal and Hexadecimal Numbers Wakerly Section
Number Systems and Codes
Octal to Decimal Decimal Octal Binary Hexadecimal.
CHAPTER 1 : INTRODUCTION
Digital Electronics Number Systems and Codes
ITE102 – Computer Programming (C++)
CSE 102 Introduction to Computer Engineering
Location in course textbook
ECE 103 Engineering Programming Chapter 3 Numbers
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
Chapter 1 Number Systems & Conversions
TAO1221 COMPUTER ARCHITECTURE AND ORGANIZATION LAB 6
Number Systems Base 2, 10, 16.
Data Representation Data Types Complements Fixed Point Representation
MMNSS COLLEGE,KOTTIYAM DEPARTMENT OF PHYSICS
Data Representation in Computer Systems
Numbering System TODAY AND TOMORROW 11th Edition
Numbering Systems and Arithmetic operations on Hex, binary, and octal
Digital Logic Design (ECEg3141) 2. Number systems, operations & codes 1.
Digital Electronics and Microprocessors
There are 10 types of people of people in this world…
Homework Homework Continue Reading K&R Chapter 2 Questions?
Binary Addition (1 of 2) Two 1-bit values A B A + B 1
ECE 331 – Digital System Design
Module 10 Operations on Bits
Remember the 10 types of people of people in this world…
Computer Architecture CST 250
1. Number Systems Chapt. 2.
Presentation transcript:

Number Systems and Bitwise Operation UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Binary, Octal, Hexadecimal, and Decimal Binary Binary numbering system has only two possible values for each digit: 0 and 1. For example, binary number decimal number 0 0 1 1 10 2 11 3 100 4 101 5 110 6 1100 1010 202 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Decimal Numbers Decimal The digits' weight increases by powers of 10. The weighted values for each position is determined as follows: 104 103 102 101 100 10000 1000 10 1 For example, A decimal number 4261 can be thought of as follows. 4 * 1000 + 2 * 100 + 6 * 10 + 1 * 1 = 4000 + 200 + 60 + 1 = 4261 (decimal) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Binary, Octal, Hexadecimal, and Decimal The digits' weight increases by powers of 2. The weighted values for each position is determined as follows: 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 For example, binary 10 is decimal 2. the binary value 1100 1010 represents the decimal value 202. 1 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202 (decimal) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Binary Two’s Complement The left-most bit is the sign bit. If it is 1, then the number is negative. Otherwise, it is positive. Give a negative value, represent it in binary two’s complement form as follows. write the number in its absolute value. complement the binary number. plus 1. Example, represent –2 in binary two’s complement with 16 bits for short int. Binary value of 2: 0b0000 0000 0000 0010 Binary complement of 2: 0b1111 1111 1111 1101 Plus 1: +1 Binary two’s complement representation of -2: 0b1111 1111 1111 1110 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Give binary two’s complement form of a negative number, find the absolute value of the negative value as follows. Complement the binary number. Plus 1. Example, find the decimal value of (0b1111 1111 1111 1110)2 in binary two’s complement form with 16 bits. Binary two’s complement (0b1111 1111 1111 1110)2 Binary complement (0b0000 0000 0000 0001)2 Plus 1 +1 Absolute value: (0b0000 0000 0000 0010)2 = 210 Negative value: -2 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Subtraction of a value in the computer can be treated as addition of its two’s complement. For example, the subtraction of (2-2) can be performed as 2+(-2) as follows: 0b0000 0000 0000 0010 (binary representation of 2) 0b1111 1111 11111110 (two’s complement representation of -2) 0b0000 0000 0000 0000 (2+(-2)) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Example > short i, j > i = 0b0000000000000010 2 > j = 0b1111111111111110 -2 > i+j UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Octal The octal system is based on the binary system with a 3-bit boundary. The octal number system uses base 8 includes 0 through 7. The weighted values for each position is as follows: 83 82 81 80 512 64 8 1 Binary to Octal Conversion Break the binary number into 3-bit sections from the least significant bit (LSB) to the most significant bit (MSB). Convert the 3-bit binary number to its octal equivalent. For example, the binary value 1 010 000 111 101 110 100 011 equals to octal value (12075643)8. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Octal to Binary Conversion Convert the octal number to its 3-bit binary equivalent. Combine all the 3-bit sections. For example, the octal value 45761023 equals to binary value 100 101 111 110 001 000 010 011. Octal to Decimal Conversion To convert octal number to decimal number, multiply the value in each position by its octal weight and add each value together. For example, the octal value (167)8 represents decimal value 119. 1*64 + 6*8 + 7*1 = 119 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Hexadecimal Similar to octal, the hexadecimal system is also based on the binary system but using 4-bit boundary. The hexadecimal number system uses base 16 including the digits 0 through 9 and the letters A, B, C, D, E, and F. The letters A through F represent the decimal numbers 10 through 15. For the decimal values from 0 to 15, the corresponding hexadecimal values are listed below. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 F E D C B A Decimal Hexadecimal UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming The weighted values for each position is as follows: 163 162 161 160 4096 256 16 1 The conversion between binary value and hexadecimal value is similar to octal number,but using four-bit sections. The hexadecimal value 20A represents decimal value 522. 2*256 + 0*16 + 10*1 = 522 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Following table provides all the information you need to convert from one type number into any other type number for the decimal values from 0 to16. Binary Octal Decimal Hex 0000 00 1001 11 09 0001 01 1010 12 10 A 0010 02 1011 13 B 0011 03 1100 14 C 0100 04 1101 15 D 0101 05 1110 16 E 0110 06 1111 17 F 0111 07 10000 20 1000 08 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Bitwise Operators There are six bitwise operators: Operator Name Description & bitwise AND The bit is set to 1 if the corresponding bits in the two operands are both 1. | bitwise OR The bit is set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bit is set to 1 if exactly one of the corresponding bits in the two operands is 1. << left shift Shift the bits of the first operand left by the number of bits specified by the second operand; fill from right with 0 bits. >> right shift Shift the bits of the first operand right by the number of bits specified by the second operand; filling from the left is implementation dependent. ~ One’s complement Set all 0 bits to 1, and all 1 bits to 0. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Example: a 1 0 1 1 0 1 0 0 b 1 1 0 1 1 0 0 1 a & b 1 0 0 1 0 0 0 0 a | b 1 1 1 1 1 1 0 1 a ^ b 0 1 1 0 1 1 0 1 b << 1 1 0 1 1 0 0 1 0 a >> 1 1 1 0 1 1 0 1 0 ~a 0 1 0 0 1 0 1 1 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming /* File: bitop.ch (run in Ch only) Use Ch features “%b” and 0b */ #include <stdio.h> int main() { char a = 0b10110100; char b = 0b11011001; char c; printf("a = 0b%8b\n", a); printf("b = 0b%8b\n", b); c = a & b; printf("a & b = 0b%8b\n", c); c = a | b; printf("a | b = 0b%8b\n", c); c = a ^ b; printf("a ^ b = 0b%8b\n", c); c = b << 1; printf("b << 1 = 0b%8b\n", c); c = a >> 1; printf("a >> 1 = 0b%8b\n", c); c = ~a; printf("~a = 0b%8b\n", c); return 0; } Output: a = 0b10110100 b = 0b11011001 a & b = 0b10010000 a | b = 0b11111101 a ^ b = 0b01101101 b << 1 = 0b10110010 a >> 1 = 0b11011010 ~a = 0b01001011 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Logic Operators There are four logic operators: 1) ! --- logic NOT 2) && --- logic AND 3) || --- inclusive OR 4) ^^ --- exclusive OR (available in Ch only) a b !a a && b a || b a ^^ b 1 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming