Bitwise Structures Chapter 9: C programming Language ספטמבר 04

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

CSC141- Introduction to Computer Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
7-5 Microoperation An elementary operations performed on data stored in registers or in memory. Transfer Arithmetic Logic: perform bit manipulation on.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 3 GETTING THE MOST OUT OF C.
The Linux Kernel: Memory Management
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions.
Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 5: Pointers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 4: Arrays and Strings.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 8: Recursion.
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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 7: Structures.
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.
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.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
1-1 NET+OS Software Group Flash API Multiple flash memory bank support New Flash API introduction Detailed Flash API Function presentation Supporting.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 9 – September 18, 2001.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
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.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
10/12/2015 Updated from Alex’s excellent note
Bit Manipulation. Get Bit from the particular Position Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int.
Data Manipulation, part two Introduction to computer, 2 nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Lecture 10 union, enumeration, bit
Chapter 4 Operations on Bits.
Boolean Algebra, Bitwise Operations
Bit Operations Horton pp
Overview Register Transfer Language Register Transfer
Bitwise Hashing.
Object Oriented Programming COP3330 / CGS5409
Introduction to Programming and the C Language
Introduction to Programming
Chapter 14 Bitwise Operators Objectives
Introduction to Programming
Embedded Programming in C
CS-401 Computer Architecture & Assembly Language Programming
Bitwise operators.
Module 10 Operations on Bits
Bitwise Operators.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bitwise Structures Chapter 9: C programming Language ספטמבר 04 Copyright Meir Kalech

What is Bitwise Structure? The smallest type is of 8 bits (char). Sometimes we need only a single bit. For instance, storing the status of the lights in 8 rooms: We need to define an array of at least 8 chars. If the light of room 3 is turned on the value of the third char is 1, otherwise 0. Total array of 64 bits. EXPENSIVE in place and time!!! ספטמבר 04 Copyright Meir Kalech

What is Bitwise Structure? It is better to define only 8 bits since a bit can also store the values 0 or 1. But the problem is that there is no C type which is 1 bit long (char is the longer with 1 byte). Solution: define a char (8 bits) but refer to each bit separately. Bitwise operators, introduced by the C language, provide one of its more powerful tools for using and manipulating memory. They give the language the real power of a “low-level language”. ספטמבר 04 Copyright Meir Kalech

What is Bitwise Structure? Accessing bits directly is fast and efficient, especially if you are writing a real-time application. A single bit cannot be accessed directly, since it has no address of its own. The language introduces the bitwise operators, which help in manipulating a single bit of a byte. bitwise operators may be used on integral types only (unsigned types are preferable). ספטמבר 04 Copyright Meir Kalech

Bitwise Operators & bitwise AND | bitwise OR ^ bitwise XOR ~ 1’s compliment << Shift left >> Shift right All these operators can be suffixed with = For instance a &= b; is the same as a = a & b; ספטמבר 04 Copyright Meir Kalech

Bitwise Operators – truth table 1 ספטמבר 04 Copyright Meir Kalech

Bitwise Operators - Examples 11010011 & 10001100 ------------ 10000000 11010011 | 10001100 ------------ 11011111 11010011 ^ 10001100 ------------ 01011111 ~11010011 ------------ 00101100 11010011>>3 ------------ 00011010 11010011<<3 ------------ 10011000 ספטמבר 04 Copyright Meir Kalech

Setting Bits How can we set a bit on or off? Manipulations on bits are enabled by mask and bitwise operators. Bitwise OR of anything with 1 results in 1. Bitwise AND of anything with 0 results in 0. ספטמבר 04 Copyright Meir Kalech

Setting Bits For instance, how can we turn on the light in room #3? lights: 00000000 char lights = 0x0; char mask = 0x1; mask <<= 2; lights |= mask; mask: 00000001 mask: 00000100 lights: 00000100 ספטמבר 04 Copyright Meir Kalech

Setting Bits For instance, how can we turn off the light in room #3? lights: 00100111 char lights = 0x27; char mask = 0xfb; lights &= mask; mask: 11111011 lights: 00100011 ספטמבר 04 Copyright Meir Kalech

Getting Bits How can we know if a bit is on or off? Manipulations on bits are enabled by mask and bitwise operators. Bitwise AND of anything with 1 results in the same value. ספטמבר 04 Copyright Meir Kalech

Getting Bits For instance, how can we check if the light in room #3 is turned on or off? lights: 00100111 char lights = 0x27; char mask = 0x1; mask <<= 2; if(lights & mask) puts(“turned on”); else puts(“turned off”); mask: 00000001 mask: 00000100 lights & mask: 00000100 ספטמבר 04 Copyright Meir Kalech

Bitwise - Example Suppose we have 8 rooms: Light is on in the rooms requested by the user. Print which rooms are lighted. void main() { unsigned char lights = 0; set_lights(&lights); print_lights(lights); } ספטמבר 04 Copyright Meir Kalech

Bitwise - Example void set_lights(unsigned char *lights) { int j, answer; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) answer=0; printf(“lights in room #%d?”, j+1); scanf(“%d”, &answer); if(answer) *lights |= mask; } lights mask 1 answer: 1 (yes) *lights |= mask 1 ספטמבר 04 Copyright Meir Kalech

Bitwise - Example void set_lights(unsigned char *lights) { int j, answer; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) answer=0; printf(“lights in room #%d?”, j+1); scanf(“%d”, &answer); if(answer) *lights |= mask; } lights 1 mask 1 answer: 0 (no) *lights |= mask 1 ספטמבר 04 Copyright Meir Kalech

Bitwise - Example void set_lights(unsigned char *lights) { int j, answer; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) answer=0; printf(“lights in room #%d?”, j+1); scanf(“%d”, &answer); if(answer) *lights |= mask; } lights 1 mask 1 answer: 1 (yes) *lights |= mask 1 ספטמבר 04 Copyright Meir Kalech

Bitwise - Example void set_lights(unsigned char *lights) { int j, answer; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) answer=0; printf(“lights in room #%d?”, j+1); scanf(“%d”, &answer); if(answer) *lights |= mask; } lights 1 mask 1 answer: 1 (yes) *lights |= mask 1 ספטמבר 04 Copyright Meir Kalech

Bitwise - Example room #1 is lighted void print_lights(unsigned char lights) { int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) if(lights & mask) printf(“room #%d is lighted”,j+1); else printf(“room #%d is NOT lighted”,j+1); } lights 1 mask 1 lights & mask 1 Output: room #1 is lighted ספטמבר 04 Copyright Meir Kalech

Bitwise - Example room #2 is NOT lighted void print_lights(unsigned char lights) { int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) if(lights & mask) printf(“room #%d is lighted”,j+1); else printf(“room #%d is NOT lighted”,j+1); } lights 1 mask 1 lights & mask Output: room #2 is NOT lighted ספטמבר 04 Copyright Meir Kalech

Bitwise - Example room #3 is lighted void print_lights(unsigned char lights) { int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) if(lights & mask) printf(“room #%d is lighted”, j+1); else printf(“room #%d is NOT lighted”, j+1); } lights 1 mask 1 lights & mask 1 Output: room #3 is lighted ספטמבר 04 Copyright Meir Kalech

Bitwise - Example room #4 is lighted void print_lights(unsigned char lights) { int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) if(lights & mask) printf(“room #%d is lit”, j+1); else printf(“room #%d is NOT lit”, j+1); } lights 1 mask 1 lights & mask 1 Output: room #4 is lighted ספטמבר 04 Copyright Meir Kalech