Applied Computer Programming

Size: px
Start display at page:

Download "Applied Computer Programming"

Transcription

1 Applied Computer Programming Representation of Numbers. Bitwise Operators Course 07 Lect.eng. Adriana ALBU, PhD Politehnica University Timisoara

2 Internal representation All data, of any type, processed by a computer must have the same fundamental representation In order to work correctly with these data, a programmer should understand: the representation of numbers and their memory storage the fundamental manner a computer operates with numbers (bitwise operations)

3 Representation of numbers Binary Octal Hexadecimal

4 Representation of numbers Most of our programs don t need concern regarding operations at the bit level We re free to think in bytes, or integers and doubles, or even higher level data types composed of a combination of these But there are times when we'd like to be able to go to the level of an individual bit

5 Representation of numbers Any value (variable or constant) that is processed by our programs needs a memory space and it has a memory representation (sequence of bits) Bit unit of data storage may have one of two values: 0 or 1 usually is not individually addressable Byte unit of addressable data in all usual computer architectures it contains 8 bits (CHAR_BIT from limits.h specifies its dimension)

6 Representation of numbers The byte is the lowest level at which we can access data; there's no "bit" type, and we can't ask for an individual bit In fact, we can't even perform operations on a single bit every bitwise operator will be applied to, at a minimum, an entire byte at a time This means we'll be considering the whole representation of a number whenever we talk about applying a bitwise operators

7 Binary representation Binary numbers used in mathematics and digital electronics expressed in the binary numeral system (also called base-2 numeral system) use two different symbols: 1 (one) and 0 (zero) these two symbols may also have the meanings of true/false, yes/no, on/off the binary system is used internally by almost all modern computers and computer-based devices such as mobile phones

8 Binary representation Any number can be represented by a sequence of bits (binary digits), which in turn may be represented by any mechanism capable of being in two mutually exclusive states In a computer, the numeric values may be represented by two different voltages

9 Binary representation When written, binary numerals are often subscripted, prefixed or suffixed in order to indicate their base The following notations are equivalent: binary (explicit statement of format) b (a suffix indicating binary format) B (a suffix indicating binary format) bin (a prefix indicating binary format) (a subscript indicating base-2 (binary) notation) % (a prefix indicating binary format) 0b (a prefix indicating binary format, common in programming languages) 8b (a prefix indicating number of bits in binary format, common in programming languages)

10 Binary representation When spoken, binary numerals are usually read digit-by-digit, in order to distinguish them from decimal numerals the binary numeral 100 is pronounced one zero zero, rather than one hundred, to make its binary nature explicit, and for purposes of correctness since the binary numeral 100 represents the value four, it would be confusing to refer to the numeral as one hundred (a word that represents a completely different value, or amount) alternatively, the binary numeral 100 can be read out as "four" (the correct value), but this does not make its binary nature explicit

11 Counting in binary Counting in binary is similar to counting in any other number system Beginning with a single digit, counting proceeds through each symbol, in increasing order Before examining binary counting, it is useful to briefly discuss the more familiar decimal counting system as a frame of reference

12 Decimal counting Decimal counting uses the ten symbols 0 through 9 Counting primarily involves incremental manipulation of the "low-order" digit, or the rightmost digit, often called the "first digit When the available symbols for the low-order digit are exhausted, the next-higher-order digit (located one position to the left) is incremented, and counting in the low-order digit starts over at 0

13 Decimal counting In decimal, counting proceeds like so: 000, 001, 002, , 008, 009, (rightmost digit starts over, and next digit is incremented) 010, 011, 012, , 091, 092, , 098, 099, (rightmost two digits start over, and next digit is incremented) 100, 101, 102,... After a digit reaches 9, an increment resets it to 0, but also causes an increment of the next digit to the left

14 Binary counting In binary, counting follows similar procedure, except that only the two symbols 0 and 1 are used Thus, after a digit reaches 1 in binary, an increment resets it to 0, but also causes an increment of the next digit to the left: 0000, 0001, (rightmost digit starts over, and next digit is incremented) 0010, 0011, (rightmost two digits start over, and next digit is incremented) 0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented) 1000, 1001, 1010, 1011, 1100, 1101, 1110,

15 Binary to decimal Since binary is a base-2 system, each digit represents an increasing power of 2, with the rightmost digit representing 2 0, the next representing 2 1, then 2 2, and so on To determine the decimal representation of a binary number simply take the sum of the products of the binary digits and the powers of 2 which they represent For example, the binary number is converted to decimal form as follows: = = = 37 10

16 Decimal to binary Successive divisions by 2, storing the reminder (0 or 1) Stop when the quotient is 0 The binary result is the sequence of reminders read from the end to the beginning: => = Validation: = = =

17 Decimal to other bases This method can be modified to convert from decimal to any base The divisor was 2 because the desired destination was base 2 (binary) If the desired destination is a different base, replace the 2 in the method with the desired base For example, if the desired destination is base 8, replace the 2 with 8 The final result will then be in the desired base

18 Decimal to octal Octal is base-8, with numbers between 0 and 7 => = common writing in programming languages: prefixed by zero: Validation: = = =

19 Decimal to hexadecimal The hexadecimal (base sixteen) numeral system has sixteen possible values, using the letters A, B, C, D, E, and F for the six place-values after 9: Dec Hex A B C D E F Oct Bin

20 Decimal to hexadecimal (F) (D) 4 => = 4DF7 16 common writing in programming languages: prefixed by 0x: 0x4DF7 Validation: 4DF7 16 = = =

21 Binary arithmetic Arithmetic in binary is much like arithmetic in other numeral systems addition subtraction multiplication division

22 Addition The simplest arithmetic operation in binary is addition Adding two single-digit binary numbers is relatively simple, using a form of carrying: , carry 1 => 10 2 ( = 2 10 ) Addition table Adding two "1" digits produces a digit "0", while 1 will have to be added to the next column

23 Addition This is similar to what happens in decimal when certain single-digit numbers are added together; if the result equals or exceeds the value of the base (10), the digit to the left is incremented: , carry 1 (since = 10 = ) , carry 1 (since = 16 = ) This is known as carrying When the result of an addition exceeds the value of a digit, the procedure is to "carry" the excess amount divided by the radix (that is, 10/10) to the left, adding it to the next positional value

24 Addition Carrying works the same way in binary: In this example, two numerals are being added together: (13 10 ) and (23 10 ) The top row shows the carry bits used carried digits Starting in the rightmost column, = 10 2 ; the 1 is carried to the left, and the 0 is written at the bottom of the rightmost column The second column from the right is added: = 10 2 again; the 1 is carried, and 0 is written at the bottom The third column: = 11 2 ; this time, a 1 is carried, and a 1 is written in the bottom row Proceeding like this gives the final answer (36 10 )

25 Subtraction Subtraction works in much the same way: starred columns are borrowed from * * * * , borrow Subtracting a "1" digit from a "0" digit produces the digit "1", while 1 will have to be subtracted from the next column This is known as borrowing The principle is the same as for carrying; when the result of a subtraction is less than 0, the least possible value of a digit, the procedure is to "borrow" the deficit divided by the radix from the left, subtracting it from the next positional value

26 Multiplication Multiplication in binary is similar to its decimal counterpart Two numbers A and B can be multiplied by partial products: for each digit in B, the product of that digit in A is calculated and written on a new line, shifted leftward so that its rightmost digit lines up with the digit in B that was used The sum of all these partial products gives the final result

27 Multiplication Since there are only two digits in binary, there are only two possible outcomes of each partial multiplication: if the digit in B is 0, the partial product is also 0 if the digit in B is 1, the partial product is equal to A Multiplication table

28 Multiplication For example, the binary numbers 1110 and 1010 are multiplied as follows: = = =

29 Division Binary division is again similar to its decimal counterpart: Here, the divisor is 101 2, or 5 decimal, while the dividend is , or 27 decimal The procedure is: the divisor goes into the first three digits of the dividend one time, so a "1" is written on the result line this result is multiplied by the divisor, and subtracted from the first three digits of the dividend; the next digit (a "1") is included to obtain a new three-digit sequence the procedure is then repeated with the new sequence, continuing until the digits in the dividend have been exhausted

30 Division Thus, the quotient of divided by is 101 2, as shown on the result line, while the remainder, shown on the bottom line, is 10 2 In decimal, 27 divided by 5 is 5, with a remainder of

31 The sizeof operator Before operating with bits, an operator that determines the number of bytes used in a data type representation is needed

32 The sizeof operator It is an unary operator used to determine the size of a data type, measured in number of bytes required to represent that type It can be applied to any data type (standard or userdefined)

33 The sizeof operator The keyword "sizeof" is followed by a type name or an expression (which may merely be a variable name) If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses with data types, sizeof evaluates the size of the memory representation for an object of the specified data type for expressions it evaluates the representation size for the type that would result from evaluation of the expression (which however is not evaluated)

34 Why do we need sizeof? To write portable programs sizes of types are implementation dependent (processor, operating system, compiler) the dimension of a data type (even it is a standard data type) is not precisely defined by the standard (for instance, an integer can be represented on 2 or on 4 bytes); the only data type that has the same representation (using 1 byte) in any standard C implementation is char for example, don t write programs that assumes an integer has 2 bytes; it is possible to run incorrectly on other systems

35 Why do we need sizeof? It is frequently very difficult to predict the sizes of compound data types such as a struct or a union In bitwise operations and in dynamic memory allocation it is required to know how many bytes a variable or a data type has

36 Example 1 a C program since sizeof(char) is defined to be 1 and assuming that ints are 4 bytes long and doubles are 8 bytes long, the following code will print: 1, 8, 4: #include <stdio.h> void main(void){ int a; double b; char c; printf("%d, %d, %d", sizeof c, sizeof (a+b), sizeof(int)); }

37 Bitwise operations NOT, AND, OR, XOR, SHIFT

38 Bitwise operations Though not directly related to the numerical interpretation of binary symbols, sequences of bits may be manipulated using Boolean logical operators When a sequence of binary symbols is manipulated this way, it is called a bitwise operation AND, OR, and XOR may be performed on corresponding bits in two binary numerals provided as input NOT operation may be performed on individual bits in a single binary numeral provided as input arithmetic shifts other important operations (used, for instance, in multiplication or division by two)

39 Bitwise operations in C Operations on bits at individual levels can be carried out using Bitwise operations in the C programming language Bits come together to form a byte which is the lowest form of data that can be accessed in digital hardware The whole representation of a number is considered while applying a bitwise operator

40 Bitwise operators in C They can be used only for integer operands C programming language provides six operators for bit manipulation: Symbol Operator ~ bitwise NOT (unary operator) & bitwise AND bitwise OR ^ bitwise exclusive OR (XOR) >> right shift << left shift

41 Bitwise NOT ~ It represents the complement of a given number for every bit 1, the result is 0 for every bit 0, the result is 1 a ~ a ~ =

42 Bitwise AND & It is just a representation of AND and does its work on bits and not on bytes, chars, integers, etc. So basically a binary AND performs the logical AND on the bits in each position of a number in its binary form & the most significant bit of the first number is 1 and that of the second number is also 1, so the most significant bit of the result must be 1 in the second most significant bit, the bit of second number is zero, so the result is 0 a b a & b

43 Bitwise OR Similar to Bitwise AND, Bitwise OR only operates on bits, not bytes Its result is a 1 if one of the either bits is 1 and zero only when both bits are a b a b

44 Bitwise XOR ^ The Bitwise XOR (exclusive OR) adds two bits while discarding the carry The result is zero only when 2 zeroes or ones are involved Sometimes XOR might just be used to toggle the bits between 1 and 0 Thus: i=i^1 when used in a loop toggles its values between 1 and 0 a b a ^ b

45 Bitwise XOR ^ You can think of XOR in the following way: you have some bit, either 1 or 0, that we'll call A when you take A XOR 0, then you always get A back: if A is 1, you get 1, and if A is 0, you get 0 on the other hand, when you take A XOR 1, you flip A: if A is 0, you get 1; if A is 1, you get 0 So you can think of the XOR operation as a sort of selective twiddle: if you apply XOR to two numbers, one of which is all 1s, you get the equivalent of a twiddle

46 Right shift >> It requires two operands It shifts each bit in its left operand to the right The number following the operator (the right operand) decides the number of places the bits are shifted Thus by doing ch>>3 all the bits will be shifted to the right by three places

47 Right shift >> Example: If the variable ch contains the bit pattern , then ch >> 1 will give the output as , and ch >> 2 will give If the number is unsigned, zeros are generated on the left when the bits are shifted to the right Else implementation dependent behavior (usually repeats the sign bit the first bit of the number) => right shift only unsigned numbers (for portability)

48 Right shift >> Right shift can be used to divide a bit pattern by 2 as shown: i=14; // bit pattern 1110 j=i>>1; /* the bit pattern is shifted by 1, obtaining 0111=7 which is 14/2 */

49 Example 2 a C program A program that displays the binary representation of a number using the right shift operator #include <stdio.h> #include <limits.h> void main(void){ unsigned int n; int i; printf("n="); scanf("%u",&n); printf("%u = ", n); for(i=sizeof(unsigned int)*char_bit-1;i>=0;i--) printf("%u",(n>>i)&1); }

50 Left shift << It shifts each bit in its left operand to the left It works opposite to that of right shift operator Thus by doing ch<<1 on a variable ch that contains the bit pattern , the result will be Blank spaces generated are filled up by zeros

51 Left shift << Left shift can be used to multiply an integer by 2 as in: int i=4; /* bit pattern is 100 */ int j=i<<1; /* => 1000, which is 8 */

52 Example 3 a C program A program that displays the binary representation of a number using the left shift operator #include <stdio.h> #include <limits.h> void main(void){ int i, n; printf("n="); scanf("%d",&n); printf("%d = ", n); for(i=sizeof(int)*char_bit-1;i>=0;i--) if((n&(1<<i))!=0) putch('1'); else putch('0'); }

53 Bitwise assignment operators C provides compound assignment operators perform the binary operation store the result in the left operand Symbol Operator &= bitwise AND assignment = bitwise OR assignment ^= bitwise XOR assignment >>= right shift assignment <<= left shift assignment

54 The operators precedence ++,, (type), ~ *, /, % +, <<, >> <, <=, >, >= ==,!= & ^ &&?: =, +=, -=, *=, /=, %=, &=, ^=, =, <<=, >>=

55 Some applications of bitwise operators One and zero only for a specific bit k 1<<k means 1 only for bit k e.g. 1 for bit 3 1<<3 = <<3 = ~ (1<<k) means 0 only for bit k e.g. 0 for bit 4 ~ (1<<4) = ~ ( <<4) = ~ =

56 Some applications of bitwise operators Get a specific bit k of a number n (verify if it is 0 or 1): n & (1<<k) e.g. bit 2 of number : & (1<<2) = & = != 0 the value given by a sequence of bits of a number n: e.g. bits 3, 2, 1, 0 of number : & 0xF = & = = 14 10

57 Some applications of bitwise operators Set / Reset set (to 1) a specific bit k of a number n: n (1<<k) OR with 1 is always 1 e.g. set the bit 3 of number (1<<3) = = => OR with 0 preserves a bit reset (make 0) a specific bit k of a number n: n & ~ (1<<k) AND with 0 is always 0 e.g. reset the bit 5 of number & ~ (1<<5) = & ~ = = & = => AND with 1 preserves a bit

58 Some applications of bitwise operators Flip XOR with 0 preserves a bit XOR with 1 flips a bit flip (change from 1 to 0 and from 0 to 1) a specific bit k of a number n: n ^ (1<<k) e.g. flip the bit 3 of number ^ (1<<3) = ^ = flip it again ^ (1<<3) = ^ =

59 Some applications of bitwise operators Example of other bit patterns (also called masks the blue elements in the previous examples) : ~ 0 << : ~ (~ 0 << 2) : ~ (~ 0 << 5) << 1 n & (~ (~ 0 << 2)) resets all bits of n, except last 2 bits n & (~ (~ 0 << 5) << 1) resets all bits of n, except 5 bits starting from 1

60 Example 4 a C program A program that works with a set of 8 LEDs (lightemitting diodes) stored as an unsigned char It offers functions that: verify if a given led is on or off; turn on a specific led; turn off a led; flip a led. All LEDs are initially turned off

61 #include <stdio.h> #include <limits.h> void display(unsigned char leds){ int i; for(i=char_bit-1;i>=0;i--) printf("%d",(leds>>i)&1); printf("\n"); } int isturnedon(unsigned char leds, int lednumber){ return leds & (1<<ledNumber); } void turnon(unsigned char *leds, int lednumber){ *leds = *leds (1<<ledNumber); } void turnoff(unsigned char *leds, int lednumber){ *leds = *leds & ~(1<<ledNumber); } void flip(unsigned char *leds, int lednumber){ *leds = *leds ^ (1<<ledNumber); } void main(){ unsigned char leds=0; int i; display(leds); turnon(&leds,5); display(leds); turnoff(&leds,5); display(leds); if(!isturnedon(leds,4)) turnon(&leds,4); display(leds); for(i=1;i<10;i++){ flip(&leds,0); display(leds); } }

62 Example 5 a C program A program that adds two unsigned integers using AND, XOR and left shift #include <stdio.h> void main(void){ unsigned int x, y, carry; printf("x="); scanf("%u", &x); printf("y="); scanf("%u", &y); while(x!=0){ carry=x&y; y=x^y; carry=carry<<1; x=carry; } printf("sum=%u", y); }

63 References Clint HICKS: Utilizare C. Uşor şi repede, Teora Printing House, 1996, ISBN Cprogramming Wikipedia

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Beginning C Programming for Engineers

Beginning C Programming for Engineers Beginning Programming for Engineers R. Lindsay Todd Lecture 6: Bit Operations R. Lindsay Todd () Beginning Programming for Engineers Beg 6 1 / 32 Outline Outline 1 Place Value Octal Hexadecimal Binary

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Chapter 7. Basic Types

Chapter 7. Basic Types Chapter 7 Basic Types Dr. D. J. Jackson Lecture 7-1 Basic Types C s basic (built-in) types: Integer types, including long integers, short integers, and unsigned integers Floating types (float, double,

More information

ME 461 C review Session Fall 2009 S. Keres

ME 461 C review Session Fall 2009 S. Keres ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Number Systems CHAPTER Positional Number Systems

Number Systems CHAPTER Positional Number Systems CHAPTER 2 Number Systems Inside computers, information is encoded as patterns of bits because it is easy to construct electronic circuits that exhibit the two alternative states, 0 and 1. The meaning of

More information

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 6. Iulian Năstac Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap

More information

The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction The Design of C: A Rational Reconstruction 1 Goals of this Lecture Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C and thereby

More information

The Design of C: A Rational Reconstruction"

The Design of C: A Rational Reconstruction The Design of C: A Rational Reconstruction 1 Goals of this Lecture Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C and thereby

More information

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers CSC 2400: Computer Systems Bits, Bytes and Data Types 1 Goals for this Week Binary number system Why binary? Converting between decimal and binary and octal and hexadecimal number systems Finite representations

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

Chapter 2: Number Systems

Chapter 2: Number Systems Chapter 2: Number Systems Logic circuits are used to generate and transmit 1s and 0s to compute and convey information. This two-valued number system is called binary. As presented earlier, there are many

More information

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

A flow chart is a graphical or symbolic representation of a process. Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of

More information

Chapter 4. Operations on Data

Chapter 4. Operations on Data Chapter 4 Operations on Data 1 OBJECTIVES After reading this chapter, the reader should be able to: List the three categories of operations performed on data. Perform unary and binary logic operations

More information

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

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

More information

The Design of C: A Rational Reconstruction" Jennifer Rexford!

The Design of C: A Rational Reconstruction Jennifer Rexford! The Design of C: A Rational Reconstruction" Jennifer Rexford! 1 Goals of this Lecture"" Number systems! Binary numbers! Finite precision! Binary arithmetic! Logical operators! Design rationale for C! Decisions

More information

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System For Your Amusement Number Systems and Number Representation Jennifer Rexford Question: Why do computer programmers confuse Christmas and Halloween? Answer: Because 25 Dec = 31 Oct -- http://www.electronicsweekly.com

More information

Expression and Operator

Expression and Operator Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

Arithmetic Operators. Portability: Printing Numbers

Arithmetic Operators. Portability: Printing Numbers Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +

More information

Level ISA3: Information Representation

Level ISA3: Information Representation Level ISA3: Information Representation 1 Information as electrical current At the lowest level, each storage unit in a computer s memory is equipped to contain either a high or low voltage signal Each

More information

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

More information

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

Expressions and Precedence. Last updated 12/10/18

Expressions and Precedence. Last updated 12/10/18 Expressions and Precedence Last updated 12/10/18 Expression: Sequence of Operators and Operands that reduce to a single value Simple and Complex Expressions Subject to Precedence and Associativity Six

More information

Number System. Introduction. Decimal Numbers

Number System. Introduction. Decimal Numbers Number System Introduction Number systems provide the basis for all operations in information processing systems. In a number system the information is divided into a group of symbols; for example, 26

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 3 Bits and Bytes; Bitwise Operators CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning 4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

More information

The Design of C: A Rational Reconstruction

The Design of C: A Rational Reconstruction The Design of C: A Rational Reconstruction 2 Goals of this Lecture Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C and thereby

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions CSE 2031 Fall 2011 9/11/2011 5:24 PM 1 Variable Names (2.1) Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a

More information

Arithmetic and Bitwise Operations on Binary Data

Arithmetic and Bitwise Operations on Binary Data Arithmetic and Bitwise Operations on Binary Data CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides

More information

Engineering Computing I

Engineering Computing I Engineering Computing I Types, Operators, and Expressions Types Operators Expressions 2 1 2.1 Variable Names Names are made up of letters and digits The first character must be a letter The underscore

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation Princeton University Computer Science 27: Introduction to Programming Systems Goals of this Lecture and Number Representation Help you learn (or refresh your memory) about: The binary, hexadecimal, and

More information

Programming I Lecture 7

Programming I Lecture 7 . Definition: An integer is a number without a fractional part. The set of integers is the union of the set of whole numbers and the set of negative counting numbers... Integers and whole numbers. C++

More information

Types, Operators and Expressions

Types, Operators and Expressions Types, Operators and Expressions EECS 2031 18 September 2017 1 Variable Names (2.1) l Combinations of letters, numbers, and underscore character ( _ ) that do not start with a number; are not a keyword.

More information

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch More About Values Casts To cast is to take a value of one type and return the corresponding value of some other type (or an error, if the cast is impossible) int(x) casts a string, float, or boolean x

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Chapter 1 Preliminaries

Chapter 1 Preliminaries Chapter 1 Preliminaries This chapter discusses the major classes of programming languages and the relationship among them. It also discusses the binary and the hexadecimal number systems which are used

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR UNIT I Digital Systems: Binary Numbers, Octal, Hexa Decimal and other base numbers, Number base conversions, complements, signed binary numbers, Floating point number representation, binary codes, error

More information

Number Systems and Number Representation

Number Systems and Number Representation Princeton University Computer Science 217: Introduction to Programming Systems Number Systems and Number Representation Q: Why do computer programmers confuse Christmas and Halloween? A: Because 25 Dec

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

Number representations

Number representations Number representations Number bases Three number bases are of interest: Binary, Octal and Hexadecimal. We look briefly at conversions among them and between each of them and decimal. Binary Base-two, or

More information

Chapter 1 Review of Number Systems

Chapter 1 Review of Number Systems 1.1 Introduction Chapter 1 Review of Number Systems Before the inception of digital computers, the only number system that was in common use is the decimal number system which has a total of 10 digits

More information

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997 Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997 APPENDIX A.1 Number systems and codes Since ten-fingered humans are addicted to the decimal system, and since computers

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Fundamentals of Programming Session 2

Fundamentals of Programming Session 2 Fundamentals of Programming Session 2 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 Sharif University of Technology Outlines Programming Language Binary numbers Addition Subtraction

More information

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning 4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

More information

Bits, Bytes, and Integers

Bits, Bytes, and Integers Bits, Bytes, and Integers with contributions from Dr. Bin Ren, College of William & Mary 1 Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned

More information

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value. 3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

More information

Chapter 12 Variables and Operators

Chapter 12 Variables and Operators Chapter 12 Variables and Operators Highlights (1) r. height width operator area = 3.14 * r *r + width * height literal/constant variable expression (assignment) statement 12-2 Highlights (2) r. height

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

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

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators CSE 351: The Hardware/Software Interface Section 2 Integer representations, two s complement, and bitwise operators Integer representations In addition to decimal notation, it s important to be able to

More information

CSCI 2212: Intermediate Programming / C Chapter 15

CSCI 2212: Intermediate Programming / C Chapter 15 ... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25 ... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations

More information

Operators in C. Staff Incharge: S.Sasirekha

Operators in C. Staff Incharge: S.Sasirekha Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C

More information

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

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

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

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

Chapter 2 Exercises and Answers

Chapter 2 Exercises and Answers Chapter 2 Exercises and nswers nswers are in blue. For Exercises -5, match the following numbers with their definition.. Number. Natural number C. Integer number D. Negative number E. Rational number unit

More information

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1.1 Introduction Given that digital logic and memory devices are based on two electrical states (on and off), it is natural to use a number

More information

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS Aleksandar Milenković The LaCASA Laboratory, ECE Department, The University of Alabama in Huntsville Email: milenka@uah.edu Web:

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS Aleksandar Milenković The LaCASA Laboratory, ECE Department, The University of Alabama in Huntsville Email: milenka@uah.edu Web:

More information

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 CS 64 Lecture 2 Data Representation Reading: FLD 1.2-1.4 Decimal Numbers: Base 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Example: 3271 = (3x10 3 ) + (2x10 2 ) + (7x10 1 ) + (1x10 0 ) 1010 10?= 1010 2?= 1

More information

Chapter 2. Data Representation in Computer Systems

Chapter 2. Data Representation in Computer Systems Chapter 2 Data Representation in Computer Systems Chapter 2 Objectives Understand the fundamentals of numerical data representation and manipulation in digital computers. Master the skill of converting

More information

Binary representation of integer numbers Operations on bits

Binary representation of integer numbers Operations on bits Outline Binary representation of integer numbers Operations on bits The Bitwise AND Operator The Bitwise Inclusive-OR Operator The Bitwise Exclusive-OR Operator The Ones Complement Operator The Left Shift

More information

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 3 Bits and Bytes; Bitwise Operators CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

UNIT 3 OPERATORS. [Marks- 12]

UNIT 3 OPERATORS. [Marks- 12] 1 UNIT 3 OPERATORS [Marks- 12] SYLLABUS 2 INTRODUCTION C supports a rich set of operators such as +, -, *,,

More information

Low-Level Programming

Low-Level Programming Programming for Electrical and Computer Engineers Low-Level Programming Dr. D. J. Jackson Lecture 15-1 Introduction Previous chapters have described C s high-level, machine-independent features. However,

More information

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

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

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

Review of Data Representation & Binary Operations Dhananjai M. Rao CSA Department Miami University

Review of Data Representation & Binary Operations Dhananjai M. Rao CSA Department Miami University Review of Data Representation & Binary Operations Dhananjai M. Rao () CSA Department Miami University 1. Introduction In digital computers all data including numbers, characters, and strings are ultimately

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff C Programming Language 1 C C is better to use than assembly for embedded systems programming. You can program at a higher level of logic than in assembly, so programs are shorter and easier to understand.

More information

CSCI 2021: Binary, Integers, Arithmetic

CSCI 2021: Binary, Integers, Arithmetic CSCI 2021: Binary, Integers, Arithmetic Chris Kauffman Last Updated: Mon Feb 18 14:35:27 CST 2019 1 Logistics Reading Bryant/O Hallaron Ch 2.1-3 Goals Finish C overview Binary Representations / Notation

More information

Lecture 2: Number Systems

Lecture 2: Number Systems Lecture 2: Number Systems Syed M. Mahmud, Ph.D ECE Department Wayne State University Original Source: Prof. Russell Tessier of University of Massachusetts Aby George of Wayne State University Contents

More information

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

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting) 2.1 Unit 2 Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting) 2.2 Skills & Outcomes You should know and be able to apply the following skills with confidence Perform addition & subtraction

More information

UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

More information

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other Kinds Of Data CHAPTER 3 DATA REPRESENTATION Numbers Integers Unsigned Signed Reals Fixed-Point Floating-Point Binary-Coded Decimal Text ASCII Characters Strings Other Graphics Images Video Audio Numbers

More information

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

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee ١ Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee Digital systems Digital systems are used in communication, business transactions, traffic control, spacecraft guidance, medical

More information

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

Internal representation. Bitwise operators

Internal representation. Bitwise operators Computer Programming Internal representation. Bitwise operators Marius Minea marius@cs.upt.ro 26 October 2015 Ideal math and C are not the same! In mathematics: integers Z and reals R have unbounded values

More information

Operators and Expressions:

Operators and Expressions: Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

More information

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects: Numeral Systems -Numeral System -Positional systems -Decimal -Binary -Octal Subjects: Introduction A numeral system (or system of numeration) is a writing system for expressing numbers, that is a mathematical

More information