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.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
Assembly Language and Computer Architecture Using C++ and Java
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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
The Binary Number System
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Numbering systems.
Computers Organization & Assembly Language
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?
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
1 Programming in Machine Language SCSC 311 Spring 2011.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
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.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
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.
Logic (continuation) Boolean Logic and Bit Operations.
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.
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.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
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.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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;
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Programming and Data Structure
CSE 220 – C Programming Bitwise Operators.
Chapter 4 Operations on Bits.
Programming in Machine Language
Chap. 2. Types, Operators, and Expressions
EPSII 59:006 Spring 2004.
Bit Operations Horton pp
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Formatting Output.
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Computer Architecture & Operations I
Homework Homework Continue Reading K&R Chapter 2 Questions?
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Manipulations CS212.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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 that bits be manipulated to create addresses for subnets and supernets Two categories of bit operators –Logical bitwise operators –Shift bitwise operators

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 of any integer data type (signed or unsigned char, short, int, and long): Bitwise and & Bitwise inclusive or | Bitwise exclusive or ^ One’s complement ~ & and | are binary operators Require integral operands: integer or char Bit by bit comparison between the two operands Bit operations

and: The and (&) operator takes two operands. A bit in the result is 1 if and only if both of the corresponding bits in the operands are 1. For example: x = y = x&y = or: The or (|) operator also takes two operands. A bit in the result is 1 if and only if either one or both of the corresponding bits in the operands are 1. For example x = y = x|y = Bit operations

Differences between && and & operators: Logical and (&&) Result of && is always 0 or 1 The evaluation of an expression that uses && terminates if the first operand is false (zero) Bitwise and (&) The result of & can be any number Expressions with the & operator are always completely evaluated Bit operations

Differences between || and | operators: Logical or (||) Result of || is always 0 or 1 The evaluation of an expression that uses || terminates if the first operand is true (1) Bitwise or (|) The result of | can be any number Expressions with the | operator are always completely evaluated Bit operations

xor: The xor (^) operator takes two operands. A bit in the result is 1 if and only if exactly one of the corresponding bits in the operand is 1. For example x = y = x^y = Bit operations

The complement operator is unary in that it requires a single operand. The other three are binary and require two operands. complement: The operator complement (~) converts 0 bits to 1 and 1 bits to 0. For example, x = ~x = Bit operations

In addition to the boolean operations, C also provides binary operators that can be used to shift all the bits of any integer type to the left or the right. The left shift operator (<<) is used to shift bits to the left. The left operand is the integer value to be shifted, and the right is the number of bits to shift it. For example, x = x<<4 = A left shift of n bits is a very efficient way to multiply by 2 n Bit operations

The right shift operator (>> )is used to shift bits to the right. The operands are identical to those used in left shift. The left operand is the bit string and the right operand is the number of bits to shift. Bit operations

The right shift operator (>> ) (cont’d) However, an important difference is that the value of new bits shifted in from the left is machine dependent. Nevertheless, it is commonly the case that the new bit will be 0 unless (1) the left operand is a signed integer and (2) the high order bit was originally a 1. In this case the integer value represents a negative number and the shifting in of 1 bits keeps it negative. An example of the right shift: x = x>>4 = A right shift of n bits is a very efficient way to divide by 2 n Bit operations

Used as flags: 0 is off, and 1 is on Use bit mask to set and test flags Mask – variable or constant, usually stored in a byte of short integer, that contains a bit configuration used to control the setting or testing of bits in a bitwise operation Example of a bit mask in a short integer Bitwise Use Bit 0 Bit 15

Creating Masks One-bit mask examples: – mask5 = 1 << – mask1 = 1 << –mask = mask1 | mask | >

Masking is a commonly used technique in processing individual bits of an integer word. In this technique, bits in the mask corresponding to bits of interest in the target word are set to 1 and the others are set to 0. The mask and the target word are then and’ed. Here are some examples of masking (assume x is an integer): if ((x & 0x1) == 1) printf("x is odd\n"); /* Simple test for even/odd */ right2 = x & 0x3; /* Extract right 2 bit values */ bit10 = (x>>10) & 0x1; /* Extract bit 10 value */ righthalf = x & 0xffff; /* Extract right half of integer*/ Masking

Suppose we want to turn off the 5 leftmost bits of a number stored in an 8-bit byte. The mask should start with the five zero bits to turn off the first 5 bits and then contain 3 one bits to leave the last three unchanged. Operator & number xxxxxxxx mask result xxx Creating Masks

Example: We know that in ASCII code, uppercase and lowercase letters differ by only 1 bit in bit 5. The lowercase letters have a 1 in bit 5, the uppercase letters have a 0. It is easy to change a lowercase letter by using an appropriate mask to turn off bit 5. char 'a' mask result 'A' Creating Masks

The bitwise and operator (&) is used to force masked bits in a variable to 0. The bitwise inclusive or operator (|) is used to force masked bits in a variable to 1. The bitwise exclusive or operator (^) is used to force masked bits in a data item to change. Exercise: Find the mask that when used with the & operator sets the second and fourth bits of an 8-bit integer to zero. Exercise: Find the mask that when used with the | operator sets the second and fourth bits of an 8-bit number to 1. Masks

Nibble The term "nibble" is used to describe 4 bits. In particular, a byte (8-bits) is composed of 2 nibbles. A 4-byte (32 bit) integer is composed of 8 nibbles. So we might view an integer as follows: The decimal value " " in binary (base 2) is equal to In memory the value would be stored as illustrated by the following 8 nibbles: Note that nibble 7 has the value , and nibble 5 has the value nibble 0nibble 1 nibble 2nibble 3nibble 4nibble 5nibble 6nibble

In addition to decimal (base 10) and binary (base 2), hexadecimal (base 16) is commonly used to represent values in our field. A single digit in hexadecimal can be represented in 4 bits (a nibble). The binary and hexadecimal values of the decimal values 0-15 are shown in the following table. Also included are the octal (base 8) values: Conversions

Note that the letters "A-F" are used to represent the 6 additional "digits" in the hexadecimal system. Keep in mind that 15 in decimal, and 1111 in binary and F in hexadecimal are ALL the same value -- they are just different ways of representing the same value (we could extend this to roman numerals, i.e. "XV" -- this is just another representation of the same number). In C we can represent a hexadecimal value by prefixing it with a "0x". For example, "x=0xf;" sets "x" to the decimal value 15 (or the binary value 1111). The statement "x=0xff" would set x to the binary value " ", which is 255 in decimal. Hexadecimal

When we do masking, we often find that using the hexadecimal notation is useful (since it corresponds more directly to bits and nibbles). We can ask C to print a value in hexadecimal (as opposed to decimal) by using the "%X" format item, for example if y=12, then printf("y=%d (%X)\n", y, y); would print the value of y in both decimal and hexadecimal, e.g.: y=12 (C) Hexadecimal