Beginning C Programming for Engineers

Similar documents
CSCI 2212: Intermediate Programming / C Chapter 15

CS 241 Data Organization Binary

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

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

Computer Organization & Systems Exam I Example Questions

Chapter 3 Basic Data Types. Lecture 3 1

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

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

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Applied Computer Programming

Expression and Operator

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

Binary Representations and Arithmetic

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

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

Arithmetic Operations

Number System. Introduction. Decimal Numbers

The Design of C: A Rational Reconstruction

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

C-string format with scanf/printf

The Design of C: A Rational Reconstruction"

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

Number Systems and Number Representation

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

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

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Survey. Motivation 29.5 / 40 class is required

LAB A Translating Data to Binary

CS33 Project Gear Up. Data

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

Chapter 1 Review of Number Systems

Bits and Bytes January 13, 2005

Bits, Bytes and Integers

Topic Notes: Bits and Bytes and Numbers

Numbers and Representations

JAVA OPERATORS GENERAL

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

Lecture 13 Bit Operations

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

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

Assist. Prof. Dr. Caner ÖZCAN

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

Chapter 3: Operators, Expressions and Type Conversion

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

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

Arithmetic and Bitwise Operations on Binary Data

Introduction to Computer Science-103. Midterm

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

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

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

CS 107 Lecture 2: Bits and Bytes (continued)

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

Bits, Words, and Integers

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

COMP2611: Computer Organization. Data Representation

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Topic Notes: Bits and Bytes and Numbers

CHW 261: Logic Design

C-string format with scanf/printf

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Integer Representation

EE292: Fundamentals of ECE

The Design of C: A Rational Reconstruction

Introduction to Computers and Programming. Numeric Values

Chapter 12 Variables and Operators

Data Representation 1

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Selection from Susta:Computer System Structures & John Loomis: Computer organization & M.Mudawar:Computer Architecture & Assembly Language. Cvičení 1.

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

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

Number Representations

Embedded programming, AVR intro

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Hardware: Logical View

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Programming. Elementary Concepts

Unsigned Binary Integers

Unsigned Binary Integers

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

Number representations

Inf2C - Computer Systems Lecture 2 Data Representation

Binary representation of integer numbers Operations on bits

Computer Architecture

Arithmetic and Bitwise Operations on Binary Data

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

Signed Binary Numbers

AGENDA Binary Operations CS 3330 Samira Khan

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Arithmetic Operators. Portability: Printing Numbers

CS 64 Week 0 Lecture 1. Kyle Dewey

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Computer Systems Programming. Practice Midterm. Name:

Time (self-scheduled): Location Schedule Your Exam: What to bring:

Lecture Notes on Ints

The type of all data used in a C++ program must be specified

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

More on C programming

Transcription:

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 2 onversions 3 Bitwise operators Basic operations Masking bits Setting bits Masking and setting Toggling bits Shifting bits Bit operations and arithmetic R. Lindsay Todd () Beginning Programming for Engineers Beg 6 2 / 32

Place Value Place Value Numerals A numeral represents a number using some notation. Normal place value numerals: each place represents a power of ten. E.g., 1492 1 10 3 + 4 10 2 + 9 10 1 + 2 10 0 108 1 10 2 + 0 10 1 + 8 10 0 Digits range from zero to nine (ten minus one). In general, for an n digit number d n d n 1... d 0, the value is d n 10 n + d n 1 10 n 1 + + d 0 10 0 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 3 / 32

Octal Numerals Place Value Octal We can use other bases besides 10. For a base b, we need a set of b digits, one for each value from 0 to b 1. For example, consider base eight (octal numerals). Digits are {0, 1, 2, 3, 4, 5, 6, 7}. E.g., 127 1 8 2 + 2 8 1 + 7 8 0 = 87 ten 19 No! In, octal integer constants are written by writing a leading zero. Octal examples i n t a = 017; / 1 8 + 7 = 15 / i n t b = 0234; / 2 64 + 3 8 + 4 = 156 / i n t c = 019; / o m p i l a t i o n e r r o r! / R. Lindsay Todd () Beginning Programming for Engineers Beg 6 5 / 32

Hexadecimal Numerals Place Value Hexadecimal Base sixteen (hexadecimal) needs non-arabic digits. We use the set of digits: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B,, D, E, F } 10F 1 16 2 + 0 16 1 + F 16 0 = 271 ten 18 1 16 1 + 8 16 0 = 24 ten In, hexadecimal integer constants are written by writing a leading 0x. Hexadecimal examples i n t a = 0 x11 ; / 1 16 + 1 = 17 / i n t b = 0xA0 ; / 10 16 + 0 = 160 / i n t c = 0xAB ; / 10 16 + 11 = 171 / R. Lindsay Todd () Beginning Programming for Engineers Beg 6 7 / 32

Place Value Hexadecimal I/O in Octal and Hexadecimal With printf, the %o format may be used to format any int value as octal. With printf, the %x and %X formats may be used to format any int as hexadecimal. The %i format can be used with scanf to read values as if they were constants. Program: numbers.c # include <stdio.h> int main () { int val ; printf (" Enter value : "); scanf ("%i", & val ); printf (" Decimal : %d, %i\n", val, val ); printf (" Octal : %o\n", val ); printf (" Hex x X: %x, %X\n", val, val ); return 0; } Results Enter value: 167 Decimal: 167, 167 Octal: 247 Hex x X: a7, A7 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 9 / 32

Binary Numerals Place Value Binary Base two (binary) only uses digits from {0, 1}. Binary digits are frequently called bits. A bit can be represented electronically with an on/off switch. 1011 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = 11 ten There are no binary constants in (but octal or hexadecimal are helpful). R. Lindsay Todd () Beginning Programming for Engineers Beg 6 10 / 32

onversions Bit Patterns Bit patterns can be represented using octal or hexadecimal, e.g. 1 1 0 1 0 1 1 0 0 3 2 6 0x d 6 Each octal digit corresponds to a pattern of three bits, since 2 3 = 8. There are 8 possible patterns of 3 bits. Hexadecimal digits correspond to a pattern of four bits, since 2 4 = 16. There are 16 possible patterns of 4 bits. R. Lindsay Todd () Beginning Programming for Engineers Beg 6 11 / 32

Bit Pattern onversions onversions Octal Bit pattern Decimal 0 0 0 0 0 1 0 0 1 1 2 0 1 0 2 3 0 1 1 3 4 1 0 0 4 5 1 0 1 5 6 1 1 0 6 7 1 1 1 7 Hexadecimal Bit pattern Decimal 0 0 0 0 0 0 1 0 0 0 1 1 2 0 0 1 0 2 3 0 0 1 1 3 4 0 1 0 0 4 5 0 1 0 1 5 6 0 1 1 0 6 7 0 1 1 1 7 8 1 0 0 0 8 9 1 0 0 1 9 A 1 0 1 0 10 B 1 0 1 1 11 1 1 0 0 12 D 1 1 0 1 13 E 1 1 1 0 14 F 1 1 1 1 15 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 12 / 32

onversions Bytes, Nybbles, and Bits Everything is represented internally using bits. A char is stored in a unit of storage called a byte, usually 8 bits. Both int and float are often 32 bits. A long is usually 32 or 64 bits. has bitwise operators that operate on integers as bit patterns. R. Lindsay Todd () Beginning Programming for Engineers Beg 6 13 / 32

Bitwise operators Bitwise operators Basic operations The bitwise operators operate on each corresponding bit of their operands. For each bit, x and y: AND OR XOR x y x&y x y x^y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Additionally, the complement operator, ~, negates each bit. x ~x 0 1 1 0 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 15 / 32

Bitwise operators Examples: Bit Operations Basic operations unsigned char x = 3 7, y =246; x 0 0 1 0 0 1 0 1 37 y 1 1 1 1 0 1 1 0 246 x&y 0 0 1 0 0 1 0 0 36 x y 1 1 1 1 0 1 1 1 247 x^y 1 1 0 1 0 0 1 1 211 ~x 1 1 0 1 1 0 1 0 218 Don t confuse & and with the logical && and operators. 37 && 246 1 37 246 1 There are also &=, =, and ^= operators. R. Lindsay Todd () Beginning Programming for Engineers Beg 6 17 / 32

Masking Bitwise operators Masking bits The AND (&) operator can be used to mask (select) particular bits from a pattern of bits. Example: Select the lowest 5 bits of a number: Program: maskbits.c x b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 0x1F 0 0 0 1 1 1 1 1 x&0x1f 0 0 0 b 4 b 3 b 2 b 1 b 0 # include <stdio.h> int main () { int x; printf (" Enter number : "); scanf ("%i", &x); printf (" Lowest 5 bits : %X\n", (x & 0 x1f )); return 0; } Results Enter number: 0x450E Lowest 5 bits: E R. Lindsay Todd () Beginning Programming for Engineers Beg 6 19 / 32

Bitwise operators Setting bits Setting Bits The OR ( ) operator can be used to specifically set particular bits. Example: Set the lowest 5 bits of a number: x b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 0x1F 0 0 0 1 1 1 1 1 x 0x1F b 7 b 6 b 5 1 1 1 1 1 Program: setbits.c # include <stdio.h> int main () { int x; printf (" Enter number : "); scanf ("%i", &x); printf (" Lowest 5 bits set : %X\n", (x 0 x1f )); return 0; } Results Enter number: 0x4503 Lowest 5 bits set: 451F R. Lindsay Todd () Beginning Programming for Engineers Beg 6 21 / 32

Bitwise operators Masking and setting Masking and Setting Sometimes we need to mask, then set, to get the desired result. Example: Set the 5 lowest bits to be a pattern stored in another variable. x x 7 x 6 x 5 x 4 x 3 x 2 x 1 x 0 ~0x1F 1 1 1 0 0 0 0 0 x&~0x1f x 7 x 6 x 5 0 0 0 0 0 y y 7 y 6 y 5 y 4 y 3 y 2 y 1 y 0 y&0x1f 0 0 0 y 4 y 3 y 2 y 1 y 0 (x&~0x1f) (y&0x1f) x 7 x 6 x 5 y 4 y 3 y 2 y 1 y 0 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 23 / 32

ode: Mask & Set Bitwise operators Masking and setting Program: maskset.c # include <stdio.h> int main () { int x, y; printf (" Enter number : "); scanf ("%i", &x); printf (" Replacement bits : "); scanf ("%i", &y); printf (" With new bits : %X\n", (x & ~0 x1f ) (y & 0 x1f ) ); return 0; } Results Enter number: 0x1B49 Replacement bits: 0x03 With new bits: 1B43 R. Lindsay Todd () Beginning Programming for Engineers Beg 6 25 / 32

Bitwise operators Toggling bits Toggling Bits The exclusive or operation can be used to toggle bits, that is, reverse their sense from true to false, and vice versa. Example: Toggle bits 4 and 6. x b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 0x50 0 1 0 1 0 0 0 0 x ^ 0x50 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 Program: toggle.c # include <stdio.h> int main () { int x; printf (" Enter number : "); scanf ("%i", &x); printf (" Toggle bits 4, 6: %X\n", (x ^ 0 x0050 )); return 0; } Results Enter number: 0xFF Toggle bits 4, 6: AF R. Lindsay Todd () Beginning Programming for Engineers Beg 6 27 / 32

Bitwise operators Shifting bits Shift Operations unsigned char x = 3 7 ; x 0 0 1 0 0 1 0 1 37 x << 1 0 1 0 0 1 0 1 0 74 x << 2 1 0 0 1 0 1 0 0 148 x << 3 0 0 1 0 1 0 0 0 40 x >> 1 0 0 0 1 0 0 1 0 18 x >> 2 0 0 0 0 1 0 0 1 9 x >> 3 0 0 0 0 0 1 0 0 4 Shift operators move bit patterns either left or right. With unsigned int numbers, shifting left is equivalent to multiplying by 2 (until bits shift off the left end); shifting right is equivalent to dividing by 2. R. Lindsay Todd () Beginning Programming for Engineers Beg 6 29 / 32

Bitwise operators Bit Patterns and Powers of 2 Bit operations and arithmetic Some interesting bit patterns: 2 1 1 0 0 0 1 1 2 2 1 0 0 1 1 3 2 3 1 0 1 1 1 7 2 4 1 1 1 1 1 15 In general, 2 n 1 is represented in binary as a numeral with n digits all 1. R. Lindsay Todd () Beginning Programming for Engineers Beg 6 30 / 32

Example: Shifts Program: bits.c Bitwise operators Bit operations and arithmetic /* Get an integer, extract bits 8-10. * LSB is bit 0. */ # include <stdio.h> int main () { int i; } printf (" Enter number : "); scanf ("%i", &i); printf ("i=%x\n", i); printf ("i > >8=% x\n", i > >8); printf (" Bits 8-10: %x\n", (i >> 8) & 07); return 0; R. Lindsay Todd () Beginning Programming for Engineers Beg 6 32 / 32