CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Similar documents
CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

BITS, BYTES, AND INTEGERS

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

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

Bits and Bytes January 13, 2005

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes, and Integers

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Manipulating Integers

Bitwise Data Manipulation. Bitwise operations More on integers

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Beginning C Programming for Engineers

JAVA OPERATORS GENERAL

Hardware: Logical View

Data III & Integers I

Bits, Bytes and Integers Part 1

Lecture 5-6: Bits, Bytes, and Integers

Operators in java Operator operands.

Data III & Integers I

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Integer Encoding and Manipulation

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

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

The Arithmetic Operators

CSCI2467: Systems Programming Concepts

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

Computer Organization & Systems Exam I Example Questions

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:

Representing and Manipulating Integers. Jo, Heeseung

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

Data III & Integers I

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

Binary representation of integer numbers Operations on bits

ME 461 C review Session Fall 2009 S. Keres

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

Bits, Bytes, and Integers

Informatics Ingeniería en Electrónica y Automática Industrial

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CS 107 Lecture 2: Bits and Bytes (continued)

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Systems Programming and Computer Architecture ( )

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

SECTION II: LANGUAGE BASICS

Bits, Bytes, and Integers Part 2

Bits, Bytes and Integers

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Chapter 3: Operators, Expressions and Type Conversion

Expression and Operator

CS 241 Data Organization Binary

Computer Systems CEN591(502) Fall 2011

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

The Design of C: A Rational Reconstruction"

Binary Logic (review)

Engineering Computing I

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

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

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CSCI 2212: Intermediate Programming / C Chapter 15

Expressions and Precedence. Last updated 12/10/18

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Introduction to Programming (Java) 2/12

Expressions (2E) Young Won Lim 4/9/18

Logical and Bitwise Expressions

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

AGENDA Binary Operations CS 3330 Samira Khan

Logic, Words, and Integers

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

This simulated machine consists of four registers that will be represented in your software with four global variables.

Time (self-scheduled): Location Schedule Your Exam: What to bring:

Expressions & Flow Control

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Arithmetic Operators. Portability: Printing Numbers

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

COMP2611: Computer Organization. Data Representation

Operators. Java operators are classified into three categories:

The Design of C: A Rational Reconstruction

Lecture 13 Bit Operations

Programming refresher and intro to C programming

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

The Design of C: A Rational Reconstruction" Jennifer Rexford!

COMP2121: Microprocessors and Interfacing. Number Systems

Data Representation 1

LAB A Translating Data to Binary

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

Overview. The C programming model. The C programming model. The C programming model. The C programming model 1/23/2009. Real-time Systems D0003E

Integers II. CSE 351 Autumn 2018

Transcription:

CSC 2400: Computer Systems Bit- Level vs. Logical Opera<ons Bit- Level Operators Common to C and Java &,, ~, ^, <<, >> 1

Used in Networking Used in Encryption/ Decryption 2

Used in Compression Look at the DEFLATE algorithm, for instance - https://en.wikipedia.org/wiki/deflate - everything in bits, not bytes Overview of C bit-level operators ~ Bitwise NOT ( flips bits) & Bitwise AND ^ Bitwise XOR Bitwise OR << Bitwise left shift (shifts bits to left) >> Bitwise right shift (shifts bits to right) 3

Observation 1 Addition q 2 s comp. addition is just binary addition. - assume all integers have the same number of bits - for now, assume that sum fits in n-bit 2 s comp. representation 01101000 (104) 11110110 (-10) + 11110000 (-16) + (-9) 01011000 (88) (-19) Assuming 8-bit 2 s complement numbers. Observation 2 Sign Extension q To add two numbers, we must represent them with the same number of bits. q If we just pad with zeroes on the left: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4; NOT GOOD!) q Instead, replicate the MS bit -- the sign bit: 4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4) 4

Bit-Level Operations in C q q Operations &,, ~, ^ available in C - Apply to any integral data type o long, int, short, char - View arguments as bit vectors - Arguments applied bit-wise Examples (char data type) - ~0x41 --> 0xBE ~01000001 2 --> 10111110 2 - ~0x00 --> 0xFF ~00000000 2 --> 11111111 2-0x69 & 0x55 --> 0x41 01101001 2 & 01010101 2 --> 01000001 2-0x69 0x55 --> 0x7D 01101001 2 01010101 2 --> 01111101 2 Bitwise Operator: NOT (~) q NOT (~) is the same as one s complement - Turns 0 to 1, and 1 to 0 q Examples: - Assume that x is an integer on 8 bits. Set x = 1. What is the value of ~x? 0 1 ~x - Set the least significant bit of y to 0: y = y & ; 5

Bitwise Operators: AND (&) and OR (I) Bitwise AND (&) Bitwise OR ( ) & 0 1 0 1 0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 & 15 0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 Bitwise Operators: AND (&) and OR (I) Assume that x is an integer variable. q Determine the least significant bit of x: int bit = x & ; q Set the least significant bit of x to 1: x = x ; 6

Bitwise Operator: XOR q XOR (^) - 0 if both bits are the same - 1 if the two bits are different ^ 0 1 0 1 q For an integer x, what is the value of x^x Relations Between Operations q DeMorgan s Laws - Express & in terms of, and vice-versa o A & B = ~(~A ~B) A and B are true if and only if neither A nor B is false o A B = ~(~A & ~B) A or B are true if and only if A and B are not both false q Exclusive-Or using Inclusive Or o A ^ B = (~A & B) (A & ~B) Exactly one of A and B is true o A ^ B = (A B) & ~(A & B) Either A is true, or B is true, but not both 7

Bitwise Operator: LeQ ShiQ q LeV shiv: x << y (same as mulxply by 2 y ) - ShiV bit- vector x lev y posixon - Fill blanks with 0 53 53<<2 0 0 1 1 0 1 0 1 Bitwise Operator: Right ShiQ q Right shiv: x >> y (same as divide by 2 y ) - ShiV bit- vector x right y posixons - Fill in blanks with sign bit (this is called arithme.c shi/) 53 0 0 1 1 0 1 0 1-75 1 0 1 1 0 1 0 1 53>>2-75>>2 sign extension sign extension Note: Java also has the unsigned shift operator >>> Not available in C. 8

Bitwise Operators q Print all the bits of a character from right to lev (starxng with the least significant bit): char c = 0xB5; 0 0 1 1 0 1 0 1 void reverse_decimal_to_binary(char c) { /* add code here */ } Bitwise Operators q Print all the bits of a character from lev to right (starxng with the most significant bit): char c = 0xB5; 1 0 1 1 0 1 0 1 void decimal_to_binary(char c) { /* add code here */ } 9

Bitmasks q Used to change or query one or more bits in a variable. q The bitmask indicates which bits are to be affected. q Common operaxons: - Set one or more bits (set to 1) - Clear one or more bits (set to zero) - Read one or more bits q Examples: - Set bit 2 of x (bit 0 is least significant): x = x - Clear bit 3 of x: x = x - Read bit 4 of x: bit = x Bitmasks q Set the least significant byte of x to FF: x = x ; q Clear the least significant byte of x: x = x ; q Read the least significant byte of x: byte = x ; 10

Bitmasks q Set to 1 bits 2, 4 and 7 of x (0 is least significant): x = x ; q Clear bits 3, 4 and 5 of x: x = x ; Contrast: Logical Operators 11

Familiar Operators (common to C, Java) Category Operators Arithmetic ++expr --expr expr++ expr-- expr1*expr2 expr1/expr2 expr1%expr2 expr1+expr2 expr1-expr2 Assignment expr1=expr2 expr1*=expr2 expr1/=expr2 expr1%=expr2 expr1+=expr2 expr1-=expr2 Relational expr1<expr2 expr1<=expr2 expr1>expr2 expr1>=expr2 expr1==expr2 expr1!=expr2 Logical!expr expr1&&expr2 expr1 expr2 Function Call Cast Conditional func(paramlist) (type)expr expr1?expr2:expr3 Logical Operators && (Logical AND), (Logical OR),! (Logical NOT) q Always return 0 or 1 q View 0 as False q Anything nonzero as True q Examples (char data type) -!0x41 --> -!0x00 --> -!!0x41 --> - 0x69 && 0x55 --> - 0x69 0x55 --> 12

What is the Output? int a = 0x43, b = 0x21; printf("a b = %x\n", a b); Printf("a b = %x\n", a b); printf("a & b = %x\n", a & b); printf("a && b = %x\n", a && b); Mental Exercise /* * isnotequal - return 0 if x == y, and 1 otherwise * Examples: isnotequal(5,5) = 0, isnotequal(4,5) = 1 */ int isnotequal (int x, int y) { return ; } 13

What did we learn? q Bit- level operators (&,, ~, ^, <<, >>) - Operate on bits (view arguments as bit vectors) q Logical operators (&&,,!) - Always return 0 (False) or 1 (True) 14