Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Introduction to C Programming CE Lecture 8 Bitwise Operations.
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.
Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.
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.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
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"
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
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.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University.
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 in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
Arithmetic Operations
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
Floating Point Numbers
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
Operator Kinds of Operator Precedence of Operator Type Casting.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Chapter 13 Introduction to C++ Language
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Binary numbers and arithmetic
What to bring: iCard, pens/pencils (They provide the scratch paper)
Enumerations.
Introduction to Programming and the C Language
Lecture 5 from (Chapter 4, pages 73 to 96)
Chapter 1 C for Embedded Systems.
Negative Integer Representation
Chapter 1 C for Embedded Systems.
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Enumerations.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Introduction to Programming
Bit Fields & Bitwise Operations
CISC/CMPE320 - Prof. McLeod
By C. Shing ITEC Dept Radford University
By C. Shing ITEC Dept Radford University
Comp Org & Assembly Lang
Bitwise operators.
Module 10 Operations on Bits
Bitwise Operators.
Bit Manipulations CS212.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University

Slide 2 Objectives Understand how to use bit operators Understand how to use mask Understand how to pack and unpack characters

Slide 3 Bit Operators OperatorExplain (bitwise) ~complement &And |Or ^Exclusive or <<Left shift >>Right shift

Slide 4 Bit Operators Use unsigned type involving > Signed type that shifts sign bit is system dependent

Slide 5 Example: unsigned int a=23, b=33; char c=‘C’;

Slide 6 Example: Expres sion Result a b ~a a&b a|b a^b

Slide 7 Example: (Cont.) Expres sion Result c c<< c>>

Slide 8 Mask Constant or variable to extract desired bits Bit mask: extract 1 bit Byte mask: extract 1 byte

Slide 9 Bit Mask Example: int i, j, n, mask=1; n=…; for (i=0;n >= j && i<32;++i) { printf(“%d”,n&mask?1:0); j*=2; // use j to get rid of leading 0’s in binary rep. mask <<=1; }

Slide 10 Byte Mask Example: int i, mask=255; int n; n=…; for (i=0;i<4;++i) { printf(“%c”,n&mask?’1’:’0’); mask <<=8; }

Slide 11 Binary Representation Example: find binary representation offind binary representation of a positive integer

Slide 12 Data Compression pack: Save space Save time: process 32 bits at a time instead of 1 character unpack

Slide 13 Data Compression – pack Example: int pack4char (char char1, char char2, char char3, char char4) { int a=char1; return (((a <<8)|char2)<<8|char3)<<8|char4; }

Slide 14 Data Compression – unpack Example: int i, m; m=…; for (i=0; i<4;++i) unpackint(m, i); int unpackint (int m, int n) { int i=8*n; unsigned mask=255; mask <<=i; return (m&mask)>>i; }

Slide 15 Practice Given unsigned a=1, b=2; Find the values of the following table: Expression Value ________ _____ a >1 a<<1+2 <<3 a+b >b

Slide 16 Practice (Answer) Given unsigned a=1, b=2; Find the values of the following table: Expression Value ________ _____ a >1 2 a<<1+2 <<3 64 a+b >b 3072

Slide 17 References Deitel & Deitel: C How to Program, 4th ed., Chapter 10, Prentice Hall