BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

Similar documents
Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

COMP 122/L Lecture 2. Kyle Dewey

JAVA OPERATORS GENERAL

Arithmetic Operations

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

A flow chart is a graphical or symbolic representation of a process.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter 4. Operations on Data

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

CS 253. January 14, 2017

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Unit 2: Accentuate the Negative Name:

COUNTING AND CONVERTING

Section 26: Associativity and Order of Operations

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

ECE 30 Introduction to Computer Engineering

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

PROBLEM OF ERRORS IN CALCULATION : A NEW CONCEPT OF DECIMAL

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Chapter 10 Binary Arithmetics

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

CS Bootcamp Boolean Logic Autumn 2015 A B A B T T T T F F F T F F F F T T T T F T F T T F F F

Assist. Prof. Dr. Caner ÖZCAN

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

9 Using Equation Networks

Operators in C. Staff Incharge: S.Sasirekha

SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

Bits, Words, and Integers

Chapter 2. Data Representation in Computer Systems

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Multiplying and Dividing by Powers of 10

Applied Computer Programming

Lecture 13 Bit Operations

Signed Multiplication Multiply the positives Negate result if signs of operand are different

4.1 QUANTIZATION NOISE

CS33 Project Gear Up. Data

add and subtract whole numbers with more than 4 digits, including using formal written methods (columnar addition and subtraction)

Arithmetic and Bitwise Operations on Binary Data

Module 2: Computer Arithmetic

Chapter 3: Arithmetic for Computers

Operators. Java operators are classified into three categories:

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Floating Point Arithmetic

SCRATCH MODULE 3: NUMBER CONVERSIONS

Logic, Words, and Integers

2.1 Transforming Linear Functions

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

ENE 334 Microprocessors

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

Bitwise Instructions

MA 1128: Lecture 02 1/22/2018

Machine Arithmetic 8/31/2007

Chapter 10 - Computer Arithmetic

Rational Number is a number that can be written as a quotient of two integers. DECIMALS are special fractions whose denominators are powers of 10.

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Expressions and Precedence. Last updated 12/10/18

ECEN 468 Advanced Logic Design

PIETRO, GIORGIO & MAX ROUNDING ESTIMATING, FACTOR TREES & STANDARD FORM

CSCI2467: Systems Programming Concepts

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CT 229. Java Syntax 26/09/2006 CT229

Unit 3. Operators. School of Science and Technology INTRODUCTION

Project 3: RPN Calculator

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Evaluating Expressions Using the Order of Operations

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

1 Computer arithmetic with unsigned integers

ECE232: Hardware Organization and Design

CS 64 Week 1 Lecture 1. Kyle Dewey

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

ME 261: Numerical Analysis. ME 261: Numerical Analysis

CSCI 2212: Intermediate Programming / C Chapter 15

Experimental Methods I

2 Computation with Floating-Point Numbers

CHAPTER 2 SENSITIVITY OF LINEAR SYSTEMS; EFFECTS OF ROUNDOFF ERRORS

Numerical computing. How computers store real numbers and the problems that result

Number System. Introduction. Decimal Numbers

Topic Notes: Bits and Bytes and Numbers

Topic 2: Decimals. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Register Transfer Language and Microoperations (Part 2)

CS 241 Data Organization Binary

2.Simplification & Approximation

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

CS/COE 0447 Example Problems for Exam 2 Spring 2011

Mathematical Data Operators

Number Systems. Both numbers are positive

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Transcription:

BITWISE OPERATORS There are a number of ways to manipulate binary values. Just as you can with decimal numbers, you can perform standard mathematical operations - addition, subtraction, multiplication, division - on binary values (which we ll cover on the next page). You can also manipulate individual bits of a binary value using bitwise operators. Bitwise operators perform functions bit-by-bit on either one or two full binary numbers. They make use of logic operating on a group of binary symbols. These bitwise operators are widely used throughout both electronics and programming. Complement (NOT) The complement of a binary value is like finding the exact opposite of everything about it. The complement function looks at a number and turns every 1 into a 0 and every 0 becomes a 1. The complement operator is also called NOT.

For example to find the complement of 10110101: NOT 10110101 (decimal 181) 01001010 (decimal 74) NOT is the only bitwise operator which only operates on a single binary value. OR OR takes two numbers and produces the union of them. Here s the process to OR two binary numbers together: line up each number so the bits match up, then compare each of their bits that share a position. For each bit comparison, if either or both bits are 1, the value of the result at that bit-position is 1. If both values have a 0 at that position, the result also gets a 0 at that position. The four possible OR combinations and their outcome are: 0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1

For example to find the 10011010 OR 01000110, line up each of the numbers bitby-bit. If either or both numbers has a 1 in a column, the result value has a 1 there too: 10011010 OR 01000110 11011110 Think of the OR operation as binary addition, without a carry-over. 0 plus 0 is 0, but 1 plus anything will be 1. AND AND takes two numbers and produces the conjunction of them. AND will only produce a 1 if both of the values it s operating on are also 1. The process of AND'ing two binary values together is similar to that of OR. Line up each number so the bits match up, and then compare each of their bits that share a position.

For each bit comparison, if either or both bits are 0, the value of the result at that bit-position is 0. If both values have a 1 at that position, the result also gets a 1 at that position. The four possible AND combinations and their outcome are: 0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1 For example, to find the value of 10011010 AND 01000110, start by lining up each value. The result of each bit-position will only be 1 if both bits in that column are also 1. 10011010 AND 01000110 00000010 Think of AND as kind of like multiplication. Whenever you multiply by 0 the result will also be 0.

XOR XOR is the exclusive OR. XOR behaves like regular OR, except it ll only produce a 1 if either one or the other numbers has a 1 in that bit-position. The four possible XOR combinations and their outcome are: 0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0 For example, to find the result of 10011010 XOR 01000110: 10011010 XOR 01000110 11011100 Notice the 2 nd bit, a 0 resulting from two 1 s XOR ed together.

Bit shifts Bit shifts aren t necessarily a bitwise operator like those listed above, but they are a handy tool in manipulating a single binary value. There are two components to a bit shift - the direction and the amount of bits to shift. You can shift a number either to the left or right, and you can shift by one bit or many bits. When shifting to the right, one or more of the least-significant bits (on the rightside of the number) just get cut off, shifted into the infinite nothing. Leading zeros can be added to keep the bit-length the same. For example, shifting 10011010 to the right two bits: RIGHT-SHIFT-2 10011010 (decimal 154) 00100110 (decimal 38) Shifting to the left adds pushes all of the bits toward the most-significant side (the left-side) of the number. For each shift, a zero is added in the least-significant-bit position.

For example, shifting 10011010 to the left one bit: LEFT-SHIFT-1 10011010 (decimal 154) 100110100 (decimal 308) That simple bit shift actually performs a relatively complicated mathematical function. Shifts to the left n bits multiplies a number by 2 n (see how the last example multiplied the input by two?), while a shift n bits to the right will do an integer divide by 2 n. Shifting to the right to divide can get weird - any fractions produced by the shift division will be chopped off, which is why 154 shifted right twice equals 38 instead of 154/4=38.5. Bit shifts can be a powerfully fast way to divide or multiply by 2, 4, 8, etc. These bitwise operators provide us most of the tools necessary to do standard mathematical operations on binary numbers. Source: https://learn.sparkfun.com/tutorials/binary