Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.

Slides:



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

Bit Manipulation. Binary Numbers Base 10 numbers are represented by sum of digits times powers of 10. For example: 234 = 2* * *10 0 Binary.
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Binary & Decimal numbers = 3* * *10 + 5*1 = 3* * * *10 0 Decimal system: Ten digits: 0,1,2,3,…,9 Example:
Representations Example: Numbers –145 –CVL – –91 –
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
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 –
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.
Binary numbers and arithmetic. ADDITION Addition (decimal)
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
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.
Computer Arithmetic Nizamettin AYDIN
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.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Digital Electronics and Computer Interfacing Tim Mewes 3. Digital Electronics.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
CPU Internal memory I/O interface circuit System bus
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
Operations on Bits Arithmetic Operations Logic Operations
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.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
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=
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
1 CS 151 : Digital Design Chapter 4: Arithmetic Functions and Circuits 4-3 : Binary Subtraction.
Integer Operations Computer Organization and Assembly Language: Module 5.
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
Two’s Complement The language of machines, part II.
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.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Cosc 2150: Computer Organization
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.
Binary & Decimal numbers
From bits to bytes to ints
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
CSE 220 – C Programming Bitwise Operators.
Instructor: David Ferry
Binary numbers and arithmetic
Bit Operations Horton pp
Number Systems and Bitwise Operation
TAO1221 COMPUTER ARCHITECTURE AND ORGANIZATION LAB 6
Introduction to Programming and the C Language
Chapter 14 Bitwise Operators Objectives
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Introduction to Programming
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Bitwise operators.
Module 10 Operations on Bits
Lecture 2: Bits, Bytes, Ints
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Bit Operations Horton pp
Presentation transcript:

Bitwise operators

Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of a sequence of decimal digits (0,1,…,9) = 1x x x x10 0 Least significant digit Most significant digit

Representing integers All numbers within the computer are stored as binary numbers.  Why?  A binary (or base 2) number consists of a sequence of bits (0,1) =1x2 5 +1x2 4 +0x2 3 +0x2 2 +1x2 1 +1x2 0  Where does the word “bit” come from? Least significant bit Most significant bit

Representing integers Typical integer sizes:  unsigned charis 8 bits  unsigned shortis 16 bits  unsigned intis 32 bits is when stored in an unsigned char is when stored in an unsigned short is when stored in an unsigned int We know that is the same as or

BOOLEAN (LOGICAL) OPERATORS

Boolean (logical) operators &&and ||or !not ^xor (exclusive or)

Boolean (logical) operators &&and  F && F is F  F && T is F  T && F is F  T && T is T

Boolean (logical) operators ||or  F || F is F  F || T is T  T || F is T  T || T is T

Boolean (logical) operators !not  !F is T  !T is F

Boolean (logical) operators ^xor  F ^ F is F  F ^ T is T  T ^ F is T  T ^ T is F

BITWISE OPERATORS (ON INTEGERS)

Bitwise operators (on integers) &bitwise and |bitwise or ~bitwise not (1’s complement) ^bitwise xor (exclusive or) -negation (2’s complement) Let’s substitute 1 for T and 0 for F.

Bitwise operators (on integers) &bitwise and (really in an 8-bit byte) &001111(really ) (really )

Bitwise operators (on integers) |bitwise or (really in an 8-bit byte) |001111(really ) (really )

Bitwise operators (on integers) ~bitwise not (1’s complement) If x is (really in an 8-bit byte) then ~x is

Bitwise operators (on integers) -negation (2’s complement)  algorithm: 1.Perform 1’s complement (~). 2.Add 1. If x is (really in an 8-bit byte) then ~x is x is (after add 1 to previous)

Bitwise operators (on integers) ^bitwise xor (really in an 8-bit byte) ^001111(really ) (really )

Other bitwise operators <<shift bits to the left  1 shift to the left is the same as multiplying by 2.  Examples 10 << 1 is 20 7 << 1 is 14 7 << 3 is 56(same as multiplying by 2 3 )

Other bitwise operators >>shift bits to the right  1 shift to the right is the same as integer division by 2.  Examples 10 >> 1 is 5 27 >> 3 is 3

More examples unsigned int ui=0; ui = 10 & 7; ui = 10 | 7; ui = 10 ^ 7 unsigned char uc = ~12;

Bits as binary flags. An int is 32 bits so we can number each student in the class from If the bit for a particular student is 1, then that indicates that they took a quiz. First, let’s define the students.  #define S0 (1<<0)  #define S1 (1<<1)  #define S2 (1<<2)  #define S3 (1<<3)  #define S4 (1<<4) .  #define S31 (1<<31)

Bits as binary flags. Now let’s define a quiz. unsigned int quiz1 = 0; How can we indicate that students 0, 5, and 9 took quiz 1?

Bits as binary flags. Now let’s define a quiz. unsigned int quiz1 = 0; How can we indicate that students 0, 5, and 9 took quiz 1? quiz1 = (s0 | s5 | s9);

Bits as binary flags. Now here comes student 12. He takes the quiz on a subsequent day because he was ill. How do we update quiz1 to indicate that student 12 also took the quiz?

Bits as binary flags. Now here comes student 12. He takes the quiz on a subsequent day because he was ill. How do we update quiz1 to indicate that student 12 also took the quiz? quiz1 |= s12;

Bits as binary flags. I’d like to write a message that indicates whether or not student 25 took the exam? How can I do that?

Bits as binary flags. I’d like to write a message that indicates whether or not student 25 took the exam? How can I do that? if ((quiz1&s25)!=0)puts(“taken”); elseputs(“skipped”); if ((quiz1&s25)==s25)puts(“taken”); elseputs(“skipped”); if (quiz1&s25)puts(“taken”); elseputs(“skipped”);

Bits as binary flags. Did both students 22 and 25 take the exam?

Bits as binary flags. Did both students 22 and 25 take the exam? if ((quiz1&(s22|s25)) == (s22|s25) ) puts(“taken”); else puts(“skipped”); if ((quiz1&s22)!=0 && (quiz1&s25)!=0) …

Bits as binary flags. Did everyone except for student 25 take the exam?

Bits as binary flags. Did everyone except for student 25 take the exam? if ( (quiz1&(~s25)) == (~s25) )puts(“yes”); elseputs(“no”);

Bits as binary flags. I thought student 25 took the exam but I was mistaken. How can I rectify my mistake?

Bits as binary flags. I thought student 25 took the exam but I was mistaken. How can I rectify my mistake? quiz1 = quiz1 & (~s25); quiz1 &= ~s25;

Bits as binary flags. Finally, I’d like to print out a list of all of the students that took exam 1.

Bits as binary flags. Finally, I’d like to print out a list of all of the students that took exam 1. int which = 1; for (int i=0; i<32; i++) { ? which <<= 1; }

Bits as binary flags. I’d like to print out a list of all of the students that took exam 1. int which = 1; for (int i=0; i<32; i++) { if (quiz1&which) printf( “student %d took the quiz. \n”, i ); which <<= 1; }

Bits as binary flags. Say I also have quiz1 and quiz2. I’d like a list of all of the students that took quiz1 or quiz2 but not both.

Bits as binary flags. Say I also have quiz1 and quiz2. I’d like a list of all of the students that took either quiz1 or quiz2 but not both. int which = 1; for (int i=0; i<32; i++) { if ( (quiz1&which) ^ (quiz2&which) ) printf( “student %d took either but not both. \n”, i ); which <<= 1; }