Bitwise Operator and Typecasting

Similar documents
1.3b Type Conversion

Type Conversion. and. Statements

CS 253. January 14, 2017

Arithmetic Operations

Bitwise Data Manipulation. Bitwise operations More on integers

EL2310 Scientific Programming

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Integers II. CSE 351 Autumn 2018

Chapter 3: Operators, Expressions and Type Conversion

Problem Solving & C M. Tech. (CS) 1st year, 2014

Binary representation of integer numbers Operations on bits

Integers II. CSE 351 Autumn Instructor: Justin Hsia

COMP2611: Computer Organization. Data Representation

C Programming Language (Chapter 2 of K&R) Variables and Constants

Fundamentals of Programming

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CSCI 2212: Intermediate Programming / C Chapter 15

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

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Chapter 4. Operations on Data

Fundamental of Programming (C)

C Language Programming

Information Science 1

C - Basics, Bitwise Operator. Zhaoguo Wang

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

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Integer Encoding and Manipulation

CSE I-Sem)

Introduction to Computing Systems Fall Lab # 3

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Expressions & Flow Control

EL2310 Scientific Programming

Programming. Elementary Concepts

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Integer Representation

CSCI2467: Systems Programming Concepts

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

Beginning C Programming for Engineers

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

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Arithmetic Operators. Portability: Printing Numbers

More Programming Constructs -- Introduction

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Arithmetic and Bitwise Operations on Binary Data

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

learning objectives learn about variables, their types and their values learn about different number representations

Course Outline Introduction to C-Programming

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

Chapter 13. Functions and Parameter Passing (Part 2)

Introduction to Programming (Java) 2/12

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Data III & Integers I

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

C++ Modern and Lucid C++ for Professional Programmers

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

SIGNED AND UNSIGNED SYSTEMS

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

Arithmetic and Bitwise Operations on Binary Data

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Computer System and programming in C

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

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

C-string format with scanf/printf

Data III & Integers I

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Bits, Bytes and Integers

Representing and Manipulating Integers Part I

LAB A Translating Data to Binary

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Zheng-Liang Lu Java Programming 45 / 79

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSC C69: OPERATING SYSTEMS

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates. Invitation to Computer Science, C++ Version, Third Edition

JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Data III & Integers I

Programming refresher and intro to C programming

CSC 1214: Object-Oriented Programming

ME 461 C review Session Fall 2009 S. Keres

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

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

CS33 Project Gear Up. Data

CSC 1107: Structured Programming

CSCI 2467, Spring 2018 Class Activity: Two s complement, bitwise and logical operators Monday, February 5

Integer Representation Floating point Representation Other data types

Quick Reference Guide

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Quick Reference Guide

The type of all data used in a C++ program must be specified

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

Real Time & Embedded Systems. Final Exam - Review

Transcription:

Bitwise Operator and Data and File Structures Laboratory http://www.isical.ac.in/~dfslab/2017/index.html Indian Statistical Institute Kolkata August 17, 2017 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 1 of 21

Outline 1 Bitwise Operators 2 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 2 of 21

Outline 1 Bitwise Operators 2 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 3 of 21

Different Types of Bitwise Operators & Bitwise AND Bitwise OR ˆ Bitwise XOR Left Shift Right Shift One s Complement DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 4 of 21

Boolean Logic: AND Gate Truth table A B Output (A & B) 0 0 0 1 0 0 0 1 0 1 1 1 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 5 of 21

Boolean Logic: OR Gate Truth table A B Output (A B) 0 0 0 1 0 1 0 1 1 1 1 1 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 6 of 21

Performing on a sequence of bits AND 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 OR 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 or 1 with a 0 0 1 with a 1 1 no change 0 with a 1 0 no change 0 or 1 with a 1 1 1 with a 0 1 no change 0 with a 0 0 no change DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 7 of 21

Applications Bitwise AND operator & is often used to mask off some set of bits 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 Bitwise OR operator is often used to mask on some set of bits 1 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 8 of 21

XOR and 1 s Complement Truth table A B Output (A ˆ B) 0 0 0 1 0 1 0 1 1 1 1 0 Truth table A Output ( A) 0 1 1 0 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 9 of 21

Left Shift and Right Shift Left Shift 1 1 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 1 0 0 1 0 Right Shift 1 1 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 1 0 0 1 3 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0 1 3 1 1 1 0 1 0 0 1 1 1 0 1 0 0 0 0 0 0 0 1 1 1 0 1 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 10 of 21

Use of Operatoes Making the third bit position of 1 1 1 0 1 0 0 1 turned off 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0 1 & 1 1 1 1 0 1 1 1 1 1 1 0 1 0 0 1 & ( (0 0 0 0 1 0 0 0)) 0 0 0 0 0 0 0 1 3 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 1 & (( (0 0 0 0 0 0 0 1)) 3) 1 1 1 0 1 0 0 1 & (( 1) 3) DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 11 of 21

Outline 1 Bitwise Operators 2 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 12 of 21

A type cast is basically a conversion from one type to another. Two types of type conversion: 1. Implicit Type Conversion Also known as automatic type conversion. 2. Explicit Type Conversion DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 13 of 21

Implicit Vs Explicit Done by the compiler It is user defined. In an expression more than one data type is present to avoid loss of data. Data types of variables upgraded to data type of the variable with largest data type. bool char short int int unsigned int long unsigned long long float double long double Possibility of loosing information. Signed is implicitly converted to unsigned The user can type cast the result to make it of a particular data type. The syntax in C: (data-type) expression var = (float) 5 / 4; DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 14 of 21

Built-in Typecast Function Inbuilt typecast functions in C Example: Typecast function atof() atoi() atol() itoa() ltoa() char a[10] = "5.46"; float var = atof(a); printf("var is = %f\n"); OUTPUT: var is = 5.460000 Description Converts string to float Converts string to int Converts string to long Converts int to string Converts long to string DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 15 of 21

Problem 1 getbits(x, p, n) function returns the (right adjusted) n-bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are sensible positive values. For example, getbits(x, 4, 3) returns the three bits in positions 4, 3 and 2, right-adjusted. DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 16 of 21

Type 1: unsigned getbits(unsigned x, int p, int n){ int i; unsigned sum=0; for(i=0;i<n;i++){ sum = sum + pow(2,i); } return ((x >> p+1-n) & sum); } Type 2: unsigned getbits(unsigned x, int p, int n){ return (x >> (p+1-n)) & ~(~0 << n); } DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 16 of 21 Problem 1: Solution getbits(x, p, n) function returns the (right adjusted) n-bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are sensible positive values. For example, getbits(x, 4, 3) returns the three bits in positions 4, 3 and 2, right-adjusted.

Explanation of Type 1 solution P = 4, n = 3 for(i = 0; i < n; i + +) sum = sum + pow(2,i) Then sum = 0 0 0 0 0 1 1 1 Let x be 0 1 0 1 1 0 1 1 We output bits 0 1 0 1 1 0 1 1 If x p + 1 n 0 0 0 1 0 1 1 0 Now (x p + 1 n) & sum 0 0 0 0 0 1 1 0 DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 17 of 21

Problem 2 Write a function setbits(x, p, n, y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 18 of 21

Problem 3 Write a function invert(x, p, n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged. DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 19 of 21

Problem 4 Write a function rightrot(x, n) that returns the value of the integer x rotated to the right by n positions. DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 20 of 21

Problem 5 1 Implement bitwise AND operator in terms of other operators. 2 Implement bitwise OR operator in terms of other operators. 3 Implement bitwise XOR operator in terms of other operators 4 Write a C program using bitwise operators to check whether an integer is a power of two or not. DFS Lab, ISI Kolkata Bitwise Operator and August 17, 2017 Slide - 21 of 21