C - Basics, Bitwise Operator. Zhaoguo Wang

Similar documents
A Fast Review of C Essentials Part I

CS 61C: Great Ideas in Computer Architecture Introduction to C


Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Motivation was to facilitate development of systems software, especially OS development.

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Motivation was to facilitate development of systems software, especially OS development.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS240: Programming in C. Lecture 2: Overview

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Programming for Engineers Iteration

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

The Design of C: A Rational Reconstruction: Part 2

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Short Notes of CS201

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Fundamental of Programming (C)

Fundamentals of Programming

CS201 - Introduction to Programming Glossary By

Continued from previous lecture

CMPE-013/L. Introduction to C Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

Computer Components. Software{ User Programs. Operating System. Hardware

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

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

PRINCIPLES OF OPERATING SYSTEMS

EL2310 Scientific Programming

Programming refresher and intro to C programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Pointers (continued), arrays and strings

Course Outline Introduction to C-Programming

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

The Arithmetic Operators

Pointers (continued), arrays and strings

Programming in C and C++

Quick Reference Guide

Presented By : Gaurav Juneja

Quick Reference Guide

CS240: Programming in C

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CS 314 Principles of Programming Languages. Lecture 9

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Programming in C - Part 2

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Introduction to C Programming

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

Work relative to other classes

EMBEDDED SYSTEMS PROGRAMMING Language Basics

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Topic 6: A Quick Intro To C

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Computer Components. Software{ User Programs. Operating System. Hardware

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Programming. Elementary Concepts

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

Lecture 2: C Programming Basic

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

Part I Part 1 Expressions

Introduction to Programming Using Java (98-388)

Expressions & Flow Control

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arithmetic Operators. Portability: Printing Numbers

6.096 Introduction to C++ January (IAP) 2009

Structure of this course. C and C++ Past Exam Questions. Text books

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Page 1. Agenda. Programming Languages. C Compilation Process

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

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

COMP 2355 Introduction to Systems Programming

1.3b Type Conversion

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

DEPARTMENT OF MATHS, MJ COLLEGE

Programming in C and C++

CS Programming In C

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

EMBEDDED SYSTEMS PROGRAMMING Language Basics

C Syntax Out: 15 September, 1995

An overview of Java, Data types and variables

Arithmetic and Bitwise Operations on Binary Data

Government Polytechnic Muzaffarpur.

CSI33 Data Structures

C and C++ I. Spring 2014 Carola Wenk

Integers II. CSE 351 Autumn 2018

The Compilation Process

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

CSC 1107: Structured Programming

Chapter 11 Introduction to Programming in C

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

CSCI2467: Systems Programming Concepts

Java Basic Programming Constructs

Transcription:

C - Basics, Bitwise Operator Zhaoguo Wang

Java is the best language!!! NO! C is the best!!!!

Languages C Java Python 1972 1995 2000 (2.0) Procedure Object oriented Procedure & object oriented Compiled to machine code, runs on bare machine Compiled to bytecodes, runs by another piece of software Scripting language, interpreted by software static type static type dynamic type Manual memory management Automatic memory management with GC

Hello World 1 #include <stdio.h> 2 3 int main() 4 { 5 printf("hello, world\n"); 6 return 0; 7

Hello World 1 #include <stdio.h> 2 3 int main() 4 { 5 printf("hello, world\n"); 6 return 0; 7 Header file Standard Library gcc helloworld.c o helloworld

Compiling Source Codes [*.c, *.h] C Program Preprocessor gcc E *.c Source Codes [*.i] gcc helloworld.c o helloworld

Compiling Source Codes [*.c, *.h] C Program Preprocessor gcc E *.c Source Codes [*.i] gcc S *.i C Program Compiler gcc helloworld.c o helloworld Assembly Codes [*.s]

Compiling Source Codes [*.c, *.h] C Program Preprocessor gcc E *.c Source Codes [*.i] gcc S *.i C Program Compiler gcc helloworld.c o helloworld Assembly Codes [*.s] gcc c *.s Binary Codes [*.o] Assembler

Compiling Source Codes [*.c, *.h] C Program Preprocessor gcc E *.c Source Codes [*.i] gcc S *.i C Program Compiler Source Codes [* (exec)] gcc helloworld.c o helloworld gcc *.o gcc c *.s Assembly Codes [*.s] Linker Binary Codes [*.o] Assembler

Three basic elements Variables The basic data objects manipulated in a program Operator What is to be done to them Expressions Combine the variables and constants to produce new values

Variables Declaration: int a = 1; Initial value Type Name

Variables Declaration: int a; Type Name Initial value later: a = 0;

Primitive Types type size (bytes) example (unsigned) char 1 char c = a (unsigned) short 2 short s = 12 (unsigned) int 4 int i = 1 (unsigned) long 8 long l = 1 float 4 float f = 1.0 double 8 double d = 1.0 pointer 8 int *x = &i 64 bits machine

Implicit conversion int main() { int a = 0; unsigned int b = 1; if (a < b) { printf("%d is smaller than %d\n", a, b); else if (a > b) { printf("%d is larger than %d\n, a, b); return 0;

Implicit conversion int main() { int a = -1; unsigned int b = 1; if (a < b) { printf("%d is smaller than %d\n", a, b); else if (a > b) { return 0; printf("%d is larger than %d\n, a, b); -1 is promoted to unsigned int and thus appears to be a large positive number. (4294967295) 10

Explicit conversion int main() { int a = -1; unsigned int b = 1; if (a < (int) b) { printf("%d is smaller than %d\n", a, b); else if (a > (int) b) { return 0; printf("%d is larger than %d\n, a, b); (type-name) expression

Operators Arithmetic +, -, *, /, %, ++, -- Relational ==,!=, >, <, >=, <= Logical &&,,! Bitwise &,, ^, ~, >>, << Arithmetic, Relational and Logical operators are identical to java s

Bitwise operator & And (&) given two bits x and y, x & y = 1 when both x = 1 and y = 1 y x & 0 1 0 0 0 1 0 1 ( 0 1 1 0 1 0 0 1 ) 2 & ( 0 1 0 1 0 1 0 1 ) 2

Bitwise operator & And (&) given two bits x and y, x & y = 1 when both x = 1 and y = 1 y x & 0 1 0 0 0 1 0 1 ( 0 1 1 0 1 0 0 1 ) 2 & ( 0 1 0 1 0 1 0 1 ) 2 ( 0 1 0 0 0 0 0 1 ) 2

Bitwise operator & And (&) given two bits x and y, x & y = 1 when both x = 1 and y = 1 & is often used to mask off some set of bits y x & 0 1 0 0 0 1 0 1 ( 0 1 1 0 1 0 0 1 ) 2 & ( 0 0 0 0 1 1 1 1 ) 2 ( 0 0 0 0 1 0 0 1 ) 2

Bitwise operator Or ( ) given two bits x and y, x y = 1 when either x = 1 or y = 1 y x 0 1 0 0 1 1 1 1 ( 0 1 1 0 1 0 0 1 ) 2 ( 0 1 0 1 0 1 0 1 ) 2

Bitwise operator Or ( ) given two bits x and y, x y = 1 when either x = 1 or y = 1 y x 0 1 0 0 1 1 1 1 ( 0 1 1 0 1 0 0 1 ) 2 ( 0 1 0 1 0 1 0 1 ) 2 ( 0 1 1 1 1 1 0 1 ) 2

Bitwise operator Or ( ) given two bits x and y, x y = 1 when either x = 1 or y = 1 is often used to turn some bits on y x 0 1 0 0 1 1 1 1 ( 0 1 1 0 1 0 0 1 ) 2 ( 0 1 0 1 0 1 0 1 ) 2 ( 0 1 1 1 1 1 0 1 ) 2

Bitwise operator ~ Not (~) given a bit x, ~x = 1 when x = 0 One s complement ~ 0 1 1 0 x ~ ( 0 1 1 0 1 0 0 1 ) 2

Bitwise operator ~ Not (~) given a bit x, ~x = 1 when x = 0 One s complement ~ 0 1 1 0 x ~ ( 0 1 1 0 1 0 0 1 ) 2 ( 1 0 0 1 0 1 1 0 ) 2

Bitwise operator ^ Xor (^) given two bits x and y, x ^ y = 1 when either x = 1 or y = 1, but not both y x ^ 0 1 0 0 1 1 1 0 ( 0 1 1 0 1 0 0 1 ) 2 ^ ( 0 1 0 1 0 1 0 1 ) 2

Bitwise operator ^ Xor (^) given two bits x and y, x ^ y = 1 when either x = 1 or y = 1, but not both y x ^ 0 1 0 0 1 1 1 0 ( 0 1 1 0 1 0 0 1 ) 2 ^ ( 0 1 0 1 0 1 0 1 ) 2 ( 0 0 1 1 1 1 0 0 ) 2

Bitwise operator << Left shift ( << ) x << y, shift bit-vector x left y positions Throw away extra bits on left Fill with 0 s on right x 0 1 1 0 1 0 0 1 x << 3

Bitwise operator << Left shift ( << ) x << y, shift bit-vector x left y positions Throw away extra bits on left Fill with 0 s on right x 0 1 1 0 1 0 0 1 x << 3 0 1 0 0 1 0 0 0

Bitwise operator >> Right shift ( >> ) x >> y, shift bit-vector x right y positions Throw away extra bits on right Fill with??? on left Logical shifting Arithmetic shifting

Bitwise operator >> Right shift ( >> ) x >> y, shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0 s on left x 1 0 1 0 1 0 0 1 Logical x >> 3 0 0 0 1 0 1 0 1

Bitwise operator >> Right shift ( >> ) x >> y, shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0 s on left Arithmetic shift Replicate most significant bit on the left x 1 0 1 0 1 0 0 1 Logical x >> 3 0 0 0 1 0 1 0 1 Arithmetic x >> 3 1 1 1 1 0 1 0 1

Bitwise operator >> Right shift ( >> ) x >> y, shift bit-vector x right y positions Throw away extra bits on right Logical shift (shr) Fill with 0 s on left Arithmetic shift (sar) Replicate most significant bit on the left x 1 0 1 0 1 0 0 1 Logical x >> 3 0 0 0 1 0 1 0 1 Arithmetic x >> 3 1 1 1 1 0 1 0 1

Which operation is used in C? Arithmetic shifting on signed number, logical shifting on unsigned number #include <stdio.h> int main() { int a = 1; unsigned int b = 1; printf("%d %d\n", a>>10, b>>10);

Logical shift on signed number int lsr(int x, int n) {???

Logical shift on signed number Observation It do the logical shift on unsigned number Solution Convert the signed type into unsigned

Logical shift on signed number int lsr(int x, int n) { return (int)((unsigned int)x >> n);

Control flow int a = b + 1 expression

Expression Combine the variables and constants to produce new values int a = b + 1 int c = ( d << 1 ) + 2 float f = (float) c

Control flow int a = b + 1; statement

Control flow { int a = b + 1; int c = a * 2; block

Control flow if (expression){ control statement int a = b + 1; int c = a * 2;

if (expression) statement 1 else statement 2 Control flow

Control flow if (expression) statement 1 else statement 2 if (expression 1 ) statement 1 else if (expression 2 ) statement 2 else statement 3

Control flow switch (expression) { case const-expr 1 : statements 1 case const-expr 2 : statements 2 default: statements 3

Control flow while (expression) { statement

Control flow while (expression) { statement for(expr1; expr2; expr3) { statement

Control flow expr 1 ; while(expr 2 ) { statement expr 3 ; for(expr1; expr2; expr3) { statement

Control flow Break cause the innermost enclosing loop or switch to be exited immediately Continue cause the next iteration of the enclosing for, while, or do loop to begin.

Control flow goto label Usable C provides the infinitely-abusable goto statement, and labels to branch to. Abandon processing in some deeply nested structure. for(...) { for(...) { for(...) { goto error error: clean up the mess

Exercises Given a number, write a function to decide if it is even? bool iseven(int n) {

Exercises Given a number, write a function to decide if it is even? bool iseven(int n) { return (n & 1) == 0;

Exercises Given a number, write a function to decide if it is a power of two? bool ispoweroftwo(int n) {

Exercises Given a number, write a function to decide if it is a power of two? bool ispoweroftwo(int n) { return (n & (n-1)) == 0;

Exercises Given a number, write a function to decide if it is a power of two? bool ispoweroftwo(int n) { return n!= 0 && (n & (n-1)) == 0;

Exercises Given a number, write a function to decide if it is a power of two? bool ispoweroftwo(int n) { return n!= 0 && (n & (n-1)) == 0 && (n >> 31) == 0 ;

Exercises Count the number of ones in the binary representation of the given number? bool count_one(int n) {

Exercises Count the number of ones in the binary representation of the given number? bool count_one(int n) { A trick clear the last bit: n & (n -1)

Exercises Count the number of ones in the binary representation of the given number? bool count_one(int n) { while(n!= 0) { n = n&(n-1); count++; return count;