Bitwise Operators Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
INSTRUCTION SET ARCHITECTURES
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Instruction Set Architecture & Design
C Language Programming. C has gradually replaced assembly language in many embedded applications. Data types –C has five basic data types: void, char,
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Chapter 2 Data Types, Declarations, and Displays
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"
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
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.
Computer Organization and Assembly Language Bitwise Operators.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
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.
Agenda ASCII char N int Character class char N String Bitwise operators homework.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
CS 3843 Computer Organization Prof. Qi Tian Fall 2013
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
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
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.
Microprocessor & Assembly Language
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
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;
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
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.
From bits to bytes to ints
CSE 220 – C Programming Expressions.
CSE 220 – C Programming Bitwise Operators.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Instructor: David Ferry
Bit Operations Horton pp
Chapter 3 Bit Operations
Microcomputer Programming
Object Oriented Programming COP3330 / CGS5409
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 14 Bitwise Operators Objectives
Bits and Bytes Boolean algebra Expressing in C
Introduction to Programming
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.
Bitwise Operators CS163 Fall 2018.
CS-401 Computer Architecture & Assembly Language Programming
Bit Fields & Bitwise Operations
Shift & Rotate Instructions)
Bit-wise and bit-shift operators
Homework Homework Continue Reading K&R Chapter 2 Questions?
Bitwise operators.
Lecture 2: Bits, Bytes, Ints
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bitwise Operators Fall 2008 Dr. David A. Gaitros

Bitwise Operators “Mathematics may be the language of scientists, but binary is the language of Computer Scientists” Dr. David A. Gaitros

Binary Operations Recall the following: – A bit is the smallest digit capable of being stored in a modern day digital computer. – A byte, consisting of 8 bits is the smallest unit of storage that a computer can work with. – A word ( usually somewhere between 32 and 64 bits) is the smallest addressable item in a computer. The word size of a computer is usually dictated by either the bus size of the system or the word size of the CPU.

Bitwise Operators OperatorNameDescription & bitwise AND1 only if both operands are 1. |bitwise OR1 if ether or both operands are 1 ^bitwise exclusive or1 if either but not both operands are 1 <<Left ShiftShifts bits of the first operand by the number of bits specified by the second operand. >>Right ShiftShifts bits of the first operand by the number of bits specified by the second operand. ~ComplementFlips the bits in the operand. All 1’s become 0’s and all 0’s become 1’s.

Bitwise Operators Bitwise operators are used to directly manipulate the bits of integral operands such as char, short, int, long (both signed and unsined). Normally unsigned integers are used when dealing with bitwise operations.

Bitwise Operations Code Example: #include using namespace std; int main(void) { unsigned x=10; unsigned y=0; y = x | y; return 0; } // What does this do? // Nothing really.

Bitwise Operators Shortcuts X &= y; same as x = x & y; x |= y; same as x = x | y; x ^= y; same as x = x ^ y; x << y; same as x = x << y; x >> y; same as x = x>> y;

Bitwise Operations Why use them? – A single bit shift left is a very fast way of integer multiplication by 2. – A single bit shift right is a very fact way of integer division by 2. – An AND operation with all zeros is a very efficient way to clear out a field. – We can use single bits in a word (32 bits) to represent Boolean values very efficiently. These are sometimes call flags.

Bitwise Operators short x = 6891; short y = 11318; Internal Representation x: y:

Bitwise Operators // Logical AND operator x: y:

Bitwise Operators // Logical OR operator x: y:

Bitwise Operators // Logical XOR // (exclusive OR) x: y:

Bitwise Operators // Shift Left 2 x: // Shift right 4 y:

Bitwise Operators // And here is the // complement of x (~x) x: ~x:

Bitwise Operators // So.. How do I set // a bit? void SetBit(int num) { int mask=1<<num; flags = flags | mask; }