INFO-I308 Laboratory Session #2

Similar documents
LAB 4 Extending Signed and Unsigned Numbers

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

Beginning C Programming for Engineers

Computer Organization & Systems Exam I Example Questions

THE FUNDAMENTAL DATA TYPES

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

2.2: Bitwise Logical Operations

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

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

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

Tutorial 1: C-Language

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

OBJECT ORIENTED PROGRAMMING

Programming, numerics and optimization

Tutorial-2a: First steps with C++ programming

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

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

Chapter 3 Basic Data Types. Lecture 3 1

Program Organization and Comments

Standard I/O in C and C++

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

Integer Data Types. Data Type. Data Types. int, short int, long int

ISA 563 : Fundamentals of Systems Programming

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS 241 Data Organization Binary

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Lab # 02. Basic Elements of C++ _ Part1

Objects and streams and files CS427: Elements of Software Engineering

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

EEE145 Computer Programming

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

Chapter 2: Introduction to C++

/* defines are mostly used to declare constants */ #define MAX_ITER 10 // max number of iterations

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Getting started with C++ (Part 2)

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

BITG 1233: Introduction to C++

WARM UP LESSONS BARE BASICS

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Getting started with Java

Fall 2017 CISC124 9/16/2017

Computer Programming. Dr. Fahad Computer Science Deptt.

The C++ Language. Arizona State University 1

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS2141 Software Development using C/C++ C++ Basics

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

SIGNED AND UNSIGNED SYSTEMS

Few reminders and demos

LECTURE 02 INTRODUCTION TO C++

Lecture 13 Bit Operations

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

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

COMP322 - Introduction to C++ Lecture 01 - Introduction

Integer Representation Floating point Representation Other data types

printf( Please enter another number: ); scanf( %d, &num2);

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

School of Information Technology and Computer Science CSCI192 - Spring Session Revision

CSE 333 Lecture 9 - intro to C++

UNIT- 3 Introduction to C++

Internal representation. Bitwise operators

Chapter Two MULTIPLE CHOICE

LAB 5 Arithmetic Operations Simple Calculator

Programming refresher and intro to C programming

Internal representation. Bitwise operators

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Internal representation. Bitwise operators

Chapter 3. Fundamental Data Types

Fundamentals of Programming CS-110. Lecture 2

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

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

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Computer Systems Programming. Practice Midterm. Name:

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Pointers and Strings Prentice Hall, Inc. All rights reserved.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

AN OVERVIEW OF C++ 1

ECET 264 C Programming Language with Applications

Lab 8 The Stack (LIFO) Structure

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

I/O Streams and Standard I/O Devices (cont d.)

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Midterm Exam Review Slides

Work relative to other classes

C++ For Science and Engineering Lecture 15

Transcription:

INFO-I308 Laboratory Session #2 LAB GOALS Understanding datatypes and bitwise operators Step 1: Start with a simple C++ console project called I308_Lab2. Step 2: Add a new (main.cpp) item (file) under the source files. 1

Step 3: The first thing we want to do is to understand our basic data types and how they are constructed in our compiler. Create the following program and run it. (Make sure to include the necessary header files, and namespaces) #include <iostream> #include <string> #include <iomanip> using namespace std; void main() unsigned char clearchar; cout << "====Understanding your Compiler and Datatypes====" << endl; cout << "Size of char: " << sizeof(char) << endl; cout << "Size of unsigned char: " << sizeof(unsigned char) << endl; cout << "Size of unicode widechar: " << sizeof(wchar_t) << endl; cout << "Size of int: " << sizeof(int) << endl; cout << "Size of float: " << sizeof(float) << endl; cout << "Size of double: " << sizeof(double) << endl; getchar(); Step 4: The next thing we should learn is to display information in different formats. This will come in quite handy as we get into lower-level programming. Sure you have a debugger which may be able to show you this data. But you should really know how to do this yourself, and without a debugger! Add the following code to your main program. cout << "==============Displaying Information in Different Formats====================" << endl; unsigned int anum; anum = 65; cout << "------------ anum (int) --------------" << endl; cout << "char format = " << char(anum) << endl; cout << "decimal format = " << std::dec << (anum) << endl; cout << "Octal format = " << std::oct << (anum) << endl; cout << "HEX format = " << std::hex << (anum) << endl; Run the above to see the results. Now add the following code as well and re-run the code. This should have the same results (however using the printf() language which is inherited from the C language. printf("oct format(c) = %c \n",anum); // C style printf printing Char number. printf("hex format(c) = %d \n",anum); // C style printf printing Decimal number. printf("oct format(c) = %o \n",anum); // C style printf printing OCTal number. printf("hex format(c) = %x \n",anum); // C style printf printing HEXadecimal number. 2

Step 5: Now let s create a function to display the actual bit pattern of our data. We do this in 3 steps. 1) Add a forward declaration for a function int_to_binary() before the main function. This function will take an unsigned integer value and converts it in to any array of characters (string) and returns it. string int_to_binary(unsigned int value); // Convert Unsigned integer into a string of bits.. string int_to_binary(unsigned int value) char theresult[128]; unsigned int mask = 0x80000000; int i; for (i = 0; i< sizeof(value) * 8; i++) if ((value & mask) == 0) theresult[i] = '0'; else theresult[i] = '1'; mask >>= 1; theresult[i] ='\0'; string result = theresult; return(result); 2) Add the actual int_to_binary() after your main() function. 3) In the main() function, call the int_to_binary() function with the integer value of anum. cout << "Binary format = " << int_to_binary(anum) << endl; #include <iostream> #include <string> #include <iomanip> using namespace std; // Forward declarations: string int_to_binary(unsigned int value); void main() // add the new code above the getchar() function cout << "==============Displaying Information in Different Formats====================" << endl; unsigned int anum; anum = 65; cout << "------------ anum (int) --------------" << endl; cout << "Char format(c++) = " << char(anum) << endl; cout << "Dec format(c++) = " << std::dec << (anum) << endl; cout << "Oct format(c++) = " << std::oct << (anum) << endl; cout << "HEX format(c++) = " << std::hex << (anum) << endl; printf("char format(c) = %c \n",anum); // C style printf printing Char number. printf("dec format(c) = %d \n",anum); // C style printf printing Decimal number. printf("oct format(c) = %o \n",anum); // C style printf printing OCTal number. printf("hex format(c) = %x \n",anum); // C style printf printing HEXadecimal number. cout << "Binary format = " << int_to_binary(anum) << endl; getchar(); 3

Now run the program again to see the results: Now that you see how one can look at the data in its different representations, why don t you try to do the same thing for Char data type. See if you can also print the bit representation for it. You may have to make a new copy of the int_to_binary() and modify it a little to accommodate char_to_binary(); See below for a little clue! string char_to_binary(unsigned char value) char theresult[128]; unsigned char mask = 0x80; int i; for (i = 0; i< sizeof(value) * 8; i++) if ((value & mask) == 0) theresult[i] = '0'; else theresult[i] = '1'; mask >>= 1; theresult[i] ='\0'; string result = theresult; return(result); Go ahead and add the above function to your program. Then make sure that you also include a forward declaration for it about the main() function. We ll use this function later on. 4

Step 6: The next thing we want to do is to understand the bitwise operators. These include the (shift-left <<), (shift-right >>), (bitwise-or ), (bitwise-and &), (bitwise-xor ^), (bitwise-not!) and (bitwise-complement ~). To test the SHIFT operators, add the following code above the getchar(); cout << "==============Understanding Bitwise Shift Operations====================" << endl; unsigned char clearchar; clearchar = 0x01; cout << "0x01 in Binary format clearchar = (clearchar << 2); cout << "Shift left 2 bits clearchar = (clearchar >> 1); cout << "Shift right 1 bit clearchar = (clearchar << 6); cout << "Shift left 6 bits Compile and run the program to make sure the above steps are working properly. Step 7: Now to test the OR operator add the following code (above the getchar() line) and run your program. cout << "==================== OR Operation=========================" << endl; clearchar = 0x01; cout << "0000 0001 00001100 = " << (clearchar 0x0C ) << endl; cout << "Hex: " << std::hex << (clearchar 0x0C ) << endl; cout << "Binary Result: " << char_to_binary(clearchar 0x0C ) << endl; Step 8: Now to test the AND operator add the following code (above the getchar() line) and run your program. cout << "=====================AND Operation========================" << endl; clearchar = 0xff; cout << "1111 1111 & 00001100 = " << (clearchar & 0x0C ) << endl; cout << "Hex: " << std::hex << (clearchar & 0x0C ) << endl; cout << "Binary Result: " << char_to_binary(clearchar & 0x0C ) << endl; 5

Step 9: Now to test the XOR operator add the following code (above the getchar() line) and run your program. cout << "=====================XOR Operation========================" << endl; clearchar = 0x09; cout << "0000 1001 ^ 0000 1100 = " << char_to_binary(clearchar ^ 0x0C ) << endl; cout << "=================Double XOR Operation=====================" << endl; cout << "Back to the original char: " << endl; cout << char_to_binary(clearchar) << " ^ 0000 1100 ^ 0000 1100= " << char_to_binary(clearchar ^ 0x0C ^ 0x0C) << endl; 6

Step 10: Now to test the NOT operator add the following code (above the getchar() line) and run your program. cout << "=====================NOT Operation========================" << endl; clearchar = 0x05; // a non-zero number cout << "~0000 1001 = " << char_to_binary(!clearchar) << endl; clearchar = 0x00; // a zero cout << "~0000 0000 = " << char_to_binary(!clearchar) << endl; Step 11: Now to test the COMPLEMENT operator add the following code (above the getchar() line) and run your program. cout << "=====================Complement Operation========================" << endl; clearchar = 0x09; cout << "~0000 1001 = " << char_to_binary(~clearchar) << endl; Optional: If you are still interested to learn more, perhaps about 32bit floating point numbers in VC++ and how their bit patterns are organized, see below: 7

8

9

// // INFO-I 308 Information Representation // Proof of concept exersize for assignment 4 // Understanding and use of bitwise operators. // // Hossein Hakimzadeh // 3/3/2014 // #include <iostream> #include <string> #include <iomanip> using namespace std; // Forward declarations: string char_to_binary(unsigned char value); string int_to_binary(unsigned int value); string f32_to_binary(float value); unsigned int ChangeEndian32(unsigned int value); // Change byte order void main() cout << "====Understanding your Compiler and Datatypes====" << endl; cout << "Size of char: " << sizeof(char) << endl; cout << "Size of unsigned char: " << sizeof(unsigned char) << endl; cout << "Size of unicode widechar: " << sizeof(wchar_t) << endl; cout << "Size of int: " << sizeof(int) << endl; cout << "Size of float: " << sizeof(float) << endl; cout << "Size of double: " << sizeof(double) << endl; cout << "==============Displaying Information in Different Formats====================" << endl; unsigned int anum; anum = 65; cout << "------------ anum (int) --------------" << endl; //std::cout.flags ( std::ios::showbase ); cout << "Char format(c++) = " << char(anum) << endl; cout << "Dec format(c++) = " << std::dec << (anum) << endl; cout << "Oct format(c++) = " << std::oct << (anum) << endl; cout << "HEX format(c++) = " << std::hex << (anum) << endl; printf("char format(c) = %c \n",anum); // C style printf printing Char number. printf("dec format(c) = %d \n",anum); // C style printf printing Decimal number. printf("oct format(c) = %o \n",anum); // C style printf printing OCTal number. printf("hex format(c) = %x \n",anum); // C style printf printing HEXadecimal number. cout << "Binary format = " << int_to_binary(anum) << endl; // Now we try the same thing with a char unsigned char achar; achar = 'A'; cout << "------------ achar --------------" << endl; //std::cout.flags ( std::ios::showbase ); cout << "char format(c++) = " << char(anum) << endl; cout << "Dec format(c++) = " << int(achar) << endl; cout << "Octal format(c++) = " << std::oct << int(achar) << endl; cout << "HEX format(c++) = " << std::hex << int(achar) << endl; cout << "Binary format = " << char_to_binary(achar) << endl; printf("char format(c) = %c \n", achar); // C style printf printing Char number. printf("dec format(c) = %d \n", achar); // C style printf printing Decimal number. printf("oct format(c) = %o \n", achar); // C style printf printing OCTal number. printf("hex format(c) = %x \n", achar); // C style printf printing HEXadecimal number. // Now we try the same thing with a float float afloat; afloat = 2.0; 10

afloat = -2.0; afloat = 4.0; afloat = 6.0; afloat = 1; afloat = 0.75; afloat = 2.5; afloat = 0.1; afloat = 0.75; cout << "==============Understanding Bitwise Shift Operations====================" << endl; unsigned char clearchar; clearchar = 0x01; cout << "0x01 in Binary format clearchar = (clearchar << 2); cout << "Shift left 2 bits clearchar = (clearchar >> 1); cout << "Shift right 1 bit clearchar = (clearchar << 6); cout << "Shift left 6 bits cout << "====================Shifting left..=========================" << endl; clearchar = 0x01; for(int i = 0; i< sizeof(unsigned char) * 8; i++) cout << std::setw(4) << int(clearchar << i) << " [Binary format = " << char_to_binary(clearchar << i) << "]" << endl; cout << "====================Shifting right.=========================" << endl; clearchar = 0x80; for(int i = 0; i< sizeof(unsigned char) * 8; i++) cout << std::setw(4) << int(clearchar >> i) << " [Binary format = " << char_to_binary(clearchar >> i) << "]" <<endl; cout << "==================== OR Operation=========================" << endl; clearchar = 0x01; cout << "0000 0001 00001100 = " << (clearchar 0x0C ) << endl; cout << "Hex: " << std::hex << (clearchar 0x0C ) << endl; cout << "Binary Result: " << char_to_binary(clearchar 0x0C ) << endl; cout << "=====================AND Operation========================" << endl; 11

clearchar = 0xff; cout << "1111 1111 & 00001100 = " << (clearchar & 0x0C ) << endl; cout << "Hex: " << std::hex << (clearchar & 0x0C ) << endl; cout << "Binary Result: " << char_to_binary(clearchar & 0x0C ) << endl; cout << "=====================XOR Operation========================" << endl; clearchar = 0x09; cout << "0000 1001 ^ 0000 1100 = " << char_to_binary(clearchar ^ 0x0C ) << endl; cout << "=================Double XOR Operation=====================" << endl; cout << "Back to the original char: " << endl; cout << char_to_binary(clearchar) << " ^ 0000 1100 ^ 0000 1100= " << char_to_binary(clearchar ^ 0x0C ^ 0x0C) << endl; cout << "=====================NOT Operation========================" << endl; clearchar = 0x05; // a non-zero number cout << "~0000 1001 = " << char_to_binary(!clearchar) << endl; clearchar = 0x00; // a zero cout << "~0000 0000 = " << char_to_binary(!clearchar) << endl; cout << "=====================Complement Operation========================" << endl; clearchar = 0x09; /**/ cout << "~0000 1001 = " << char_to_binary(~clearchar) << endl; getchar(); string char_to_binary(unsigned char value) char theresult[128]; unsigned char mask = 0x80; int i; for (i = 0; i< sizeof(value) * 8; i++) if ((value & mask) == 0) theresult[i] = '0'; else theresult[i] = '1'; mask >>= 1; theresult[i] ='\0'; string result = theresult; return(result); string int_to_binary(unsigned int value) char theresult[128]; unsigned int mask = 0x80000000; int i; for (i = 0; i< sizeof(value) * 8; i++) if ((value & mask) == 0) theresult[i] = '0'; else theresult[i] = '1'; mask >>= 1; theresult[i] ='\0'; string result = theresult; return(result); //---------------------------------------------------------------------- // The bit pattern for float (4byte) was verified using the following // Microsoft web site: // https://msdn.microsoft.com/en-us/library/0b34tf65%28d=printer%29.aspx // // IEEE Floating-Point Representation (Visual Studio 2015) // // Note this function will not work for DOUBLE (64 bit) double 12

// precision floating point numbers. string f32_to_binary(float value) cout << "Float value = " << value <<endl; //----------------------------------------------------------- // Changing the byteorder for float appears to be necessary. // otherwise printing the bits produces incorrect byte order. // Also we need to cast the float into a unsigned int before // sending it to the ChangeEndian32() function, because // bitwise operators can not be applied to float data type. unsigned int *intptr; intptr = (unsigned int *) &value; *intptr = ChangeEndian32(*intPtr); // change byte order //----------------------------------------------------------- unsigned char *startingbyte; startingbyte = (unsigned char *) &value; char theresult[128]; int j = 0; for(int numbytes =3; numbytes >=0; numbytes--) for (int numbits = 7; numbits >= 0; numbits--) if (( (( *startingbyte >> numbits ) & 1) ) == 1) theresult[j] = '1'; else theresult[j] = '0'; j++; //cout << (( *startingbyte >> numbits ) & 1); startingbyte++; //cout << endl; theresult[j] ='\0'; string result = theresult; return(result); unsigned int ChangeEndian32(unsigned int value) return ( ((value & 0xFF000000ul) >> 24) ((value & 0x00FF0000ul) >> 8) ((value & 0x0000FF00ul) << 8) ((value & 0x000000FFul) << 24) ); 13