Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP 21000 Spring 2014 C Part V.

Slides:



Advertisements
Similar presentations
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Advertisements

CSCE 212 Computer Organization Lecture 2 Data Representation.
Bits and Bytes CS 213 Aug. 27, 1998 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and.
Bits and Bytes January 17, 2002 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and strings.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
The little language that could Remember C is a “small language”
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include #include int main ( ) { printf( "%d\n", 455 ); printf( "%d\n", 455 ); printf(
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
CS 270: Computer Organization Bits, Bytes, and Integers
Bits and Bytes Spring, 2015 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
File IO and command line input CSE 2451 Rong Shi.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Carnegie Mellon 1 This week: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers  Representation: unsigned and.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CS429 Computer Architecture Topics Simple C program Basic structure, functions, separate files Compilation Phases, options Assembler GNU style, byte ordering,
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Online Judge System Tom Chao Zhou CSC2100B Data Structures Tutorial 2.
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2016 C Part IV.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
A bit of C programming Lecture 3 Uli Raich.
Instructor: David Ferry
Command Line Arguments
Number Systems and Bitwise Operation
Introduction to Programming and the C Language
Variables, Types and Expressions
توابع ورودي-خروجي.
Variables, Types and Expressions
Chapter 14 Bitwise Operators Objectives
Enumerations.
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
Introduction to Computer Organization & Systems
Comp Org & Assembly Lang
EECE.2160 ECE Application Programming
Comp Org & Assembly Lang
Bitwise operators.
Character Arrays char string1[] = “first”;
“The Class That Gives CMU Its Zip!”
Bitwise Operators.
“The Class That Gives CMU Its Zip!”
Introduction to Computer Organization & Systems
Presentation transcript:

Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V

Command Line int main(int argc, char *argv[ ]) { int j; if (argc != 2) { printf("factorial takes one integer argument\n"); return(1); /* abnormal termination. */ } /* ASCII string to integer conversion */ j = atoi(argv[1]); printf("factorial(%d) = %d\n", j, factorial(j)); return(0); } 2-2 Argc indicates the number of command line arguments (including the program name). Argv is an array of pointers to char (strings) that lists all of the command line arguments.

Command Line #include int factorial (int n) { int ans = 1; while (n > 1) { ans *= n; n = n - 1; } return(ans); } 2-3

Bitwise operators in C 1 #include 2 #include main() 6 { 7 int x, y; 8 9 printf("Enter two numbers: "); 10 scanf("%d %d", &x, &y); printf ("%d AND %d gives %d\n\n", x, y, x & y); 13 printf ("%d OR %d gives %d\n\n", x, y, x | y); 14 printf ("%d Exclusive OR %d gives %d\n\n", x, y, x ^ y); 15 printf ("%d shifted left 2 places gives %d\n\n", x, x << 2); 16 printf ("%d shifted right 2 places gives %d\n\n", x, x >> 2); 17 printf ("%d complemented gives %d\n\n", x, ~x); 18 } 2-4

Masking int theMask = 0x000000FF; // could also do #define theMask 0x000000FF a = 15215; b = a << 2; printf("Shifting by 2 bits: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); b = a & theMask; printf("Masking out everything but the last byte: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); 2-5

Masking b = a & theMask; b = b << 8; printf("Masking out the last byte and then shifting 8 bits: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); } 2-6

Masking #include typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) /* with some compilers you need to put in the first 0x */ //printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } int main(int argc, char* argv[]) { int a, b; int theMask = 0x000000FF; 2-7

Octal Numbers octal1.c #include #define DIG 20 void octal_print(int x) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ { putchar ('-'); /* output a minus sign */ x = - x; } do { od = x % 8; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/8) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ do { putchar(s[--i]); /* output characters on stdout */ } while ( i > 0 ); putchar ('\n'); } 2-8

Octal Numbers octal1.c int main() { int x; octal_print(0); /* result: 00 */ octal_print(-1); /* result: -01 */ octal_print(123456); /* result: */ octal_print( ); /* result: */ octal_print(999); /* result: */ octal_print(-999); /* result: */ octal_print(1978); /* result: */ printf("enter a number or -1 to quit: "); scanf("%d", &x); while (x != -1){ octal_print(x); printf("\n"); printf("enter a number of -1 to quit: "); scanf("%d",&x); } 2-9

Any Base convert.c #include #define DIG 20 void octal_print(int x, int base) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ { putchar ('-'); /* output a minus sign */ x = - x; } do { od = x % base; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/base) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ do { putchar(s[--i]); /* output characters on stdout */ } while ( i > 0 ); putchar ('\n'); } 2-10

Octal Numbers octal2.c #include #define LOWDIGIT 07 #define DIG 20 void octal_print(register int x) { register int i = 0; char s[DIG]; if ( x < 0 ){ /* treat negative x */ putchar ('-'); /* output a minus sign */ x = - x; } do{ s[i++] = ((x & LOWDIGIT) + '0'); /* mod performed with & */ } while ( ( x >>= 3) > 0 ); /* divide x by 8 via shift */ putchar('0'); /* octal number prefix */ do{ putchar(s[--i]); /* output characters on s */ } while ( i > 0 ); putchar ('\n'); } 2-11

Octal Numbers octal2.c int main() { int x; octal_print(0); /* result: 00 */ octal_print(-1); /* result: -01 */ octal_print(123456); /* result: */ octal_print( ); /* result: */ octal_print( ); /* result: (0 indicates octal)*/ octal_print( ); /* result: */ octal_print(999); /* result: */ octal_print(-999); /* result: */ octal_print(1978); /* result: */ printf("enter a number or -1 to quit: "); scanf("%d", &x); while (x != -1){ octal_print(x); printf("\n"); printf("enter a number of -1 to quit: "); scanf("%d",&x); } 2-12