CS Programming In C

Similar documents
Computer Organization & Systems Exam I Example Questions

Beginning C Programming for Engineers

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

(T) x. Casts. A cast converts the value held in variable x to type T

Fundamentals of Programming

Representing and Manipulating Integers Part I

CS61B Lecture #14: Integers

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

Fundamental of Programming (C)

Chapter 7. Basic Types

Chapter Two MIPS Arithmetic

Integer Representation

Representing and Manipulating Integers. Jo, Heeseung

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Work relative to other classes

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

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

Arithmetic and Bitwise Operations on Binary Data

Arithmetic type issues

LAB A Translating Data to Binary

Computer Systems C S Cynthia Lee

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

8. Characters and Arrays

Data Type Fall 2014 Jinkyu Jeong

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

More about Binary 9/6/2016

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn 2018

Data Types. Data Types. Integer Types. Signed Integers

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

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

CS 241 Data Organization Binary

Tutorial 1: C-Language

THE FUNDAMENTAL DATA TYPES

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Programming in C++ 6. Floating point data types

Low-Level Programming

A Fast Review of C Essentials Part I

Arithmetic Expressions in C

Arithmetic Operators. Portability: Printing Numbers

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

Lab 1: Introduction to C Programming

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

Fundamentals of Programming

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

CS Programming In C

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

Fundamental of Programming (C)

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

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Bits, Bytes, and Integers Part 2

Representing Integers

Chapter 3 Basic Data Types. Lecture 3 1

Binary Representations and Arithmetic

Algorithms, Data Structures, and Problem Solving

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

int main(void) { int a, b, c; /* declaration */

Lecture 2: C Programming Basic

Introduction to C Programming

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CSCI 2212: Intermediate Programming / C Chapter 15

Pointers (2) Applications

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

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

Bits, Bytes and Integers

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CS 107 Lecture 3: Integer Representations

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Computer Systems CEN591(502) Fall 2011

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Integer Representation

Computer Systems Programming. Practice Midterm. Name:

Arithmetic and Bitwise Operations on Binary Data

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

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

AGENDA Binary Operations CS 3330 Samira Khan

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS107, Lecture 2 Bits and Bytes; Integer Representations

Bits, Bytes and Integers Part 1

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Bitwise Instructions

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Computers Programming Course 5. Iulian Năstac

CS 107 Lecture 2: Bits and Bytes (continued)

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Transcription:

CS 24000 - Programming In C Week Eight: arithmetic and bit operations on chars ; C Programming Tools: GDB, C preprocessor, Makefile Zhiyuan Li Department of Computer Science Purdue University, USA

This week s lectures Arithmetic and bit-wise operations on characters related to Lab 6 Tools for C program development GDB for debugging, C preprocessors, and Makefile

Char input and output In Lab 6, we deal with arbitrary files, not just text files. A non-text file may be unformatted, and therefore may not be suitable for formatted I/O functions such as scantf() and printf(). Since we are doing byte-level encryption/decryption in Lab 6, we perform character I/O by either UNIX read/write calls or getchar()/putchar() C standard lib routines

UNIX read/write In the first week, we looked at how to do char I/O using the UNIX read() and write() system calls. For Lab 6, we can do unbufferred read/write Not efficient, but simple: #include <stdio.h> main() { char buf[1]; int n; unsigned char code, key = \x0f ; while ((n = read(0, buf, 1)) > 0) { code = *buf; *buf = code ^ key; /* perform an xor cipher */ write(1, buf, 1); } return 0; }

Use getchar() and putchar() Alternatively, we can use C standard library functions getchar() putchar() Must be careful with typecast #include <stdio.h> main() { unsigned char msg, code, key = '\x0f'; int input; while ((input = getchar())!= EOF) { msg = (unsigned char) input; code = msg ^ key; input = (int) code; putchar(input); } return 0; }

Sign Extension The main reason for getchar() to return an integer (typecast from unsigned char) is because the EOF number is greater than what a char can represent The leading bits of the typecast integer will be all zeros because the read char is unsigned Note: By default a char is signed. Casting it to int will result in sign extension, \xff will be come 0xffffffff

We want to perform byte-level cipher Therefore we must recast the integer to unsigned char Otherwise, we will be rotating four bytes, e.g. Function putchar() takes an integer as parameter, hence we recast the unsigned char result to integer, x, before calling putchar(x) Again the leading bits will be zero in this integer Putchar(x) will automatically convert x back to unsigned char before writing to the output.

Type Promotion The arithmetic unit on the processor hardware operates on integers. Therefore to perform add, subtract, and other arithmetic operations on bytes The operands are promoted to integers first If the lvalue (i.e. the variable holding the result) in the assignment statement is a char (or unsigned char) The integer result will be automatically recast to char (or unsigned char) Next, we compare the result of adding unsigned numbers versus signed numbers Pay attention to sign extension and type casting

Adding unsigned chars (byteadd.c) #include <stdio.h> main() { unsigned char ua = 0, ub = -1, ux; unsigned char uc = 0, ud = 1, uy; int w, z; unsigned char ubig = '\xff', uoverflow, ucast; int ioverflow; /* difference between adding a negative byte versus subtracting a positive byte when writing back to an integer */ ux = ua + ub; uy = uc - ud; w = ua + ub; z = uc - ud; printf("ux is \t %#X\n", ux); printf("uy is \t %#X\n", uy); printf("w is \t %#X\n", w); printf("z is \t %#X\n", z); /* The following shows what happens with 'overflow' when adding bytes together */ uoverflow = ubig + ubig; ioverflow = ubig + ubig; ucast = (unsigned char) ioverflow; printf("uoverflow is \t %#X\n", uoverflow); printf("ioverflow is \t %#X\n", ioverflow); printf("icast is \t %#X\n", ucast); return 0;}

Results ux is 0XFF uy is 0XFF w is 0XFF z is 0XFFFFFFFF uoverflow is 0XFE ioverflow is 0X1FE icast is 0XFE

Adding signed chars (signedadd.c) #include <stdio.h> main() { char a = 0, b = -1, x; char c = 0, d = 1, y; int w, z; char big = '\xff', overflow, bytecast; int ioverflow; x = a + b; y = c - d; w = a + b; z = c - d; printf("x is \t %#X\n", x); printf("y is \t %#X\n", y); printf("w is \t %#X\n", w); printf("z is \t %#X\n", z); /* The following shows what happens with 'overflow' when adding bytes together */ overflow = big + big; ioverflow = big + big; bytecast = (char) ioverflow; printf("overflow is \t %#X\n", overflow); printf("ioverflow is \t %#X\n", ioverflow); printf("bytecast is \t %#X\n", bytecast); return 0;

Results x is 0XFFFFFFFF y is 0XFFFFFFFF w is 0XFFFFFFFF z is 0XFFFFFFFF overflow is 0XFFFFFFFE ioverflow is 0XFFFFFFFE bytecast is 0XFFFFFFFE

The Unix od command Let us run command: od -t x1 text Displays 0000000 61 62 63 64 65 66 67 0a 0000010 We see that the seven letters are displayed seven chars in hexadecimal representation: 61 to 67. Text: abcdefg

Next, we will examine the od display of the result of bit-wise xor, char addition, and bitrotation These are three cipher operations used in Lab 6

Review the example on bit rotation /* Purpose: showing result of bit rotation */ #include <stdio.h> main() { char a = '\x0f'; char b, c; unsigned char ua = '\x0f'; unsigned char ub, uc; printf("a is \t %#X\n", a); printf("a is \t %#X\n", a); printf("ua is \t %#X\n", ua); ub = ua >> 2; printf("ub is \t %#X\n", ub); uc = ua << 6; printf("uc is \t %#X\n", uc); ua = ub uc; printf("ua is \t %#X\n", ua); ua = '\x0f'; ua = ua >> 2 ua << 6; printf("rotation of ua is \t%#x\n", (unsigned char) ua >> 2 ua << 6); } b = a >> 2; printf("b is \t %#X\n", b); c = a << 6; printf("c is \t %#X\n", c); a = b c;