LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

Similar documents
World Inside a Computer is Binary

Number representations

COE 202- Digital Logic. Number Systems II. Dr. Abdulaziz Y. Barnawi COE Department KFUPM. January 23, Abdulaziz Barnawi. COE 202 Logic Design

Arrays and Strings. Arash Rafiey. September 12, 2017

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

As stated earlier, the declaration

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Number System. Introduction. Decimal Numbers

PROGRAMMING IN C AND C++:

Subject: Computer Science

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

COE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

COMP2611: Computer Organization. Data Representation

Advanced C Programming Topics

Course Outline Introduction to C-Programming

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Principles of Programming. Chapter 6: Arrays

Moodle WILLINGDON COLLEGE SANGLI. ELECTRONICS (B. Sc.-I) Introduction to Number System

Number Systems. Both numbers are positive

CS & IT Conversions. Magnitude 10,000 1,

CMPE223/CMSE222 Digital Logic Design. Positional representation

COMP Overview of Tutorial #2

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Final Labs and Tutors

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation


Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Digital Fundamentals

AP Computer Science Unit 1. Programs

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Integer Representation

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

Lab ACN : C++ Programming Exercises

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Introduction to Computer Science Midterm 3 Fall, Points

Lecture (01) Introduction Number Systems and Conversion (1)

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Functions. Arash Rafiey. September 26, 2017

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

LAB A Translating Data to Binary

BINARY SYSTEM. Binary system is used in digital systems because it is:

Programming for Engineers Arrays

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

WARM UP LESSONS BARE BASICS

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

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

Number Systems. TA: Mamun. References: Lecture notes of Introduction to Information Technologies (ITEC 1011) by Dr Scott MacKenzie

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

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

Binary Representations and Arithmetic

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Numeral system Numerals

Computer Science & Engineering 150A Problem Solving Using Computers

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

211: Computer Architecture Summer 2016

LAB 5: REPETITION STRUCTURE(LOOP)

Arithmetic type issues

Algorithms, Data Structures, and Problem Solving

Introduction to C Language

ME 461 C review Session Fall 2009 S. Keres

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

This is CS50. Harvard University Fall Quiz 0 Answer Key

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Embedded Systems - FS 2018

An-Najah National University Faculty of Engineering Electrical Engineering Department Programmable Logic Controller. Chapter 11 Math instruction

CHW 261: Logic Design

SISTEMI EMBEDDED. Basic Concepts about Computers. Federico Baronti Last version:

LAB 5: REPETITION STRUCTURE(LOOP)

Numerical Representations On The Computer: Negative And Rational Numbers

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

The C Programming Language Guide for the Robot Course work Module

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Numerical Representations On The Computer: Negative And Rational Numbers

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

CMPS 10 Introduction to Computer Science Lecture Notes

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Arrays. Week 4. Assylbek Jumagaliyev

Do not start the test until instructed to do so!

Dept. of CSE, IIT KGP

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Variables and Operators 2/20/01 Lecture #

1. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

A Fast Review of C Essentials Part I

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Display Input and Output (I/O)

Transcription:

LAB 1 Binary Numbers/ Introduction to C Rajesh Rajamani ME 431 Department of Mechanical Engineering University Of Minnesota OVERVIEW What is feedback control? Binary and Hexadecimal Numbers Working with the C Language Tasks in Lab 1 1

FEEDBACK CONTROL Automatic control: Ensuring that one or more variables of interest are controlled to desired values Examples Cruise control Temperature control Elevator control The variables of interest are called outputs Control inputs are utilized for achieving automatic control Disturbance inputs might act on the system The power of feedback Explain Sensors and actuators BINARY TO DECIMAL CONVERSION Binary Decimal 1 0 10 1 x 0 x 01 1 0 0 x 1x 1 101 1 0 1 x 0 x 1x 5 MSB LSB

BINARY NUMBERS Why are binary numbers needed in programming with data acquisition systems? The digital electronics in computers inherently store data as binary numbers Counters, analog-to-digital-converters, and other systems on a data acquisition card measure electrical signals and convert them to binary numbers Low cost embedded processors and microcontrollers do not have floating point units It is computationally efficient to deal entirely with data in binary format DECIMAL TO BINARY CONVERSION Convert decimal number 104 to binary 104 Integer Remainder 104/ 5 0 5/ 6 0 6/ 13 0 13/ 6 1 6/ 3 0 3/ 1 1 1/ 0 1 Answer: 1101000 1x 6 1x 5 1x 3 64 3 8 104 3

HEXADECIMAL TO DECIMAL CONVERSION Hexadecimal numbers: Representation in terms of powers of 16 0 1 3 4 5 6 7 8 9 10 11 1 13 14 15 Decimal 0 1 3 4 5 6 7 8 9 A B C D E F Hexadecimal Hexadecimal Decimal 10 1 x 1 0 16 16 16 F1 1 0 41 1 1 15 x 16 1x 16 B 1 0 433 1 x 16 11x 16 1x 16 DECIMAL TO HEXADECIMAL CONVERSION Convert decimal number 104 to hexadecimal 104 Integer Remainder 104/16 6 8 6/16 0 6 Answer: 68 1 6 x 16 8 x 16 0 96 8 104 4

HEXADECIMAL TO BINARY One hexadecimal digit gets replaced by 4 binary digits Decimal: 104 Hexadecimal: 68 6: 0110 8: 1000 Binary: 0110 1000 ADDITION OF BINARY NUMBERS Rules: 0 + 0 = 0 1 + 0 = 1 1 + 1 = 10 i.e. 0 + carry 1 1 + 1 + 1 = 11 i.e. 1 + carry 1 5

ADDITION: EXAMPLE Example 14 + 19 = 33 14: 0 1110 19: 1 0011 10 0001 1x 5 0 1x 3 1 33 SUBTRACTION OF BINARY NUMBERS Rules: 0-0 = 0 1-0 = 1 1-1 = 0 0-1 = 10 1 + borrow = 1 + borrow 6

SUBTRACTION: EXAMPLE Example 7-14 = 13 7: 1 1011 14: 0 1110 0 1101 1x 3 1x 1x 0 8 4 1 13 SIGNED NUMBERS A positive number has 0 as the most significant bit A negative number has 1 as the most significant bit + 15: 0 1111-15:?? 1 1111????? NO 0 1111 1 1111 10 1110 0 7

SIGNED NUMBERS Need to use the s complement to represent a negative number s complement First take 1 s complement +15: 0 1111 1 s complement: 1 0000 Then add 1 + 0 0001 Hence -15: 1 0001 1 0001 SIGNED NUMBERS Check +15: 0 1111-15: 1 0001 10 0000 OK! 8

INTERPRETING SIGNED NUMBERS What is 1 0001? Answer: From the MSB, it is clear that it is a negative number Therefore, first subtract 1 1 0001 0 0001 1 0000 Then take 1 s complement 0 1111 Hence the number is -15! SIGNED NUMBERS: EXAMPLE Find 18435 in hexadecimal Step 1: First find +18435 in hexadecimal 18435 Integer Remainder 18435/16 115 3 115/16 7 0 7/16 4 8 4/16 0 4 Hence the hexadecimal equivalent of + 18435 is 4803 9

SIGNED NUMBERS: EXAMPLE Step : Convert to binary 4803 in binary is 0100 1000 0000 0011 Step 3: Take 1 s complement 1011 0111 1111 1100 SIGNED NUMBERS: EXAMPLE Step 4: Add 1 1011 0111 1111 1100 1 1011 0111 1111 1101 Step 5: Convert back to hexadecimal B 7 F D Final answer: B7FD 10

INTRODUCTION TO C Example Program #include <stdio.h> void main() { float x, y; printf("\nenter number"); scanf("%f",&x); printf("\n%f\n",x); y = x*x; printf("\nhere is the square of the number:%f\n\n",y); } printf("enter the number whose square you wish to find (q to quit)\n"); while(scanf("%f",&x) == 1) { printf("\nx=%f",x); y = x*x; printf("\ny=%f",y); printf("\nenter the number whose square you wish to find (q to quit)\n"); } FOR LOOPS Syntax for (k=0; k<5; k++) { printf( %d\n,k); } While loop syntax Covered last week while(..) {. } 11

ARRAYS Example: Declaration of an array The line int count[10]; declares count to be an integer array of size 10. In C, all arrays start with slot 0 (rather than slot 1) Hence, the first element in the array count is called count[0] The last element in the array count is called count[9] ARRAYS An array can be declared with no initial values Then its initial values are unpredictable If an initializer is given, then array length may be omitted from the square brackets in the declaration statement Example: int count[] = {3,18,4,7,6,5,9,1,,3}; Note that the size of the array count is not explicitly declared. However, because of the initializer, count will be an array of size 10 1

ARRAYS The elements of an array can be accessed in ways Using subscripts Using pointers We will use subscripts in this lab. Pointers will be covered in the next lab. Example: Write a program to reverse order the elements of an array Note: Task in lab is to arrange the elements of an array in ascending order ORDER ARRAY ELEMENTS Determine i th element, i =0,1,,.,n =0 =1 i i Find smallest number among elements i, i +1, i+,.,n Place the smallest number in the i th element This can be done using for loops i =0,1,,3,..,n j =i+1,i+,i+3,..,n i j =i+1,i+,.,n Compare i th element with j th element and swap if j th element is smaller 13

TASKS IN LAB Tasks are described in detail in handout Task 1 Hello World example Task Check addition of hexadecimal numbers from pre-lab Task 3 Write a program to take in user input temperature and convert temperature in Fahrenheit to Celsius Task 4 Sorting elements in an array Task 5 Introduction to Matlab, plotting Important: Pre-labs, Post-labs 14