Computer Organization and Assembly Language Bitwise Operators.

Slides:



Advertisements
Similar presentations
Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Advertisements

Machine Instructions Operations
INSTRUCTION SET ARCHITECTURES
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
J. Michael Moore Computer Organization CSCE 110. J. Michael Moore High Level View Of A Computer ProcessorInputOutput Memory Storage.
Assembly Language and Computer Architecture Using C++ and Java
Level ISA3: Information Representation
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Assembly Language and Computer Architecture Using C++ and Java
J. Michael Moore Computer Organization CPSC 110. J. Michael Moore High Level View Of A Computer ProcessorInputOutput Memory Storage.
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
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.
Professor Jennifer Rexford COS 217
Prepared by: Sanchita mishra Sonkali bhalavi sunita Submitted to: Shweta agrawal.
Binary Arithmetic Math For Computers.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Data Representation Number Systems.
Arithmetic for Computers
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CPS120: Introduction to Computer Science Lecture 8.
Positional Number Systems
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
IT253: Computer Organization
1 Programming in Machine Language SCSC 311 Spring 2011.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Arithmetic Logic Unit (ALU) Anna Kurek CS 147 Spring 2008.
Number Systems Revision of conversations What is a register Addition Complementation.
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.
IT253: Computer Organization
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise.
Arithmetic Operations
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Integer Operations Computer Organization and Assembly Language: Module 5.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
CSE 220 – C Programming Bitwise Operators.
Chapter 4 Operations on Bits.
Integer Multiplication and Division
Instructor: David Ferry
Bit Operations Horton pp
Chapter 3 Bit Operations
Data Structures Mohammed Thajeel To the second year students
CSCE Fall 2013 Prof. Jennifer L. Welch.
Chapter 14 Bitwise Operators Objectives
Bitwise Operations and Bitfields
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Topic 6: Bitwise Instructions
Shift & Rotate Instructions)
Shift & Rotate Instructions)
Computer Organization
CSCE Fall 2012 Prof. Jennifer L. Welch.
Number Representation & Operators
CPU Structure CPU must:
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Computer Organization and Assembly Language Bitwise Operators

C and C++ allow operations on bits Actually, they work on byte boundaries The data is always assumed to be unsigned integer Operations on other types may have unpredictable results. 2

Bitwise Operators Why bother with bitwise operations? Data compression Encryption Speed But there’s a price in complexity, flexibility, and maintainability 3

SHIFT OPERATORS 4

Shift Operators Left Shift (<<) moves all bits to the left, padding with 0s. << is equivalent to multiplication by powers of 2 But the overflow is lost, so it’s not true multiplication int num = 4;// int result;// ?? result = num << 3;// = 32 5

Shift Operators Right Shift (>>) moves all bits to the right. Unsigned numbers are padded with 0s Signed numbers may preserve sign. Can be thought of as division by powers of 2 Same limitations as multiplication 6

COMPLEMENT 7

Complement Operator Complement (~) flips all bits Not the same as logical NOT (!) ! = 0, ~ = !0 = 1, ~0 = Useful for finding max unsigned integer value long int maxval = ~0;// ?? depends on system Good for building masks (more later) 8

LOGICAL OPERATORS 9

Logical Operators AND (&) – not to be confused with && Operates on bit pairs in different words int num1 = 107; int num2 = 54; int result = num1 & num2;// result = & =

Logical Operators OR (|) – not to be confused with || Operates on bit pairs in different words int num1 = 107; int num2 = 54; int result = num1 | num2;// result = | =

Logical Operators XOR (^) – no corresponding operator Operates on bit pairs in different words int num1 = 107; int num2 = 54; int result = num1 ^ num2;// result = ^ =

COMPRESSION 13

Compression Not all data needs a whole byte to store How many bits to store letters and numbers? = 36 is less than 7 bits Even if we use upper/lowercase, 6 bits are enough So we could store 4 characters in 24 bits (3 bytes) But how to do it? 14

Compression Data: Result: st (or 5 th, 9 th, and so on) character is easy: shift left by 2 and make it byte 1 int byte1 = char1 << 2; // nd character must be split across bytes 1 and 2 byte1 = byte1 + char2 >> 4; // int byte2 = char2 << 4; //

Compression Data: Result: rd character is split across bytes 2 and 3 byte2 = byte2 + char3 >> 2; // int byte3 = char3 << 6; // The last character is easiest of all, just add it byte3 = byte3 + char4; // Repeat for the next 4 characters until you run out. You’ve saved 25% space at the cost of a little time. 16

Compression Getting the characters back is a similar process 1st (or 4 th, 7 th, and so on) byte holds char1 and part of char2: int char1 = byte1 >>2; int char2 = (byte1 > 2; Moving left 6 bits erases the first character and then moving right 2 bits puts the remainder in the right spot > >

Compression 2nd byte holds the rest of char2 and part of char3: char2 = char2 + (byte2 >>4); int char3 = (byte2 > 2; 3 rd byte holds the rest of char3 and all of char4: char3 = char3 + (byte3 >>6); int char4 = (byte3 > 2; Rinse and repeat until you run out of bytes. 18

ENCRYPTION 19

Encryption Early data in programs was easy to find and read. Simple encryption works similarly to compression Use a key that is between 0 and maxint - maxchar Add the key to data bytes to make them unreadable Subtract the key (if you know it) to make it readable Unfortunately, a computer can quickly try all possible keys So, add a step that rotates the bits as well 20

Encryption Rotation is just a combination of left and right shift int data = 103; // int encrypt_data = (data >3) // This multiplies the possibilities by 16: 8 bit positions X 2 (either before or after adding the key) Still pretty easy for a computer to break but keeps out the casual snoops 21

BIT TEST AND SET 22

Bit Test and Set Many hardware devices are controlled by registers Think of a hardware register as a bank of switches The register is ‘memory mapped’ We control the switches by ‘setting’ the bit to 1 or ‘resetting’ the bit to 0. We can also see what the bits are to see how the device is set This technique also works for software ‘switches’ if we need to save space 23

Bit Test and Set Testing a bit to see if it is set or reset requires a ‘mask’ If our mask has a 1 where we want to test and 0 elsewhere, we can use the & operator to test with Ex: mask = (we want to test the 3 rd bit) _____1_______0__ data & & mask result The other bits in the data don’t matter 24

Bit Test and Set _____1_______0__ data & & mask result A nonzero result means the bit is set (1) A zero result means the bit is reset (0) We can only test one bit at a time If our mask is and we get a non-zero result, which bit (or was it both) was set? However, sometimes we don’t care which one. 25

Bit Test and Set Many headers files will define masks for the possible bit positions bit0mask = 1; // bit1mask = 2; // bit2mask = 4; // and so on… But we can be cleverer: mask = ; Now we can use left shift to test any bit int testbit = 3; // we want to test the third bit result = data & (mask<<(testbit – 1)); 26

Bit Test and Set The convention is usually to number the bits from the right starting at 0 So the third bit would actually be bit 2 and we don’t need the subtraction in the last example. That’s faster and is a big reason the convention was adopted. The shift method of creating a mask is nearly as fast as fetching from memory … sometimes faster. 27

Bit Test and Set Now we can test a bit, but how to change one? The same mask works here as well. If you wish to set the bit, use OR (|) data | (mask << 3); // sets the 4 th bit It doesn’t matter if the bit was already set If you wish to reset the bit, subtract the mask data – (mask << 3); // resets the 4 th bit But it only works correctly if the bit was set, so test first 28

Bit Test and Set Unlike testing, we can set or reset multiple bits at a time mask = 49; // sets/resets bits 0, 4, and 5 Shifting of these masks are generally useless so defining them in a header is the way to go We can define the bit positions and add them to create any mask Remember the masks from slide 28? mask = bit5mask + bit4mask + bit0mask; 29

Bit Test and Set Back on slide 10, mask building with complement was mentioned Good if you want to set/reset all except Mask the bits you wish to except Use the complement function to get actual mask mask = 9; // single out bits 3 and 0 ~mask; // now all but 3 and 0 are masked 30

Bit Test and Set Suppose you want to flip a bit, no matter what it’s current value. Then you simply use the XOR operator. Any mask bit set to 1 flips the data bit. m = 1: d = 1, r = 0 d = 0; r = 1 Any mask bit set to 0 leaves the data bit alone m = 0: d = 1, r = 1 d = 0; r = 0 31

END OF SECTION 32