Introduction to C Programming CE00312-1 Lecture 8 Bitwise Operations.

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

8085 processor. Bus system in microprocessor.
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
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.
Binary Arithmetic Stephen Boyd March 14, Two's Complement Most significant bit represents sign. 0 = positive 1 = negative Positive numbers behave.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
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 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 29, 2004 Lecture Number: 26.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
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
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
MIPS ALU. Building from the adder to ALU ALU – Arithmetic Logic Unit, does the major calculations in the computer, including – Add – And – Or – Sub –
Arithmetic Operations
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
CDA 3101 Spring 2016 Introduction to Computer Organization
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
Chapter 3: Dataflow Modeling Digital System Designs and Practices Using Verilog HDL and 2008~2010, John Wiley 3-1 Chapter 3: Dataflow Modeling.
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Memory unit terminology memory contains series of bits, grouped into addressable units word - unit of memory access and/or size of data registers, 16/32/64.
Computer Engineering page 1 Integer arithmetic Depends what you mean by “integer”. Assume at 3-bit string. –Then we define: zero = 000 one = 001 Use zero,
Operator Kinds of Operator Precedence of Operator Type Casting.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Chap. 2. Types, Operators, and Expressions
Morgan Kaufmann Publishers
Bit Operations Horton pp
Number Systems and Bitwise Operation
Chapter 3: Dataflow Modeling
MISP Assembly.
Chapter 14 Bitwise Operators Objectives
Computer Architecture & Operations I
Introduction to Programming
Module 10 Operations on Bits
Bitwise Operators.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Introduction to C Programming CE Lecture 8 Bitwise Operations

Bitwise Operations - Introduction To date – logical operators AND&&(two ampersands) e.g. if ( x < y && y < z ) printf("x is less than z\n"); OR||(two vertical lines) e.g. if ( x < z || y < z ) printf(“x or y is less than z\n"); Now – manipulating individual bits.

Bitwise Complement The “~” operator Ones complement Inverts the individual bits in a number e.g ~ ~7-8 or ~7248 Depending upon number implementation

Two’s Complement ~number + 1 gives the “Twos Complement” of number. e.g.Twos complement of ~ ~ or 249 ???? Signed values careful when using bitwise operators!

Subtraction Easy to implement addition hardware. Subtraction hardware? Using two’s complement. can use adders to subtract. a – bis the same as a + (-b) -b acquired by taking one’s complement of b and adding 1 a + (~b + 1)

Bitwise Logical Operators & Bitwise AND |Bitwise INCLUSIVE OR ^Bitwise EXCLUSIVE OR These take two integer operands and manipulate them bit by bit - do not confuse them with && and ||

Bitwise Shift Operators Left Shift<< Usage expr1 << expr2; E.g.c = ‘Z’; c << 4; Right Shift>> Usage expr1 >> expr2;

Left Shifting = = 14 = 28 = 112 = 56 = 224 = 192 ! << 1

Shift Operators – Warning Operation of Left and Right are similar. They are not symmetrical Bits can be lost when shifting. Use on unsigned types Why? Value of expr2 should… not be negative not exceed number of bits in expr1

Precedence and Associativity If a=1; and b=2; What are the results of…… a > 1; a << 1+2 << 3; a + b > b;

Precedence and Associativity If a=1; and b=2; What are the results of…… a > 1;(a > 1; a << 1+2 << 3;(a << (1+2)) << 3 a + b > b; ((a + b) > b As with any operator – always check precedence and associativity.

Masks Masks are variables, or constants, that can be used to extract desired bits from a second variable. E.g – to check the state of the least significant bit int x = 7;int mask = 1; printf(“%d”, x & mask); consider (x & (1 << 2)) ? 1 : 0

Bit Fields Allows packing of data into a structure. Able to define the number of bits used for a variable Especially useful when… Memory or data storage at a premium Reading from non standard file formats – e.g 9 bit words C allows this within a structure by adding a bit length specifier after the variable.