Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,

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

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Chapter 2 Data Types, Declarations, and Displays
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Fourth Edition.
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.
Professor Jennifer Rexford COS 217
Number Systems.
Simple Data Type Representation and conversion of numbers
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
Number Systems What is the Standard Base we
Number System. Number Systems Important Number systems – Decimal – Binary – Hexadecimal.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
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.
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
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.
Number Representation (Part 1) Computer Architecture (Fall 2006)
CEC 220 Digital Circuit Design Number Systems & Conversions Friday, January 9 CEC 220 Digital Circuit Design Slide 1 of 16.
NUMBER SYSTEMS.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Data Storage © 2007 Pearson Addison-Wesley. All rights reserved.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Number Representation and Arithmetic Circuits
CEC 220 Digital Circuit Design Number Systems & Conversions Wednesday, Aug 26 CEC 220 Digital Circuit Design Slide 1 of 16.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
Digital Electronics Principles & Applications Fifth Edition Chapter 2 Numbers We Use in Digital Electronics ©1999 Glencoe/McGraw-Hill Roger L. Tokheim.
CHAPTER 3 BINARY NUMBER SYSTEM. Computers are electronic machines which operate using binary logic. These devices use two different values to represent.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
N 3-1 Data Types  Binary information is stored in memory or processor registers  Registers contain either data or control information l Data are numbers.
DECIMAL, BINARY, AND HEXADECIMAL. Decimal Numbering System Ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Represent larger numbers as a sequence of digits.
Topic: Binary Encoding – Part 2
Topic: Binary Encoding – Part 1
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Programming Fundamentals
Structure, Unions, typedef and enumeration
ECE Application Programming
TMF1414 Introduction to Programming
EPSII 59:006 Spring 2004.
Bit Operations Horton pp
Number Systems and Bitwise Operation
What to bring: iCard, pens/pencils (They provide the scratch paper)
C Structures, Unions, Bit Manipulations and Enumerations
Data Representation Data Types Complements Fixed Point Representation
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
Digital Electronics Ms. Deepa Mehta.
Manipulators CSCE 121 J. Michael Moore
Computer Architecture & Operations I
There are 10 types of people of people in this world…
Chapter 1 Number System RGGP, Narwana.
Homework Homework Continue Reading K&R Chapter 2 Questions?
C Structures, Unions, Bit Manipulations and Enumerations
Bitwise Operators.
CS334: Number Systems Lab 1.
Module 10 Operations on Bits
Remember the 10 types of people of people in this world…
Computer Science 1 Review and finish Number base conversion
Networks & I/O Devices.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Bit Manipulation when every bit counts

Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal, octal numbers? why are they used? what are the following numbers in decimal 012, 0b111, 0xA1, 23 ? Convert 11 decimal into binary, octal, hexadecimal. l What is bitwise OR, bitwise AND, bitwise exclusive OR, bitwise complement, left and right shift? how are they denoted and how are they computed? what is sizeof ? l What is called a mask and how is it used? l what are bit fields? 2

Why Bit Manipulation l extremely efficient and compact way of data storage and processing l a way of low-level interaction with peripheral devices: mice, disks, scanners, etc. l memory and compute cycles are cheap, bit manipulating programs are cryptic. Do not use unless you have to 3

Bits l Bit - elementary unit of information storage n can store either 1 or 0 l Electronic circuits are most robust and dependable if they have at most two stable states n computer data is stored and processed as sequences of bits l more complex data types are encoded in bit sequences n How are signed/unsigned integers are represented? Floating point data? Characters? l base for numeric representation can be binary, decimal, octal or hexadecimal octal constant has leading 0 : 012 == 10 hexadecimal has leading 0x : 0x12 == 18 binary has leading 0b: 0b11 == 3 l octal and hexadecimal simplify conversion to binary base n octal digit represents three binary digits 012 means (binary) n hexadecimal – four binary digits 0x12 means (binary) 4

Conversion with Decimal Base l to decimal 0651 = 6*8^2 + 5*8^1 + 1*8^0 = 425 0x1B6 = 1*16^2 + 11*16^1 + 6*16^0 = 438 0b10101 = 1*2^4 + 1*2^2 + 1*2^0 = 21 l from decimal 425 %8 438 %16 21 % x1B b

Input/Output in Bases l a number may be input or printed in decimal (default) octal or hexadecimal using stream manipulators dec - decimal oct - octal hex – hexadecimal l to use – input/output manipulator to stream cin >> oct >> myNumber; // inputs in octal cout << hex << myNumber; // outputs in hexadecimal l remember the same number can be input/output using any of the bases 6

Bitwise Operations l treat data as a sequence of bits rather than complex data type unsigned (int) – good data type to use l operations & - bitwise and: 1 only if both operands are & | - bitwise or: 1 if at least one operand is 1 ^ - bitwise exclusive or: 1 only if exactly one operand is 1 ~ - bitwise complement: single operand, 1 s to 0, 0 to 1 << - left shift: shifts bits to left (by specified number of positions), right bits are filled with 0 -oes: if variable a is 0010 then (a << 2) is 1000 –this operation looks familiar, what is it? >> - right shift: shifts bits to right, left bits are filled with 0 -oes compound assignment is allowed for all operations: a &=0x001 ; useful function: sizeof( type or class ) 7

Bit Fields l can specify fewer bits in integer member variables of structures and classes l example class card { private: unsigned face : 4; // 4 bits; 0-15 unsigned suit : 2; // 2 bits; 0-3 unsigned color : 1; // 1 bit; 0-1 }; 8