C-string format with scanf/printf

Similar documents
C-string format with scanf/printf

Programming Project 2

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

World Inside a Computer is Binary

Beginning C Programming for Engineers

Computer Organization & Systems Exam I Example Questions

Logical and Bitwise Expressions

JAVA OPERATORS GENERAL

A flow chart is a graphical or symbolic representation of a process.

Operators. Java operators are classified into three categories:

Computers Programming Course 6. Iulian Năstac

SECTION II: LANGUAGE BASICS

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

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

CSCI 2212: Intermediate Programming / C Chapter 15

Chapter 3 Basic Data Types. Lecture 3 1

CS 253. January 14, 2017

CS 241 Data Organization Binary

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

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

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

Chapter 3: Operators, Expressions and Type Conversion

Lecture 02 C FUNDAMENTALS

Programming in C++ 5. Integral data types

Where we are going (today)

Programming. Syntax and Semantics

Operators in java Operator operands.

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

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Java Basic Datatypees

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

Part I Part 1 Expressions

Number System. Introduction. Decimal Numbers

Types, Variables, and Constants

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Reserved Words and Identifiers

Expressions and Precedence. Last updated 12/10/18

Chapter 4. Operations on Data

Logical and Bitwise Expressions

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Standard Boolean Forms

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

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

UNIT- 3 Introduction to C++

4. Number Representations

Bits, Words, and Integers

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

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

The Arithmetic Operators

COMP2611: Computer Organization. Data Representation

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

SIGNED AND UNSIGNED SYSTEMS

Chapter 7. Additional Control Structures

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

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

A Java program contains at least one class definition.

AGENDA Binary Operations CS 3330 Samira Khan

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

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

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

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

7/25/2016. Example: Addition of Unsigned Bit Patterns. ECE 120: Introduction to Computing. Adding Two Non-Negative Patterns Can Overflow

Bits, Bytes and Integers

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Where we are going (today)

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

ESc101 : Fundamental of Computing

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

Overview. ELEC2041 Microprocessors and Interfacing. Lecture 7: Number Systems - II. March 2006.

Arithmetic and Bitwise Operations on Binary Data

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

Programming. Elementary Concepts

Chapter 12 Variables and Operators

DEPARTMENT OF MATHS, MJ COLLEGE

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

Arithmetic Operators. Portability: Printing Numbers

Bit Manipulation in C

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

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

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

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

Conditional Statement

Lecture 13 Bit Operations

Logic, Words, and Integers

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 11 C track: lecture 8

Computer System and programming in C

Tutorial 1: C-Language

Fundamentals of Programming

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Applied Computer Programming

Expression and Operator

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Memory, Data, & Addressing II CSE 351 Spring

Transcription:

CSI333 Lecture 4

C-string format with scanf/printf char mycstring[4]; int intvar; scanf("%3s", &intvar ); /*reads up to 3 chars and stores them PLUS \0 in the 4-byte var. intvar*/ scanf("%3s", mycstring); /*DITTO into the 4-byte byte array*/ /* DIFFERENT from other output formats! */ printf("%s", &intvar); /* Print a C-String! */ printf("%s", mycstring); /* Another C-string */ C strings are null-terminated char arrays They go by the address of their first char. In C/C++, with array char mycharray[56]; mychararray (no brackets!) denotes the (const) ADDRESS OF the first character. mycharray is equivalent to &mycharray[0]

Bit - Binary Digit Basic Unit of Information stored and manipulated in our computers. Computer 1 hardware stores & manipulates & transmits all data digitally: which means with on/off, 0/1, true/false (usually) electrical 2 signals. A bit (binary digit) is a single unit of memory or transmission that can have only 2 possible values. 1 and much other popular electronic product 2 also optical and magnetic

A representation is a coding scheme to give meaning to bit strings (sequences). Different kinds of data are each represented by different meanings we give to sequences of bits. Some hardware (printers, keyboards e.g.) embodies particular representations: 01000010 makes standard printers print B The base or radix 2 (binary) number system is used by present computers to represent non-negative integers.

Example: 0001 1100 1000 0110 0 2 15 + 0 2 14 + 0 2 13 + 1 2 12 +1 2 11 + 1 2 10 + 0 2 9 + 0 2 8 +1 2 7 + 0 2 6 + 0 2 5 + 0 2 4 +0 2 3 + 1 2 2 + 1 2 1 + 0 2 0 = (0+0+0+4096)+(2048+1024+0+0)+(256+0+0+0)+(0+4+2+0) = 7302

Computer Experts all know their powers of 2: 2 1 2 2 11 2048 = 2Ki 2 2 4 2 12 4096 = 4Ki 2 3 8 2 13 8192 = 8Ki 2 4 16ten = 0x10 2 14 16, 384 = 16Ki 2 5 32ten = 0x20 2 15 32, 768 = 32Ki 2 6 64ten = 0x40 2 16 65, 536 = 64Ki 2 7 128ten = 0x80 2 8 256 2 9 512 2 10 1024 = 1kibi = 1Ki 2 20 1, 048, 576 = 1Mi 2 30 1, 073, 741, 824 = 1Gi 10 6 10 9 Thus 1024 bytes of storage is officially a kibibyte, not a kilobyte. However, computer professionals generally dislike this unit (they say it sounds like a cat food) so the ambiguity in the size of a kilobyte persists. The prefix is a contraction of kilobinary. The symbol Ki-, rather than ki-, was chosen for uniformity with the other binary

How to convert a number binary; SAME IDEA for converting a number to English! Let X be a variable initialized with the number. Repeat until done: 1. If X==0 write the remaining bits and go home. Otherwise, guess or figure out (by dividing) the largest power of 2 that fits Specifically, what is the largest power of two which is less than or equal to X? 2. Write zeros for any bits that should be zero. (for English, write nothing.) Write the bit for that power of 2. 3. Compute X = X -(that power of 2). Example? 7302.

How to access individual bits in C/C++/Java (1) Bitwise Logical Operators (2) Shift operators Bitwise Logical Operators apply to integer type objects (char, short, int, long int and their unsigned counterparts 3 ) and give integer results defined in terms of bits. Advice: Use C/C++ unsigned long int for bitstrings when 32 bits are needed. (long guarantees 32 bits from ANSI C.) All unsigned types use straight binary notation, regardless of whether the signed types use 2-s complement, 1-s compl. or sign magnitude... the sign bit is treated as an ordinary bit. (Harbison & Steele) 4 3 also bool after conversion to int 4 C/C++ on general purpose computers uses 2-s complement integers, but other sign representations are possible.

Since the LC-3 Instruction Set Architecture (1) uses (like all others) bit fields in the machine language code, (2) and has bitwise operations, C++ bitwise operations are useful to simulate LC-3 in C++/C/Java. First, single bit Boolean or truth value operations: Unary NOT (complement): (also denoted by, x, ) x NOT(x) 0 1 1 0 Binary AND, (inclusive) OR, EXCLUSIVE OR : (Binary means TWO OPERANDS here) x y x AND y x OR y x XOR y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Other Symbols:, &,,, + eor,

Other names for logic values: 1: true, on, asserted, enabled 0: false, off, deasserted, disabled on, asserted, enabled etc. are popular in electrical and computer engineering circles.

C++ Bitwise Operators apply Boolean operations to the individual bits of the binary integer representations. unsigned char X = 0x0C; // 0000 1100 (binary) unsigned char Y = 0x0A; // 0000 1010 (X & Y) == 0x04 // 0000 1000 AND (X Y) == 0x0E // 0000 1110 OR (X ^ Y) == 0x06 // 0000 0110 EOR (~ X) == 0xF3 // 1111 0011 COMPL

Caution: It s a common bug to confuse Bitwise operations with Logical AND, OR, NOT: (X && Y) == 1 if X!=0 and Y!=0, 0 otherwise (X Y) == 1 if X!=0 or Y!=0, 0 otherwise (! X) == 0 if X!=0, 1 if X==0 In C/C++, consider any non-zero int or pointer as true, and 0 as false. Many C programmers write #define true 1 and #define false 0 In C++/Java, true and false are literals (constants) of type bool (boolean in Java). (In C++, non-zero ints or pointers are converted to bool true, zero converts to false, true converts to 1 and false to 0).

Shifts For integral X, AMT, AMT 0 X << AMT is X shifted Left AMT bit positions. Zero bits are shifted in from the right. Example: (0x000F8001 << 3) == 0x007C0008 For unsigned X or X 0 X >> AMT is X shifted Right AMT bit positions. Zero bits are shifted in from the left. Example: (0x007C0008 >> 3) == 0x000F8001 For signed X, X < 0, in X >> AMT, whether 0s or 1s are shifted in is IMPLEMENTATION DEPENDENT!!

How to access individual bits in C/C++/Java Bitwise operations & Bitwise AND int I; 0x8000000 & I equals 0 if the top-order bit (bit 31) is 0; equals 0x80000000 0 if that bit is 1. Bitwise OR I = I 0x8000000; makes bit 31 of I become (or stay) 1. It sets bit 31.

How to access individual bits in C/C++/Java Bitwise operations & Bitwise AND int I; 0x8000000 & I equals 0 if the top-order bit (bit 31) is 0; equals 0x80000000 0 if that bit is 1. Bitwise OR I = I 0x8000000; makes bit 31 of I become (or stay) 1. It sets bit 31. What does I = I & 0x7FFFFFFF; do?

How to access individual bits in C/C++/Java Bitwise operations & Bitwise AND int I; 0x8000000 & I equals 0 if the top-order bit (bit 31) is 0; equals 0x80000000 0 if that bit is 1. Bitwise OR I = I 0x8000000; makes bit 31 of I become (or stay) 1. It sets bit 31. What does I = I & 0x7FFFFFFF; do? clears bit 31. ~ Bitwise NOT ~0x7FFFFFFF =?

How to access individual bits in C/C++/Java Bitwise operations & Bitwise AND int I; 0x8000000 & I equals 0 if the top-order bit (bit 31) is 0; equals 0x80000000 0 if that bit is 1. Bitwise OR I = I 0x8000000; makes bit 31 of I become (or stay) 1. It sets bit 31. What does I = I & 0x7FFFFFFF; do? clears bit 31. ~ Bitwise NOT ~0x7FFFFFFF =? 0x80000000 Bit-shift operations I << k SHIFTS the bits LEFT k positions. k can be constant or variable. I >> k Guess what?

Bit shifts in Java << Left-shift (as in C and C++) >> Signed Right-shift. >>> Unsigned Right-shift. The ambiguity allowed in standard C/C++ on whether X >>... on signed integral types X is does sign-extension or not has been removed by the Java designers. I n Java, all integral types are signed (8-bit byte, 16-bit short, 32-bit int, and 64-bit long) except for the 16-bit char type.