SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

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

Variables and Data Representation

Machine Arithmetic 8/31/2007

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

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

CS 253. January 14, 2017

JAVA OPERATORS GENERAL

These are notes for the third lecture; if statements and loops.

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Communication Patterns

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Assist. Prof. Dr. Caner ÖZCAN

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

MITOCW watch?v=4dj1oguwtem

Topic Notes: Bits and Bytes and Numbers

The following content is provided under a Creative Commons license. Your support

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Decisions, Decisions. Testing, testing C H A P T E R 7

A practicalintroduction to embedded programming. Brian Plancher 10/17/2018

AGENDA Binary Operations CS 3330 Samira Khan

Applied Computer Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

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

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

(Refer Slide Time: 00:51)

CS 241 Data Organization Binary

The Stack, Free Store, and Global Namespace

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

Bits, Words, and Integers

MITOCW watch?v=zm5mw5nkzjg

Binary, Hexadecimal and Octal number system

COMP 122/L Lecture 2. Kyle Dewey

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

C Language Programming

Taskbar: Working with Several Windows at Once

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Arithmetic-logic units

CSCI 2212: Intermediate Programming / C Chapter 15

CS 4349 Lecture August 21st, 2017

SECTION II: LANGUAGE BASICS

Topic Notes: Bits and Bytes and Numbers

Post Experiment Interview Questions

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

Lecture C7: Assembly programming

Chapter 3: Operators, Expressions and Type Conversion

BEGINNER PHP Table of Contents

MITOCW watch?v=w_-sx4vr53m

CS 64 Week 0 Lecture 1. Kyle Dewey

Binary representation of integer numbers Operations on bits

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Bryan Kreuzberger, Creator of The Breakthrough System Presents. Breakthrough BLUEPRINT

printf( Please enter another number: ); scanf( %d, &num2);

Integer Representation

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

mid=81#15143

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

MA 1128: Lecture 02 1/22/2018

Robert Ragan s TOP 3

Divisibility Rules and Their Explanations

Hey there, I m (name) and today I m gonna talk to you about rate of change and slope.

Lecture 4 CSE July 1992

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

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

Operators. Java operators are classified into three categories:

Introduction to Cryptology Dr. Sugata Gangopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Roorkee

CS Students Linux User's Guide

SPRITES Moving Two At the Same Using Game State

9 Using Equation Networks

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

MAXIMIZING THE UTILITY OF MICROSOFT OUTLOOK. Presented by: Lisa Hendrickson Deborah Savadra

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

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

Chrome if I want to. What that should do, is have my specifications run against four different instances of Chrome, in parallel.

Digital Logic a learn.sparkfun.com tutorial

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

the SAP-2 I. Intro cmpt-150-arc Sections 8-8, 8-9, 9-4, 9-5, 9.6, We ll do this in bits and pieces, doing the beginning of each section first.

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Chapter 1 Getting Started

Chapter 4. Operations on Data

6.001 Notes: Section 1.1

Reverse Engineering For The Not So Backward

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

CSC-105 Exam #1 October 10, 2013

LAB A Translating Data to Binary

PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Lab 2: Conservation of Momentum

Logical and Bitwise Expressions

Introduction to Computing Systems Fall Lab # 3

Subversion was not there a minute ago. Then I went through a couple of menus and eventually it showed up. Why is it there sometimes and sometimes not?

Learn Ninja-Like Spreadsheet Skills with LESSON 9. Math, Step by Step

How to Create a Killer Resources Page (That's Crazy Profitable)

Arithmetic and Bitwise Operations on Binary Data

Heuristic Evaluation of Covalence

Denotational semantics

Transcription:

Simple Programming SIMPLE PROGRAMMING The 10 Minute Guide to Bitwise Operators (Cause you've got 10 minutes until your interview starts and you know you should probably know this, right?) Twitter: Web: @FayeWilliams

Bitwise Operators If you are lucky enough to work on low-level, embedded systems (like me), then eventually, someday, you will probably have to make sense of bitwise manipulation, and when that day comes, you will just LOVE this guide, because it makes them super easy to understand. And even if you think you won t use them, well, you could read this guide anyway, because you never know when a pub quiz is gonna ask about exclusive OR, and like the scouts, it's good to be prepared. Let s get started The bitwise operators enable you to work at a very fine-grained level, which is advantageous when you are dealing with limited space, or sending messages with limited capacity. What the hell does that mean? Ah, sorry about the textbook spiel there, let's start again. Bitwise operators allow us to work at a sub-byte level. You know what a byte is computer memory is made up of lots of them. Well, a byte itself is divided up into 8 bits.

Each one of these teeny, tiny bits has one of two states. It is either ON or OFF. Usually, in computing, a zero represents OFF and a 1 represents ON. No, I don't know why I'm using capitals either. A bit is the smallest piece of storage or data that a computer can utilise. You can't do anything with less than a bit. Now, it turns out that you can t directly access these bits, but they are readable and writeable - if you know how to use the bitwise operators. All clear so far? Great. In order to read and write individual bit values, C provides us with four operators: & Bitwise AND Bitwise OR (inclusive OR) ^ Bitwise XOR (exclusive OR) ~ Bitwise NOT (or one's complement )

What about bit shifting? Yes, smarty-pants, there are also two bit-shift operators: << and >> However, these don't operate on individual bits, they merely move all the bits left or right. If you want to know more, there s a post on my website that provides a practical example of bit shifting. I won t be covering it here. Binary numbers I use binary numbers to illustrate the concepts in this guide. If you need a reminder on how they work, there s a short and sweet explanation here. OK. Enough intro. Let s have a look at each of the bitwise operators in turn. We ll see how they work and talk through an example or two of what on earth you would use them for in real life.

Bitwise AND: & Bitwise AND is represented by the & symbol (ampersand). Example: 128 & 10 = 0 What is this actually doing? Bitwise AND compares each bit setting and if they match, sets the corresponding output bit to 1. So using the example above, we can visualise the actual bit comparisons as follows: 10000000 // 128 & 00001010 // 10 00000000 // 0 What we're doing here is comparing the top bit and the bottom bit. If they are the same, the output is 1. If they are different the output is 0. Crazy? Unintuitive? Glorious? Yes, all of the above.

What would I use bitwise AND for? Good question. The above example seems totally abstract, and quite clearly would make you wonder what use this would ever actually have. To help cement the concepts in your mind I'm going to provide examples that will allow you to see the beauty of these special operators. Seeing them in use will also help you understand their value programmatically. 1. Obtain the value of a bit Let s say I am using a byte to hold the true/false status of 8 items. I want to know if item 5 is true (i.e. set to 1). Now, you can't read bits individually - have I said that already? You can read the contents of a byte, but you can't access individual bits without using the operators. So, since I can't use something like byte[4] to access the 5 th element, instead I'll AND my byte with the appropriate mask and test the result.

Whoa. Mask? What mask? A mask is just a byte that is set to a fixed value. Since you know which bits are set in your mask, you can use it to check the status of an unknown byte. When you AND two bytes together, the value of the mask is returned if the relevant bits match. In this case, my mask is decimal 16 (or, 00010000). See how (reading right to left), I have the bit set to 1 at position 5? This is the value I am checking for: 01101011 // 107 & 00010000 // 16 (Mask) 00000000 // 0 The result is zero, because bit 5 is not set. Let s check a different byte: 01111011 // 123 & 00010000 00010000 // 16 // 16 (Mask) Now the result is 16, because bit 5 is set.

2. Turn off an individual bit You can also use AND in 'reverse', and turn off selected bits using a fixed mask. Let s say that I want to turn off bit 5. I can do this with the mask 239 (11101111). The result is the original input, but with bit 5 switched off: 01111011 // 123 & 11101111 01101011 // 107 // 23 (Mask) Can you see what s happened here? The bitwise AND comparison checks each bit and sets the result to 1 if they match. Therefore, if I want to turn off bit 5, all I need to do is set my mask to contain zero at this position. That way, bit 5 will never be set to 1 in the result. Setting everything else to 1 in the mask ensures that all other bits remain unchanged. Note that you can t turn a bit on with the AND operator, since you must have two values of 1 to get the output of 1 (but see bitwise OR for this).

Bitwise OR: Bitwise OR is represented by the symbol (pipe). Example: 130 10 = 138 What is this actually doing? Bitwise OR compares each bit setting and if either one is set, sets the corresponding output bit to 1. So in the example above, we can see the actual bit comparisons as follows: 10000010 // 130 00001010 // 10 10001010 // 138 Note that it doesn t matter if one or both bits are set in what we are comparing. As long as either one is set, the output bit is set to 1. This is what makes it inclusive. What would I use bitwise OR for? An example follows it s much easier to understand the usefulness of these operators when you see them in action.

1. Turn on an individual bit Remember we used bitwise AND to turn off an individual bit? Well bitwise OR allows you to do the opposite. Let s say we have a byte, containing 8 bits, which are used as true/false values. Without changing the settings of any of the other bits, we want to set the first bit to be true. We apply the mask 1 (decimal) as follows: 11000100 // 196 00000001 // 1 (Mask) 11000101 // 197 The result is that the first bit (that is, the right-most bit) is now set to 1, and the other bit settings remain unchanged. That s all there is to it

Bitwise XOR: ^ Bitwise XOR is represented by the ^ symbol (caret or hat ). Example: 130 ^ 10 = 136 What is this actually doing? Bitwise XOR is an exclusive OR operation it compares each bit setting and if either one is set, sets the corresponding output bit to 1. However, if both bits are set, then the corresponding output bit is set to zero. So to clarify, both OR and XOR compare each bit setting and if either one is set, sets the corresponding output bit to 1. However, if both bits are set, OR sets the output to 1 (inclusive), while XOR sets the output to zero (exclusive). In short, XOR gives a result of 1 only if the two inputs are different.

So the example above has bit comparisons as follows: 10000010 // 130 ^ 00001010 // 10 10001000 // 136 What would I use bitwise XOR for? Let s take a look at how we might use XOR. 1. Toggle bits XOR is primarily used to toggle bits on and off. Let s say we have a byte which represents 8 boolean values and we want to turn the first two off and on again. We can use the same mask to toggle the bits we are interested in: 10000111 // 135 ^ 00000011 // 3 (Mask) 10000100 // 132 Applying the mask turns off the last two bits.

10000100 // 132 ^ 00000011 // 3 (Mask) 10000111 // 135 Applying it again (to the result from above) turns the bits back on. All the other bits remain unchanged. XOR is just like applying a switch very useful in low level programming.

Bitwise NOT ~ Bitwise NOT is represented by the ~ symbol (tilde). Example: ~125 = 130 Bitwise NOT only takes one operand. This means that it is a unary operator. What is this actually doing? Bitwise NOT essentially flips (i.e. changes) the bit setting for each and every bit, resulting in what is known as the one s complement of the original number. So the example above has a bit output as follows: ~ 01111101 // 125 10000010 // 130

What would I use bitwise NOT for? 1. Turn off an individual bit Actually, the NOT operator doesn t itself turn off a bit, but in conjunction with the AND operator, you can use it with a mask to turn off a bit in a nice readable manner. This is best illustrated with a full example: char settings = 0; // 00000000 char FLAG1 = 1; // 00000001 Let s assume that settings is a byte we use to represent 8 boolean values. First of all, we ll set the first bit of our settings variable to 1 using the OR operator and our FLAG1 mask: settings = settings FLAG1; In binary this is: 00000000 // 0 (settings) 00000001 // 1 (FLAG1) 00000001 // 1 (settings)

Now, if we want to turn that bit off again, instead of using AND and defining a new mask (11111110), we can use our existing FLAG1 mask and the NOT operator as follows: settings = settings & ~FLAG1; This reads quite intuitively: settings AND NOT FLAG1 turns off the first bit in settings. In binary: 00000001 // 1 (settings) & 11111110 // 254 (one s complement of FLAG1) 00000000 // 0 (settings - back to our original number) Note that applying the NOT operator to the flag generates the same binary sequence we would need to define as a new mask if we wanted to use AND alone (11111110). The NOT operator saves us a variable (important where you have space constraints). Can t I just use XOR to turn this bit on and off? Well spotted, yes you can

//turn first bit on or off settings = settings ^ FLAG1; The difference with XOR is: 1. You may not know if you are turning the bit on or off without checking the result (or input) as it s just a switch (which means more instructions) 2. XOR always has an effect, whether you intend it to or not. With & ^ (AND NOT) you know you are explicitly turning a bit off, and if the bit is already off when you apply the mask, the result is unchanged. Awesome eh?

Bitwise operator quick start guide In summary, which operator to use when. & Bitwise AND Use with a mask to check if bits are on or off Turn off individual bits Bitwise OR Turn on individual bits ^ Bitwise XOR Toggle bits on and off, like a switch ~ Bitwise NOT Turn off individual bits with AND