Bit Manipulation. Binary Numbers Base 10 numbers are represented by sum of digits times powers of 10. For example: 234 = 2*10 2 +3*10 1 +4*10 0 Binary.

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 3 GETTING THE MOST OUT OF C.
Advertisements

OBJECTIVES You should be able to:  Convert binary fractions to a decimal  Convert decimal fractions to binary.
Types and Arithmetic Operators
DATA REPRESENTATION CONVERSION.
©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.
Converting Numbers Between Bases. While the quotient is not zero…  Divide the decimal number by the new base.  Make the remainder the next digit to.
Binary Addition Rules Adding Binary Numbers = = 1
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.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.
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.
Binary Operations Math/Logical. Binary Math Decimal Addition Example ) Add = 15 Write down 5, carry ) Add 3 +
Binary Multiplication. Any multiplication can be re-expressed as a series of additions. For example, 7 * 3 (seven times three) is simply the sum of 3.
They are the same as registers since they store binary numbers. Called shifting registers since they shift (left or right) the binary number stored in.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
Calculating with Significant Figures
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
WARM UP Scientific Notation GOAL Read and write numbers in scientific notation. KEY WORDS Scientific Notation.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2: The Logic of Compound Statements 2.5 Application: Number Systems and Circuits for Addition 1 Counting in binary is just like counting in decimal.
Comparing and ordering rational numbers
Introduction to Numerical Analysis I MATH/CMPSC 455 Binary Numbers.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Section 2.6 Representation of numbers. Decimal representation (base 10) Given a positive integer X, the decimal representation of X is a string of digits.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bit-String Flicking.
Set A formal collection of objects, which can be anything May be finite or infinite Can be defined by: – Listing the elements – Drawing a picture – Writing.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Introduction To Number Systems Binary System M. AL-Towaileb1.
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
digit x 1 digit 3 DIGIT BY 2 DIGIT Multiplying Decimals 4 digit by 3 digit
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Bit Manipulation in 'C' 'C' bit operators
Binary & Normalization What is Normalization? We discussed this the other day (special review session slides, near the end) Can someone tell us.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Mealy Machines Finite State Machines with Outputs given on the transitions.
♣Multiplying Decimals♣
Multiplication
Chapter 4 Operations on Bits.
BINARY CODE.
Multiplication
Multiplying by powers of 10 Decimals
University of Gujrat Department of Computer Science
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Binary – Octal - Hexadecimal
Formatting Output.
Introduction to Programming and the C Language
Review Operation Bingo
Chapter 14 Bitwise Operators Objectives
Bitwise Operators CS163 Fall 2018.
Multiplying Decimals 3-6 & 3-7.
Boolean Logic Boolean Logic is considered to be the basic of digital electronics. We know that a computer’s most basic operation is based on digital electronics.
Divisibility Rules.
Homework Homework Continue Reading K&R Chapter 2 Questions?
Integers & Absolute Value
Decimals: Multiplying by 2.5
Decimal / Binary Conversions
Bitwise operators.
Sliding Decimal Points
Converting from Base-n to Base-10
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Standard Form: Multiplying powers of 10
Converting Numbers Between Bases
Presentation transcript:

Bit Manipulation

Binary Numbers Base 10 numbers are represented by sum of digits times powers of 10. For example: 234 = 2* * *10 0 Binary numbers are similar, except that the only digits are 1 and 0, and the digits multiply powers of 2. The string "1011" is binary for the decimal number = 1*2 3 +0*2 2 +1*2 1 +1*2 0

Bit Operators The "and" operator: (10 & 3) = 2 Why? It's clearer expressed in binary numbers: 1010 Apply the rules for and based on each & 0011 row independently. Here 1 = True, and 0 = False. Thus, 1 & is True and True = True = 1 The "or" operator: (10 & 3) = 11 Why? Again, clearer in binary 1010 = 10 | 0011 = = 11

Bit Operators The "xor" or "exclusive or" operator: (10 ^ 3) = 9 Why? 1010 ^

Flags Consider a class Monster that has an integer field named traits. Traits will record capabilities of the Monster in individual bits. Bits can be defined with left shift, e.g. final static int HAS_FLYING = 1<<0; final static int HAS_INVISIBILITY = 1<<1; final static int CAN_SWIM = 1<<2; final static int FIRE_PROOF = 1<<3;

Flags To turn a flag on: m.traits |= CAN_SWIM; To turn a flag off: m.traits &= ~CAN_SWIM; To toggle a flag: m.traits ^= CAN_SWIM; The tilde operator "~" inverts the bits on an integer.