ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CSC141- Introduction to Computer Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
Bit Field.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Introduction to C Programming CE Lecture 8 Bitwise Operations.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
 2006 Pearson Education, Inc. All rights reserved Bits, Characters, C-Strings and struct s.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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.
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
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
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Topic: Binary Encoding – Part 2
C Formatted Input/Output
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Chap. 2. Types, Operators, and Expressions
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Number Systems and Bitwise Operation
C Structures, Unions, Bit Manipulations and Enumerations
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
MISP Assembly.
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
C Operators, Operands, Expressions & Statements
Bit Fields & Bitwise Operations
MIPS Assembly.
C Structures, Unions, Bit Manipulations and Enumerations
Bitwise Operators.
MIPS Assembly.
Lecture 2: Bits, Bytes, Ints
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To

Bitwise Operators All data is represented internally as sequences of bits – Each bit can be either 0 or 1 – Sequence of 8 bits forms a byte

3 Fig | Bitwise operators.

4 Outline fig10_07.c (1 of 2 )

5 Outline fig10_07.c (2 of 2 ) displayMask is a 1 followed by 31 zeros Bitwise AND returns nonzero if the leftmost bits of displayMask and value are both 1, since all other bits in displayMask are 0s.

6 Common Programming Error Using the logical AND operator ( && ) for the bitwise AND operator ( & ) and vice versa is an error.

7 Fig | Results of combining two bits with the bitwise AND operator &.

8 Fig | Results of combining two bits with the bitwise inclusive OR operator |.

9 Fig | Results of combining two bits with the bitwise exclusive OR operator ^.

10 Outline fig10_09.c (1 of 3 ) Bitwise AND sets each bit in the result to 1 if the corresponding bits in the operands are both 1

11 Outline fig10_09.c (2 of 3 ) Bitwise inclusive OR sets each bit in the result to 1 if at least one of the corresponding bits in the operands is 1 Bitwise exclusive OR sets each bit in the result to 1 if only one of the corresponding bits in the operands is 1 Complement operator sets each bit in the result to 0 if the corresponding bit in the operand is 1 and vice versa

12 Outline fig10_09.c (3 of 3 )

13 Outline fig10_10.c

14 Common Programming Error Using the logical OR operator ( || ) for the bitwise OR operator ( | ) and vice versa is an error.

15 Outline fig10_13.c (1 of 3 ) Left shift operator shifts all bits left a specified number of spaces, filling in zeros for the empty bits

16 Outline fig10_13.c (2 of 3 ) Right shift operator shifts all bits right a specified number of spaces, filling in the empty bits in an implementation-defined manner

17 Outline fig10_13.c (3 of 3 )

18 Common Programming Error The result of shifting a value is undefined if the right operand is negative or if the right operand is larger than the number of bits in which the left operand is stored.

19 Portability Tip 10.7 Right shifting is machine dependent. Right shifting a signed integer fills the vacated bits with 0 s on some machines and with 1 s on others.

20 Fig | The bitwise assignment operators.

21 Fig | Operator precedence and associativity. (Part 1 of 2.)

22 Fig | Operator precedence and associativity. (Part 2 of 2.)

Bit Fields Bit field – Member of a structure whose size (in bits) has been specified – Enable better memory utilization – Must be defined as int or unsigned – Cannot access individual bits Defining bit fields – Follow unsigned or int member with a colon ( : ) and an integer constant representing the width of the field – Example: – struct BitCard { – unsigned face : 4; – unsigned suit : 2; – unsigned color : 1; – };

Bit Fields Unnamed bit field – Field used as padding in the structure – Nothing may be stored in the bits – struct Example { – unsigned a : 13; – unsigned : 3; – unsigned b : 4; – } – Unnamed bit field with zero width aligns next bit field to a new storage unit boundary

25 Outline fig10_16.c (1 of 2 ) Bit fields determine how much memory each member of a structure can take up

26 Outline fig10_16.c (2 of 2 )

27 Outline