Bit Manipulation in 'C' 'C' bit operators

Slides:



Advertisements
Similar presentations
Chapter 4 Register Transfer and Microoperations
Advertisements

Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.
©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.
Arithmetic & Logic Unit Does the calculations Everything else in the computer is there to service this unit Handles integers May handle floating point.
Programming the ATmega16
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
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.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
Binary Arithmetic Stephen Boyd March 14, Two's Complement Most significant bit represents sign. 0 = positive 1 = negative Positive numbers behave.
MICRO OPERATIONS Department of Computer Engineering, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
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.
REGISTER TRANSFER LANGUAGE MICROOPERATIONS. TODAY OUTLINES Logic Microoperations Shift Microoperations.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Micro Operation. MICROOPERATIONS Computer system microoperations are of four types: - Register transfer microoperations - Arithmetic microoperations -
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.
MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
1 Arithmetic and Logic Operations Patt and Patel Ch. 2 & 3.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Arithmetic Operations
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
CSC321 Homework Due Due Tuesday after spring break Turn in –Design information State diagram State table K-map simplifications and expressions for flip-flop.
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
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.
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Binary Logic (review) Basic logical operators:(Chapter 7 expanded) NOT AND – outputs 1 only if both inputs are 1 OR – outputs 1 if at lest one input is.
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
Negative numbers: Week 10 Lesson 1
Chapter 4 Operations on Bits.
COMPUTER ARCHITECTURE & OPERATIONS I
CSCI206 - Computer Organization & Programming
Bit Operations Horton pp
Overview Register Transfer Language Register Transfer
Logic Bitwise Instructions
MISP Assembly.
Chapter 14 Bitwise Operators Objectives
REGISTER TRANSFER LANGUAGE
Computer Architecture & Operations I
The University of Adelaide, School of Computer Science
Computer Organization and Design
Bitwise Operators CS163 Fall 2018.
Shift & Rotate Instructions)
Shift & Rotate Instructions)
The University of Adelaide, School of Computer Science
Homework Homework Continue Reading K&R Chapter 2 Questions?
CS 206D Computer Organization
Bitwise Operators.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
MIPS Arithmetic and Logic Instructions
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bit Manipulation in 'C' 'C' bit operators OP symbol Description Example & AND y = y & 0xF7; | OR y = y | 0x08; ^ XOR >> n shift bits right n places y = y >> 4; << n shift bits left n places y = y << 2; ~ complements (invert all bits) y = ~y; Only use with integer data types - Not real types. Note: In 'C' binary operators(such as y = y OP z;) can be written as in a shortened form: y OP= z; E.g.'s y = y + 7; y+=7; y = y & 0x80; y &= 0x80; y = y >> 4; y >>= 4;

Masking for bit manipulation Masking uses AND and OR operators Use OR ('|') to set bits to '1' Use AND ('&') to set bits to 0 Use XOR(^) to toggle bits (0 -> 1 and 1-> 0) OR A B Output 1 AND A B Output 1 XOR A B Output 1

Output Masking Setting bits to 1 using the OR (|) bit-wise operator General format: value = value | mask; Shorthand : value |= mask; Mask bits : '1' to set a bit, '0' to leave bit unchanged. Setting bits to 0 using the AND (&) bit-wise operator General format: value = value & mask; Shorthand : value &= mask; Mask bits : '0' to clear a bit, '1' to leave bit unchanged Example: Controlling a single bit (Note VAR is an 8-bit integer data type.) To set bit 3 to '1' the mask is 00001000 VAR= VAR | 0x08; To set bit 3 to '0' the mask is 11110111 VAR = VAR & 0xf7;

More examples For 32 bit integer data types: FIO2PIN |= 0x00040000; // ? FIO2PIN &= 0xFFFBFFFF; FIO2PIN |= ~0x00040000; Changing several bits in one operation FIO2PIN |= 0x00FF0000; FIO2PIN |= 0x00001010; PIO2PIN &= ~ 0x00001010; Toggling bits using XOR (^) FIO2PIN ^= 0x00000080 FIO2PIN |= 0x00040000; // set bit 18 FIO2PIN &= 0xFFFBFFFF; // clear bit 18 FIO2PIN |= ~0x00040000; // clear bit 18 Changing multiple bits FIO2PIN |= 0x00FF0000; // set bits 16 thro' 23 FIO2PIN |= 0x00001010; //set bits 4 and 12 PIO2PIN &= ~ 0x00001010; //clear bits 4 and 12 Toggling bits using XOR (^) FIO2PIN ^= 0x00000080; //toggle bit 7

Input Masking When a single bit need to be tested Use AND (&) and a mask to isolate the selected bit value & mask Used in input Polling to test an individual input bit Example: Testing bit 3 if mask is 00001000 (0x08) Two possible results 00000000 or 00001000 when ANDED I.e. zero or non-zero Loop while bit is '0', exit when bit becomes 1 while( (VAR & 0x08) == 0) { ; } Loop while bit is '1', exit when bit becomes 0 while( (VAR & 0x08) != 0) { ; }

More examples if ((FIO2PIN & 0x00000080) == 0) { FIO2PIN |= 0x00000001; } if (FIO2PIN & 0x00000004) != 0) FIO2PIN &= ~0x00000008; if(FIO2PIN & 0x000000F0) == 0x000000F0) FIO2PIN ^= 0x00000002; If bit 7 is 0 then set bit 0 if bit 2 is 1 then clear bit 3 if bits 4 thro' 7 are ones then toggle bit 1

Shift Operations << shift left >> shift right used to move bits to the left - lose MSB and gain new LSB (new LSB = 0 for all data types) multiplies by 2 for each shift. >> shift right used to move bits to the right - lose LSB and gain new MSB (new MSB = 0 for unsigned integer data types, or previous MSB for signed types). divides by 2 for each shift.

Shift operations FIO2PIN |= (1<<7); // set bit 7 00000000000000000000000000000001 : start 00000000000000000000000000000010 : 1 00000000000000000000000000000100 : 2 00000000000000000000000000001000 : 3 00000000000000000000000000010000 : 4 00000000000000000000000000100000 : 5 00000000000000000000000001000000 : 6 00000000000000000000000001000000 : 7 FIO2PIN &= ~(1<<23); // clear bit 23

Exercise A32 bit register is interfaced to the following:- A motor connected to bit 7 (0 is off, 1 is on) An led connected to bit 0 (0 is off, 1 is on) Two switches S1 and S2 connected to bits 1 and 2 respectively that generate either logic 0 or 1 inputs. Write C code to perform the following steps: Loop until switch S1 is logic 1 Now turn on the led then wait until both switches are at logic 1 Now turn on the motor Loop until either of th eswitches is change to logic 0 Turn of the motor and the led