Tutorial 1: C-Language

Similar documents
COMP2611: Computer Organization. Data Representation

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

Integer Representation

Computer Organization & Systems Exam I Example Questions

CSCI 2212: Intermediate Programming / C Chapter 15

Binghamton University. CS-211 Fall Pointers vs. Arrays

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

Beginning C Programming for Engineers

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?

Binghamton University. CS-211 Fall Pointers

Midterm Exam Review Slides

CprE 288 Spring 2014 Homework 5 Due Fri. Feb. 21

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

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

SIGNED AND UNSIGNED SYSTEMS

Binghamton University. CS-211 Fall Pointers vs. Arrays

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

EL2310 Scientific Programming

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

Dynamic Memory Allocation and Command-line Arguments

Memory, Data, & Addressing II CSE 351 Spring

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

Create a Program in C (Last Class)

INFO-I308 Laboratory Session #2

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

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

Pointers (1A) Young Won Lim 1/9/18

Computer Systems Programming. Practice Midterm. Name:

Pointers (1A) Young Won Lim 1/5/18

Bits, Bytes, and Integers

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

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Programming in C++ 6. Floating point data types

Chapter 3 Structured Program Development in C Part II

AN703. Micro64/128. Accessing the 36k of SRAM 12/3/04

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Chapter 3 Basic Data Types. Lecture 3 1

Arithmetic and Bitwise Operations on Binary Data

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

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Binghamton University. CS-211 Fall Pointers

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Survey. Motivation 29.5 / 40 class is required

C-string format with scanf/printf

C-string format with scanf/printf

Bitwise Instructions

AGENDA Binary Operations CS 3330 Samira Khan

Pointers (1A) Young Won Lim 3/5/18

Unit 4. Input/Output Functions

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

Introduction. Keywords: MAXQ, IAR, memory allocation, flash data, flash storage, SRAM

A Short Course for REU Students Summer Instructor: Ben Ransford

Programming (1.0hour)

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

Chapter 7. Basic Types

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

Conto D2 COMMUNICATION PROTOCOL CONTENTS 1.0 INTRODUCTION

Pointers (2) Applications

A Fast Review of C Essentials Part I

Getting started with Java

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

We would like to find the value of the right-hand side of this equation. We know that

Expression and Operator

Review Topics. Midterm Exam Review Slides

ECE264 Fall 2013 Exam 3, November 20, 2013

Bits, Bytes and Integers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Pointers as Arguments

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

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

Introduction to C Programming

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

CE4DMID01 COMMUNICATION PROTOCOL CONTENTS 1.0 INTRODUCTION

Bits, Bytes and Integers Part 1

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

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

CS61B Lecture #14: Integers

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

C# MOCK TEST C# MOCK TEST II

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Using pointers with functions

Arithmetic and Bitwise Operations on Binary Data

IF96017 MODBUS COMMUNICATION PROTOCOL

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

Data Types and Variables in C language

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

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

Computer System and programming in C

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Data Representation and Storage. Some definitions (in C)

CS C Primer. Tyler Szepesi. January 16, 2013

BITS, BYTES, AND INTEGERS

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

First of all, it is a variable, just like other variables you studied

Transcription:

Tutorial 1: C-Language Problem 1: Data Type What are the ranges of the following data types? int 32 bits 2 31..2 31-1 OR -2147483648..2147483647 (0..4294967295 if unsiged) in some machines int is same as short long same as int long long 64 bits 2 63..2 63-1 OR -9223372036854775808..9223372036854775807 (0..18446744073709551615 if unsigned) short 16 bits 2 15..2 15-1 OR -32768..32767 (0..65535 if unsigned) char 8 bits 2 7..2 7-1 OR -128..127 (0..255 if unsigned) Problem 2: Data Type Conversion Consider the C code in Figure 1. Answer the following questions. What are the meaning of statements (b = (short) a;) and (c = (char) a;)? What are the outputs of the printf statements if (a = -232590606)? short b; char c; b = (short) a; c = (char) a; printf("short = \"%d\"\n\n", b); printf("char = \"%d\"\n\n", c); Figure 1: Program on Data Type Conversion Two statements (b = (short) a;) and (c = (char) a;) are called type casts. They convert one data type to another. In order to understand how they work and what are printed out we must start with representing (a = -232644062) in hex (base 16) format. We can do this using a calculator. In hex format (a = -232644062) is represented as (a = 0xf2222222). Two statements (b = (short) a;) and (c = (char) a;) have the following effects: (b = (short) a;) : (a = 0xf2222222) in 32 bits (b = 0x2222) in 16 bits (c = (char) a;) : (a = 0xf2222222) in 32 bits (c = 0x22) in 8 bits Converting back from the hex format to decimal give (b = 8738) and (c = 34). 1

integer = "-232644062" short = "8738" char = "34" Figure 2: The Outputs of printf Statements For (a = -232590606) the hex representation is (a = 0xf222f2f2). Two statements (b = (short) a;) and (c = (char) a;) have the following effects: (b = (short) a;) : (a = 0xf222f2f2) in 32 bits (b = 0xf2f2) in 16 bits (c = (char) a;) : (a = 0xf222f2f2) in 32 bits (c = 0xf2) in 8 bits Converting back from the hex format to decimal give (b = -3342) and (c = -14). integer = "-232590606" short = "-3342" char = "-14" Figure 3: The Outputs of printf Statements Problem 3: More Data Type Conversion Consider the C code in Figure 4. Answer the following questions. What are the meaning of statements (a = (int) b;) and (c = (char) b;)? What are the outputs of the printf statements if (b = -30848)? short b= -32768; int a; char c; a = (int) b; c = (char) b; printf("short = \"%d\"\n\n", b); printf("char = \"%d\"\n\n", c); Figure 4: Program on Data Type Conversion Let us see how the two type casts in statements (b = (short) a;) and (c = (char) a;) work. In order to understand how they work and what are printed let us represent (b = -32768) in hex format as (b = 0x80000). Two statements (a = (int) b;) and (c = (char) b;) have the following effects: (a = (int) b;) : (b = 0x8000) in 16 bits (a = 0xffff8000) in 32 bits (c = (char) b;) : (b = 0x8000) in 16 bits (c = 0x00) in 8 bits Converting back from the hex format to decimal give (a = -32768) and (c = 0). 2

short = "-32768" integer = "-32768" char = "0" Figure 5: The Outputs of printf Statements For (b = -30848) the hex representation is (a = 0x8780). Two statements (a = (int) b;) and (c = (char) b;) have the following effects: (a = (int) b;) : (b = 0x8780) in 16 bits (a = 0xffff8780) in 32 bits (c = (char) b;) : (b = 0x8780) in 16 bits (c = 0x80) in 8 bits Converting back from the hex format to decimal give (a = -30848) and (c = -128). short = "-30848" integer = "-30848" char = "-128" Figure 6: The Outputs of printf Statements Problem 4: Some More Data Type Conversion Consider the C code in Figure 7. Answer the following questions. What are the meaning of statements (b = (int)(short) a;) and (c = (int)(char) a;)? What are the outputs of the printf statements if (a = -232590606)? int b; int c; b = (int)(short) a; c = (int)(char) a; printf("int_short = \"%d\"\n\n", b); printf("int_char = \"%d\"\n\n", c); Figure 7: Program on Data Type Conversion Two type casts statements (b = (int)(short) a;) and (c = (int)(char) a;) convert (a) to types (short) and (char) and back to (int). Again representing (a = -232644062) as (a = 0xf2222222). Two statements (b = (int) (short) a;) and (c = (int) (char) a;) have the following effects: (b = (int)(short) a;) : (a = 0xf2222222) in 32 bits (0x2222) in 16 bits (0x2222) in 16 bits (b = 0x00002222) in 32 bits 3

(c = (int)(char) a;) : (a = 0xf2222222) in 32 bits (0x22) in 8 bits (0x22) in 8 bits (c = 0x00000022) in 32 bits Converting back from the hex format to decimal give (b = 8738) and (c = 34). integer = "-232644062" int_short = "8738" int_char = "34" Figure 8: The Outputs of printf Statements For (a = -232590606) the hex representation is (a = 0xf222f2f2). Two statements (b = (int)(short) a;) and (c = (int)(char) a;) have the following effects: (b = (int)(short) a;) : (a = 0xf222f2f2) in 32 bits (0xf2f2) in 16 bits (0xf2f2) in 16 bits (b = 0xfffff2f2) in 32 bits (c = (char) a;) : (a = 0xf222f2f2) in 32 bits (0xf2) in 8 bits (0xf2) in 8 bits (c = 0xfffffff2) in 32 bits Converting back from the hex format to decimal give (b = -3342) and (c = -14). integer = "-232590606" int_short = "-3342" int_char = "-14" Figure 9: The Outputs of printf Statements Problem 5: Still Some More Data Type Conversion Consider the C code in Figure 10. Answer the following questions. What is the output of the last printf statement, if (+ 1) is removed from the statement (d =(short *) &a + 1;)? char *b, *c; short *d; b =(char *) &a; c =(char *) &a + 1; d =(short *) &a + 1; printf("char1 = \"%d\"\n\n", *b); printf("char2 = \"%d\"\n\n", *c); printf("short = \"%d\"\n\n", *d); Figure 10: Program on Data Type Conversion with Pointers 4

For (a = -232644062) the hex representation is (a = 0xf2222222). We can represent a as contacanation of 4 bytes as: a = 0xf2222222. The statement (b = (char *) &a;) obtains the address of the int type variable a and converts is to the address of the char type through type cast (char *) and assigns it to pointer c of type char. So what gets printed out by the second printf statement the first (right most) byte of a which is 0x22 = 34. The (c = (char *) &a + 1;) will access the second (from right) byte of a which is again 0x22 = 34. The statement (d = (short *) &a +1;) obtains the address of the integer type variable a and converts is to the address of the short (16-bit) type through type cast (short *), addes 1 to it, and assigns it to pointer d of type short. So what gets printed out by the last printf statement the second (left) half of a which is 0xf222 = -3550. char1 = "34" char2 = "34" short = "-3550" Figure 11: The Outputs of printf Statements By dropping (+ 1) from the statement (d = (short *) &a + 1;) we obtains the address of first (right) half of a which is 0x2222 = 8738. So what gets printed out by the last printf statement is the first (right) half of a which is 0x2222 = 8738. 5