Bitwise Operators.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
Advertisements

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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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"
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
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?
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.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
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.
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
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.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
CHAPTER 4 CS 3370 – C++ Expressions. Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Number Systems and Bitwise Operation
What to bring: iCard, pens/pencils (They provide the scratch paper)
Enumerations.
Fundamental Data Types
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Data Type.
Introduction to Programming and the C Language
Data Type.
Andy Wang Object Oriented Programming in C++ COP 3330
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
Bits and Bytes Topics Representing information as bits
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Lectures on Numerical Methods
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Fundamental Data Types
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Data Type.
Lecture 2: Bits, Bytes, Ints
Bit Manipulations CS212.
EECE.2160 ECE Application Programming
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Bitwise Operators

Bitwise Operators

Bitwise Complement Two’s Complement int a = 70707; 00000000 00000001 00010100 00110011 ~a 11111111 11111110 11101011 11001100 (-70708) Two’s Complement -n: Take bitwise complement, and then add 1 to it 0: 0x00000000 -1: 0xffffffff

Bitwise Binary Logical Operators

Left and Right Shift Operators expr1 << expr2

Left and Right Shift Operators Right shift operator >> is not symmetric to <<. For unsigned integral expressions, 0s are shifted in at the high end. For signed integral expressions, some machines shift in 0s, while others shift in sign bits. Programmers often use unsigned types when using bitwise operators

Left and Right Shift Operators

Masks A mask a constant or variable that is used to extract desired bits from another variable or expression the constant value 1 00000000 00000000 00000000 00000001 : used to determine the low-order bit of an int expression int i, mask = 1; for ( i=0; i<10; ++i) printf(“%d”, i & mask); the constant value 255 00000000 00000000 00000000 11111111 : a mask for the low-order byte v & 255 A mask for the 3rd bit (v & (1 << 2)) ? 1 : 0

Printing an int Bitwise /* Bit print an int expression */ #include <limits.h> void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* in limits.h */ int mask = 1 << (n – 1); /* mask = 1000…0 */ for (i = 1; i <= n; ++i) { putchar(((a & mask) == 0) ? ‘0’ : ‘1’); a <<= 1; /*  a = a << 1; */ if (i % CHAR_BIT == 0 && i < n) putchar(‘ ‘); } CHAR_BIT: Length of a char variable

Packing and Unpacking [ In file pack_bits.c ] /* Pack 4 characters into an int. */ #include <limits.h> int pack(char a, char b, char c, char d) { int p = a; /* p will be packed with a, b, c, d */ p = (p << CHAR_BIT) | b; p = (p << CHAR_BIT) | c; p = (p << CHAR_BIT) | d; return p; } In main(), printf(“abcd = “); bit_print(pack(‘a’, ‘b’, ‘c’, ‘d’)); putchar(‘\n’); abcd = 01100001 01100010 01100011 01100100

Packing and Unpacking /* Unpack a byte from an int */ #include <limits.h> char unpack(int p, int k) /* k = 0, 1, 2, or 3 */ { int n = k * CHAR_BIT; /* n = 0, 8, 16, or 24 */ unsigned mask = 255; /* low-order byte */ mask <<= n; return ((p & mask) >> n); } The int expression (p & mask) >> n gets converted to a char before it gets passed back to the calling environment. When an int is converted to a char, only the low-order byte is kept; other bytes are discarded.