CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.

Slides:



Advertisements
Similar presentations
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Advertisements

Assembly Language and Computer Architecture Using C++ and Java
Review Two’s complement
Assembly Language and Computer Architecture Using C++ and Java
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
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.
MATH 224 – Discrete Mathematics
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?
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Binary Numbers. Why Binary? Maximal distinction among values  minimal corruption from noise Imagine taking the same physical attribute of a circuit,
Islamic University Of Gaza, Nael Aburas Data Storage Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas
June 10, 2002© Howard Huang1 Number systems To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
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.
CSC 3210 Computer Organization and Programming
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
From bits to bytes to ints
Unit 1 Logical operators.
Morgan Kaufmann Publishers
CprE 185: Intro to Problem Solving (using C)
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Lesson Objectives Aims
Exponents Scientific Notation
Data Representation in Computer Systems
Variables and Primative Types
Bit Operations Horton pp
Number Systems and Bitwise Operation
Fundamentals & Ethics of Information Systems IS 201
University of Gujrat Department of Computer Science
Bit-Level Discussion of Numeric Types
Topic 3: Data Binary Arithmetic.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Number Systems Decimal (base 10) { }
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
CS-401 Computer Architecture & Assembly Language Programming
COMS 161 Introduction to Computing
Bits and Bytes Topics Representing information as bits
Coding Concepts (Data- Types)
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Unit 10 Arithmetic-logic Units
Binary Numbers.
Homework Homework Continue Reading K&R Chapter 2 Questions?
EECE.2160 ECE Application Programming
Number Representation & Operators
Comp Org & Assembly Lang
CMSC250 Fall 2018 Circuits 1 1.
Comp Org & Assembly Lang
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
EECE.2160 ECE Application Programming
Bit Manipulations CS212.
Combination Logic & FPGAs.
Bit Operations Horton pp
Presentation transcript:

CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression

Operators – Bitwise Operations Bitwise operations are operations that are performed at the binary bit level on a value. The logic for these operations comes from the function of their corresponding logic gate in electronics and circuitry. You'll be working with these more in CS341. In programming, these elementary operations are often helpful for solving problems at the higher level, so they're included into the language. 1010 1100

Operators – Bitwise AND & The Bitwise AND (&) operator takes two numeric operands and gives a single numeric in response. char flags = flags & 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if both bits from the operands are 1. Otherwise, the bit for that column in the result is 0. It doesn't matter which order the operands are in (commutativity). However, generally, it's said that the second operand is "turning off flags" in the first operand. 1010 1100 & 0000 1111 = 0000 1100

Operators – Bitwise OR | The Bitwise OR (|) operator takes two numeric operands and gives a single numeric in response. char flags = flags | 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if at least one bit from the operands are 1. Otherwise, the bit for that column in the result is 0. The Bitwise OR is also commutative. It's said that the second operand is "turning on flags" in the first operand. 1010 1100 | 0000 1111 = 1010 1111

Operators – Bitwise XOR ^ The Bitwise XOR or Exclusive OR (^) operator takes two numeric operands and gives a single numeric in response. This is NOT the Exponent Operator and NOT how you take bases to powers! char flags = flags ^ 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if exactly one bit from the operands are 1. You can also think of it as the 1 bits from the second operand "flipping" the bits of the first operand. (Note: 0 does not flip anything) This operation is also commutative. 1010 1100 ^ 0000 1111 = 1010 0011

Operators – Bitwise NOT ~ / One's Comp. We've actually already covered Bitwise NOT (~) before as a concept, rather than an operator. It's the One's Complement! char flags = ~flags; It's a unary operator which operates on numeric values. For each bit in the operand, the resultant value has that bit flipped. This is mathematically equivalent to (–x – 1) ~ 0101 1100 = 1010 0011

Bitwise – Truth Tables Truth Tables can be used to describe a bitwise operation column-by-column. Go column-by-column through both operands' binary representations and use the table to determine the bit value for that column in the result. AND 1 OR 1 1 1 1 1 1 1 Operands XOR 1 NOT Results 1 1 1 1 1

Bitwise – Hexwise Any bitwise operation can be done equivalently on other power-of-two representations of the numbers. We can do this for hexadecimal by grouping the binary digits into fours and replacing them with their hexadecimal digits. Note, since the bitwise operations are always the same for the groups of four bits, they'll be the same for the hex digits. 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111 7F 0111 1111 & A0 & 1010 0000 = 20 = 0010 0000

Bitwise – Table for Hexadecimal AND F E D … C OR F E D … These tables would get really huge, just get familiar with doing these operations in groups of 4 bits at a time. As an exercise on your own time, try making the full tables for AND and OR. Operands Results

Examples - Bitwise Operations & and | 1010 1100 1010 1100 1101 1101 0x11111111 & 0000 0000 & 1111 1111 & 0000 1111 & 0x99999999 = 0000 0000 = 1010 1100 = 0000 1101 = 0x11111111 1010 1100 1010 1100 1101 0010 0x11111111 | 0000 0000 | 1111 1111 | 0000 1111 | 0x88888888 = 1010 1100 = 1111 1111 = 1101 1111 = 0x99999999

Examples - Bitwise Operations The XOR operator requires a little more care in discussion. Note that the last example of it can turn one message into another. 1010 1100 1010 1100 1101 0010 0xCAFEBABE ^ 0000 0000 ^ 1111 1111 ^ 0000 1111 ^ 0x74117070 = 1010 1100 = 0101 0011 = 1101 1101 = 0xBEEFCACE

Encryption with XOR One very powerful property of XOR that the other bitwise operations don't have is the recoverability of past operands. newx = x ^ key; oldx = newx ^ key; This is possible because taking the XOR with respect to key gives us x with all of the key bits flipped. If we do this twice, every bit in x is either never flipped or flipped twice (which brings it back to it's starting point). x ^ n ^ n = x ^ 0 = x for all x and n

Type Conversion – String to Numeric atoi We learned last week how to work with fscanf to read numeric values from input. However, once values are already in memory as strings, fscanf won't help us work with them as numeric values. int num = atoi(str); In stdlib.h, there is a function called atoi which converts a number in a string to a numeric int value. The function ignores any whitespace characters before the representation in the buffer given and it can be followed by any non-numeric characters. Will return 0 on failure to find a numeric string at the beginning of the buffer.

Type Conversion – How to implement atoi Remember from previous lectures that every digit in decimal representation is the coefficient of a power of 10. To implement atoi, we simply need to take each ASCII digit to it's numeric counterpart ('0'  0) Then multiply it by the power of 10 corresponding to that column. Finally, total everything up and that's the numeric value of that string. int atoi(char s[ ]) { int i, n = 0; for (i=0; s[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '0'); return n; }

Fundamentals – Conditional Expression ?: This is one of the only ternary operations you’re likely to find in most programming languages. An operation that takes 3 operands. int max = (a > b) ? a : b; The conditional (?:) operator is an in-place if-expression which has three parts: the condition, the then-value, and the else-value. If the condition is non-zero (true), the entire expression evaluates to the then-value. Otherwise, the entire expression evaluates to the else-value.