Bitwise Operators.

Slides:



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

©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
Logical and Shift operations A particular bit, or set of bits, within the byte is set to 1 or 0 depending on conditions encountered during the execution.
Review Two’s complement
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
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.
CSE20 Lecture 3 Number Systems: Negative Numbers 1.Sign and Magnitude Representation 2.1’s Complement Representation 3.2’s Complement Representation 1.
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.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
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.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Lecture 6 Excess Notation. Excess 8 notation indicates that the value for zero is the bit pattern for 8, that is 1000 Excess 8 notation indicates that.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Bit Manipulation in 'C' 'C' bit operators
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
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.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Rational Expressions relational operators logical operators order of precedence.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
From bits to bytes to ints
CSE 220 – C Programming Expressions.
Chapter 4 Operations on Bits.
Operators and Expressions
Recitation 4&5 and review 1 & 2 & 3
Standard Data Encoding
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Instructor: David Ferry
Computer Architecture & Operations I
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Binary Code  
CSCI206 - Computer Organization & Programming
Bits and Bytes Boolean algebra Expressing in C
Bitwise Operators CS163 Fall 2018.
Topic 6: Bitwise Instructions
CS-401 Computer Architecture & Assembly Language Programming
Objective evaluate powers that have negative and zero exponents.
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Bits and Bytes Topics Representing information as bits
Bit-wise and bit-shift operators
Homework Homework Continue Reading K&R Chapter 2 Questions?
CS 206D Computer Organization
EECE.2160 ECE Application Programming
Number Representation & Operators
Lecture 2: Bits, Bytes, Ints
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
EECE.2160 ECE Application Programming
Presentation transcript:

Bitwise Operators

Number Representation Recap Humans think about numbers in decimal Computers think about numbers in binary Base conversion to go between Hex is more human-readable than binary All information on a computer is in binary Nice because big difference between “high” and “low” Binary encoding can represent anything! Program needs to know how to interpret bits

Operators Recap This will flip all bits in the operand NOT: ~ This will flip all bits in the operand AND: & This will perform a bitwise AND on every pair of bits OR: | This will perform a bitwise OR on every pair of bits XOR: ^ This will perform a bitwise XOR on every pair of bits SHIFT: <<, >> This will shift the bits right or left logical vs. arithmetic

Operators Recap Evaluates the entire operand, rather than each bit NOT: ! Evaluates the entire operand, rather than each bit Produces a 1 if == 0, produces 0 if nonzero AND: && Produces 1 if both operands are nonzero OR: || Produces 1 if either operand is nonzero

Lab 1 Worksheet in class Tips: Work on 8-bit versions first, then scale your solution to work with 32-bit inputs Save intermediate results in variables for clarity Shifting by more than 31 bits is UNDEFINED. This will NOT yield 0

Examples Create 0xFFFFFFFF using only one operator Limited to constants from 0x00 to 0xFF Naïve approach: 0xFF + (0xFF << 8) + (0xFF << 16) … Better approach: ~0x00 = 0xFFFFFFFF

Examples Replace the leftmost byte of a 32-bit integer with 0xAB Let our integer be x First, we want to create a mask for the lower 24 bits ~(0xFF << 24) will do that using just two operators (x & mask) will zero out the leftmost 8 bits Now, we want to OR in 0xAB to those zeroed-out bits Final result: (x & mask) | (0xAB << 24) Total operators: 5