CET 3510 – Lecture 11 Bit Manipulation in a High-Level Programming Language Dr. José M. Reyes Álamo.

Slides:



Advertisements
Similar presentations
7-5 Microoperation An elementary operations performed on data stored in registers or in memory. Transfer Arithmetic Logic: perform bit manipulation on.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
1 Homework / Exam Reading –PAL, pp Homework –mp2 due before class number 12 Exam #1 –Class 13 (three sessions from today) –Open book / Open notes.
Introduction to Assembly Here we have a brief introduction to IBM PC Assembly Language –CISC instruction set –Special purpose register set –8 and 16 bit.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Introduction to C Programming CE Lecture 8 Bitwise Operations.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Shift and Rotate Instructions
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Introduction to Computer Engineering by Richard E. Haskell Shift and Rotate Instructions Module M16.2 Section 10.3.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Ch. 7 Logic, Shift and Rotate instr.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Department of Computer Science and Software Engineering
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Logical and Bit Operations Chapter 9 S. Dandamudi.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 1 Chapter Four.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
Computer Architecture CST 250
Assembly Language Programming IV: shift, struct, recursion
Data Transfers, Addressing, and Arithmetic
Chapter 2 Assignment and Interactive Input
Microprocessor Systems Design I
Aaron Miller David Cohen Spring 2011
Bit Operations Horton pp
Chapter 3 Bit Operations
Chapter 7 Additional Control Structures
Fundamentals of Computer Organisation & Architecture
Computer Architecture and Assembly Language
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Programming
CS-401 Computer Architecture & Assembly Language Programming
Shift & Rotate Instructions)
By: A. H. Abdul Hafez CAO, by Dr. A.H. Abdul Hafez, CE Dept. HKU
Shift & Rotate Instructions)
Shift, Multiply, and Divide
Branching instructions
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Repetition Statements (Loops) - 2
Microprocessor and Assembly Language
Shift and Rotate Instructions.
Bit Manipulations CS212.
Computer Architecture and System Programming Laboratory
CS-401 Computer Architecture & Assembly Language Programming
Bit Operations Horton pp
Presentation transcript:

CET 3510 – Lecture 11 Bit Manipulation in a High-Level Programming Language Dr. José M. Reyes Álamo

Bitwise Operations A logical operation that is performed between bit of the operand(s) in their corresponding position Bitwise AND Bitwise OR Bitwise NOT Bitwise XOR

Bitwise Operations Recipe: HLA Code Example: mov( eax, ecx ); Move the first operand into an appropriate register Perform the corresponding operation proving the first and second operand HLA Code Example: mov( eax, ecx ); and( ebx, ecx ); or( ebx, ecx ); xor( ebx, ecx ); mov( ebx, ecx ); not( ecx ); //BE CAREFUL

Bitwise Operations in C/C++ Result is the same, syntax is different Bitwise AND operator: & Bitwise OR operator: | Bitwise NOT operator: ~ Bitwise XOR operator: ^ Be careful: Do not confuse these with the logical comparison operators: &&, ||, !

Bitwise Operations in C/C++ Code Example: void logicalOperations(){ int x, y, z; x = 5; y = 2; z = x & y; cout << "x & y = " << z << endl; z = x | y; cout << "x | y = " << z << endl; z = x ^ y; cout << "x ^ y = " << z << endl; z = ~x; cout << "~x = " << z << endl; system("PAUSE"); }

Shifting Left in HLA Shift left move each bit n positions to the left Lower order bits becomes a 0 Higher order bit become a carry out HLA code: shl( count, dest ) | count = positions to shift; dest = register or variable Shifting n places to the left one position is equivalent to multiplying by the base (radix) n times (radix)

Shifting Right in HLA Shift right move each bit n positions to the right Higher order bits becomes a 0 Lower order bit become a carry out HLA code: shr( count, dest ) | count = positions to shift; dest = register or variable sar( count, dest ) | count = positions to shift; dest = register or variable. Used for arithmetic shifting. Shifting n places to the right one position is equivalent to dividing by the base (radix) n times

Shifting in C/C++ The bitwise shifting operators are Right shift (>>) Left shift (<<) Be careful with the compiler as shifting (especially shifting right) might return unexpected results

Shifting in C++ Code Example: void shiftingOperations(){ } int x, y, z; x = 2; x = x << 5; cout << "x shifted left = " << x << endl; x = x >> 1; cout << "x shifted right = " << x << endl; }

Bit Manipulation Example HLA HLA Code: stdout.put( “Enter the current month, day, and year: “ ); stdin.get( month, day, year ); mov( 0, ax ); mov( ax, packedDate ); //Just in case there is an error.   mov( month, al ); shl( 5, ax ); or( day, al ); shl( 7, ax ); or( year, al ); mov( ax, packedDate ); stdout.put( “Packed data = $”, packedDate, nl );

Bit Manipulation Example C++ void packedDate(){ int month, day, year, packedDate; cout << "Enter the current month, day, and year: " << endl; cin >> month >> day >> year; packedDate = 0; packedDate = packedDate | month; packedDate = packedDate << 5; packedDate = packedDate | day; packedDate = packedDate << 7; packedDate = packedDate | year; cout << "Packed date is " << hex << packedDate << endl; }