24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.

Slides:



Advertisements
Similar presentations
INSTRUCTION SET ARCHITECTURES
Advertisements

Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
The Binary Numbering Systems
Lecture - 2 Number systems and computer data formats
Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Chapter 4 Operations on Bits
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
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
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Arithmetic for Computers
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CS 270: Computer Organization Bits, Bytes, and Integers
'C' Bitwise Operators Relevance to 'C' of bitwise applications Syntax and expressions Example getter and setter functions Eratothene's prime number sieve.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
CSCI 136 Lab 1: 135 Review.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
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.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Number Representation and Arithmetic Circuits
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.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
Chapter 7 Bit Manipulation. 7.1 Logical 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.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Bit Manipulation in 'C' 'C' bit operators
Microprocessor & Assembly Language
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.
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.
Chapter 4 Operations on Bits.
Instructor: David Ferry
Chapter 3 Bit Operations
Data Structures Mohammed Thajeel To the second year students
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
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.
CS-401 Computer Architecture & Assembly Language Programming
Bits and Bytes Topics Representing information as bits
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Bits and Bytes Topics Representing information as bits
Bit-wise and bit-shift operators
Homework Homework Continue Reading K&R Chapter 2 Questions?
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Number Representation & Operators
Comp Org & Assembly Lang
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

24/06/2015CSE1303 Part B lecture notes 2 Last time  Floating point  Anatomy of floating point  sign  exponent  mantissa  Floating point arithmetic  multiplication  addition  Limitations of floating point

24/06/2015CSE1303 Part B lecture notes 3 In this lecture  Working with large data  byte order  Working with small data  bitwise operations AND, OR, NOT, XORAND, OR, NOT, XOR maskingmasking  shifting relationship to multiplication and divisionrelationship to multiplication and division

24/06/2015CSE1303 Part B lecture notes 4 Byte order  Problem  memory is addressed by bytes (8 bits)  how to store a 32-bit value in memory? 0x13579BCF 32-bit value (written in hex for compact- ness, but really in binary) addresses ? 8 bits

24/06/2015CSE1303 Part B lecture notes 5 Byte order  Solution  break up value into byte-sized chunks  store each chunk in successive bytes 0x13579BCF each hex digit represents four bits, so two hex digits to a byte addresses one byte here next byte here another byte here final byte here 8 bits

24/06/2015CSE1303 Part B lecture notes 6 Byte order  Which order to store the bytes?  starting at the most significant (“big”) end? addresses x13579BCF 0x13 0x57 0x9B 0xCF work this way

24/06/2015CSE1303 Part B lecture notes 7 Byte order  Which order to store the bytes?  starting at the least significant (“little”) end? addresses x13579BCF 0x13 0x57 0x9B 0xCF work this way

24/06/2015CSE1303 Part B lecture notes 8 Byte order  Two common ways of storing word into memory  Big Endian  store most significant byte in lowest address  store least significant byte in highest address  Little Endian  store least significant byte in lowest address  store most significant byte in highest address

24/06/2015CSE1303 Part B lecture notes 9 Byte order  Which byte order is used depends on the computer’s Central Processing Unit (CPU)  Big Endian Motorola 680x0 (Amiga/Atari ST/Mac classic)Motorola 680x0 (Amiga/Atari ST/Mac classic) IBM/Motorola PowerPC (current Macintosh)IBM/Motorola PowerPC (current Macintosh) MIPS (SGI Indy/Nintendo 64)MIPS (SGI Indy/Nintendo 64) Motorola 680X familyMotorola 680X family  Little Endian Intel 80x86/Pentium (IBM PC compatible)Intel 80x86/Pentium (IBM PC compatible) Rockwell 6502 (Commodore 64)Rockwell 6502 (Commodore 64) MIPS (Sony Playstation/Digital DECstation)MIPS (Sony Playstation/Digital DECstation)  Some processors (e.g., MIPS) can be configured to be either Big Endian or Little Endian

24/06/2015CSE1303 Part B lecture notes 10 Logic operations  Boolean logic operations  operate on one or two binary values (true/false, 1/0)  logical AND (on two values) C operator: &&C operator: && result is true if both operands are true, else falseresult is true if both operands are true, else false  logical OR (on two values) C operator: ||C operator: || result is true if either operand is true, else falseresult is true if either operand is true, else false  logical NOT (on one value) C operator: !C operator: ! invert operand: true  falseinvert operand: true  false

24/06/2015CSE1303 Part B lecture notes 11 Bitwise operations  Bitwise logic operations  operate on each bit of one or two values  bitwise AND (on two values) for each bit: result is 1 if both operands are 1, else 0for each bit: result is 1 if both operands are 1, else 0  bitwise OR (on two values) for each bit: result is 1 if either operand is 1, else 0for each bit: result is 1 if either operand is 1, else 0  bitwise Exclusive OR (“XOR”) (on two values) for each bit: result is 1 if either operand (but not both) is 1, else 0for each bit: result is 1 if either operand (but not both) is 1, else 0  bitwise NOT (on one value) for each bit: invert (or “flip”) the bit: 1  0for each bit: invert (or “flip”) the bit: 1  0 like flip in “flip_and_add_1” negation of signed valuelike flip in “flip_and_add_1” negation of signed value

24/06/2015CSE1303 Part B lecture notes 12 Bitwise operations: NOT  Bitwise NOT  Invert every bit in a value each bit inverted independently of the others truth table in out 01 10

24/06/2015CSE1303 Part B lecture notes 13 Bitwise operations: AND  Bitwise AND  performs logical AND on corresponding bits of two values result is 1 if both corresponding input bits are 1, 0 otherwise truth table in 1 in 2 out

24/06/2015CSE1303 Part B lecture notes 14 Bitwise operations: OR  Bitwise OR  performs logical OR on corresponding bits of two values result is 1 if either corresponding input bit (or both) are 1, 0 otherwise truth table in 1 in 2 out

24/06/2015CSE1303 Part B lecture notes 15 Bitwise operations: XOR  Bitwise Exclusive OR (XOR)  performs logical Exclusive OR (XOR) on corresponding bits of two values result is 1 if either corresponding input bit (but not both) are 1, 0 otherwise truth table in 1 in 2 out

24/06/2015CSE1303 Part B lecture notes 16 Bitwise operations  Some operations don’t affect bits  y AND 1 = y  y OR 0 = y  y XOR 0 = y  Some operations force bits to 0 or 1  y AND 0 = 0  y OR 1 = 1  Some operations change bits  y XOR 1 = not_y

24/06/2015CSE1303 Part B lecture notes 17 Extracting bits  Sometimes need to isolate one or more bits in a value  e.g., find sign in signed int / float extract this bit to find out if value is positive or negative but need to ignore all the other bits

24/06/2015CSE1303 Part B lecture notes 18 Extracting bits  Use bitwise AND to extract bits  for desired bits, AND with 1 0 AND 1  0, 1 AND 1  10 AND 1  0, 1 AND 1  1 bit remains unchangedbit remains unchanged  for other bits, AND with 0 0 AND 0  0, 1 AND 0  00 AND 0  0, 1 AND 0  0 bit is cleared (set to 0)bit is cleared (set to 0)  only desired bits remain, others are cleared

24/06/2015CSE1303 Part B lecture notes 19 Extracting bits  Example: extract sign bit of C float sign bit AND a 1 in this column allows the sign bit through zeroes in these columns stop any value from getting through resulting value is zero: number is positive

24/06/2015CSE1303 Part B lecture notes 20 Extracting bits  Example: extract sign bit of C float AND a value like this which is used to extract, set or clear individual bits is usually called a mask

24/06/2015CSE1303 Part B lecture notes 21 Clearing bits  Sometimes need to clear a bit to zero  e.g., clear sign bit of a float, to make positive (like fabs function)  Use bitwise AND to clear bits  for bits to clear, AND with 0 0 AND 0  0, 1 AND 0  00 AND 0  0, 1 AND 0  0 bit is cleared (set to 0)bit is cleared (set to 0)  for other bits, AND with 1 0 AND 1  0, 1 AND 1  10 AND 1  0, 1 AND 1  1 bit is unchangedbit is unchanged  desired bits are cleared to zero, the other remain unchanged

24/06/2015CSE1303 Part B lecture notes 22 Clearing bits  Example: set sign bit of C float to 0 sign bit AND a 0 in this column forces the sign bit to zero ones in these columns allow the value to show through resulting number is positive: 0 in MSB

24/06/2015CSE1303 Part B lecture notes 23 Setting bits  Sometimes need to set a bit to one  e.g., set sign bit of a float, to make negative  Use bitwise OR to set bits  for bits to set, OR with 1 0 OR 1  1, 1 OR 1  10 OR 1  1, 1 OR 1  1 bit is set (to 1)bit is set (to 1)  for other bits, OR with 0 0 OR 0  0, 1 OR 0  10 OR 0  0, 1 OR 0  1 bit is unchangedbit is unchanged  desired bits are set to one, others remain unchanged

24/06/2015CSE1303 Part B lecture notes 24 Setting bits  Example: set sign bit of C float to 1 sign bit OR a 1 in this column forces the sign bit to one zeroes in these columns allow the value to show through resulting number is negative: 1 in MSB

24/06/2015CSE1303 Part B lecture notes 25 Toggling bits  Sometimes need to flip a bit’s value  e.g., toggle sign bit to negate a float  Use bitwise XOR (exclusive OR) to toggle bits  for bits to toggle, XOR with 1 0 XOR 1  1, 1 XOR 1  00 XOR 1  1, 1 XOR 1  0 bit is flipped (0  1)bit is flipped (0  1)  for other bits, XOR with 0 0 XOR 0  0, 1 XOR 0  10 XOR 0  0, 1 XOR 0  1 bit is unchangedbit is unchanged  desired bits are flipped, others remain unchanged

24/06/2015CSE1303 Part B lecture notes 26 Toggling bits  Example: negate a C float sign bit XOR a 1 in this column causes the sign bit to flip zeroes in these columns cause the original bit to remain the same resulting number has opposite sign bit

24/06/2015CSE1303 Part B lecture notes 27 Bitwise operations in C  bitwise AND (on two values)  C operator: & (ampersand)  bitwise OR (on two values)  C operator: | (vertical bar)  bitwise Exclusive OR (XOR) (on two values)  C operator: ^ (caret)  bitwise NOT (on one value)  C operator: ~ (tilde)

24/06/2015CSE1303 Part B lecture notes 28 Bitwise operations in C /* Examples of bitwise operations. */ int main() { /* 16-bit value, binary */ /* 16-bit value, binary */ unsigned short value = 0x035E; unsigned short value = 0x035E; /* 16-bit mask, binary */ /* 16-bit mask, binary */ unsigned short mask = 0x00FF; unsigned short mask = 0x00FF; /* %04X printf format prints as 4 hex digits*/ /* %04X printf format prints as 4 hex digits*/ printf("%04X\n", value ); /* 035E */ printf("%04X\n", value ); /* 035E */ printf("%04X\n", mask ); /* 00FF */ printf("%04X\n", mask ); /* 00FF */ printf("%04X\n", ~value ); /* FCA1 */ printf("%04X\n", ~value ); /* FCA1 */ printf("%04X\n", value & mask ); /* 005E */ printf("%04X\n", value & mask ); /* 005E */ printf("%04X\n", value | mask ); /* 03FF */ printf("%04X\n", value | mask ); /* 03FF */ printf("%04X\n", value ^ mask ); /* 03A1 */ printf("%04X\n", value ^ mask ); /* 03A1 */ return 0; return 0;}

24/06/2015CSE1303 Part B lecture notes 29 Shifting  Example: extracting exponent from a C float AND value mask if you use a mask to get the exponent you get this: really want this: really want this: shift right 23 bits

24/06/2015CSE1303 Part B lecture notes 30 Shifting  Shifting is the moving of patterns of bits left or right within a word  left or right  0 ≤ amount to shift < word size example: shifting left by three bits fill with zeroes these bits are shifted “off the end” and are discarded

24/06/2015CSE1303 Part B lecture notes 31 Shifting in C  Binary (two value) operators  Shift left operator: <<  Shift right operator: >>  Value to shift goes on left of the operator  Amount to shift goes on the right of the operator

24/06/2015CSE1303 Part B lecture notes 32 Shifting in C /* Example using C shift operators. */ int main() { /* 16-bit value, binary */ /* 16-bit value, binary */ unsigned short value = 0x8F02; unsigned short value = 0x8F02; int i = 4; /* used as a shift amount */ int i = 4; /* used as a shift amount */ /* Shift right or left by 4 bits. */ /* Shift right or left by 4 bits. */ printf("%04X\n", value >> 4 ); /* 08F0 */ printf("%04X\n", value >> 4 ); /* 08F0 */ printf("%04X\n", value << 4 ); /* F020 */ printf("%04X\n", value << 4 ); /* F020 */ /* Either side can be variable or literal. */ /* Either side can be variable or literal. */ printf("%04X\n", value >> i ); /* 08F0 */ printf("%04X\n", value >> i ); /* 08F0 */ /* Shifting by 1 harder to see in hex; /* Shifting by 1 harder to see in hex; write these in binary to see what’s happening. */ write these in binary to see what’s happening. */ printf("%04X\n", value >> 1 ); /* 4781 */ printf("%04X\n", value >> 1 ); /* 4781 */ printf("%04X\n", value << 1 ); /* 1E04 */ printf("%04X\n", value << 1 ); /* 1E04 */ return 0; return 0;}

24/06/2015CSE1303 Part B lecture notes 33 Shifting and multiplication  8-bit unsigned binary shift left 1 x2 ×2 ×2

24/06/2015CSE1303 Part B lecture notes 34 Shifting and division  8-bit unsigned binary shift right 1 ÷2 ÷ 2

24/06/2015CSE1303 Part B lecture notes 35 Shifting  Shifting left a value by n places is equivalent to multiplying by 2 n  << 1  × 2; << 2  × 4; << 3  × 8; etc.  Shifting right a value by n places is equivalent to dividing by 2 n  >> 1  ÷ 2; etc.  Shifting is quicker than multiplication/division  for efficiency, compiler will replace multiplication or division by constant power of two with a shift left or shift right

24/06/2015CSE1303 Part B lecture notes 36 Shifting signed values  8-bit signed (two’s complement) shift right 3 ÷8 MSB = 0: positive fill with 0

24/06/2015CSE1303 Part B lecture notes 37 Shifting signed values  8-bit signed (two’s complement) – shift right 3 ÷8 MSB = 1: negative fill with 0  got , wanted –12 10 = : differs in first three bits

24/06/2015CSE1303 Part B lecture notes 38 Shifting signed values  8-bit signed (two’s complement) –96 10 –12 10 shift right 3 ÷8 MSB = 1: negative fill with copies of original MSB by filling with copies of the MSB, positive numbers stay positive, and negative numbers stay negative

24/06/2015CSE1303 Part B lecture notes 39 Shifting  Need to distinguish two kinds of right shift  logical: always fill left bits with 0  arithmetic: fill left bits with copies of MSB  C >> operator is logical for unsigned values, unknown for signed values gcc uses arithmetic right shift on signed valuesgcc uses arithmetic right shift on signed values  Not a problem with left shift  logical left shift same as arithmetic left shift  signed: overflow occurred if MSB changes  unsigned: overflow if a 1 bit “exits stage left”

24/06/2015CSE1303 Part B lecture notes 40 Covered in this lecture  Byte order  bitwise operations  AND, OR, NOT, XOR  masking  shifting  relationship to multiplication and division  right shifts arithmetic shift rightarithmetic shift right logical shift rightlogical shift right

24/06/2015CSE1303 Part B lecture notes 41 Going further  Boolean algebra  the mathematics of binary values  Digital logic  hardware for doing AND, OR,...  Bit fields  another way of referring to bits in C

24/06/2015CSE1303 Part B lecture notes 42 Next time  Pointers  Aggregation of data  arrays  structs  Characters  ASCII  Strings Reading: Lecture notes section B06