large units some names for large numbers CIS 2107 Chapter 2 Notes Part 1

Similar documents
CIS 2107 Chapter 2 Notes Part 1. Representing and Manipulating Information

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

Introduction to the Use of Computers

Bits, bytes and digital information. Lecture 2 COMPSCI111/111G

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

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

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

Where we are going (today)

Where we are going (today)

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Where we are going (today)

Memory, Data, & Addressing II CSE 351 Spring

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

Here is a diagram of a simple computer system: (this diagram will be the one needed for exams) CPU. cache

CS61C : Machine Structures

Topic Notes: Bits and Bytes and Numbers

Are Computers Smart? To a programmer: Lecture #1 Introduction & Numbers Andy Carle. Are Computers Smart? What are Machine Structures?

Topic Notes: Bits and Bytes and Numbers

Applied Computer Programming

Bits, Bytes and Integers

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

CS61C : Machine Structures

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

Bits and Bytes January 13, 2005

CSCI 2021: Binary, Integers, Arithmetic

Common Number Systems

ITEC 1011 Introduction to Information Technologies

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

CS61C : Machine Structures

Building Java Programs Chapter 1

Memory, Data, & Addressing II

Bits, Bytes, and Integers

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

Few reminders and demos

Lecture 5-6: Bits, Bytes, and Integers

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CS61C : Machine Structures

Bits, Bytes, and Integers Part 2

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Pointers (1A) Young Won Lim 1/22/18

THE FUNDAMENTAL DATA TYPES

Hardware: Logical View

Pointers (1A) Young Won Lim 2/10/18

Lecture 1: What is a computer?

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

ISA 563 : Fundamentals of Systems Programming

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

Pointers (1A) Young Won Lim 2/6/18

College of Computer and Information Sciences Department of Computer Science. CSC 220: Computer Organization. Unit1 Number Systems

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

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

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

A Short Course for REU Students Summer Instructor: Ben Ransford

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

CS 61C: Great Ideas in Computer Architecture Introduction to C

What's in a computer?

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

Practical Malware Analysis

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

CIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm

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

levels (layers) of abstraction

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Binary Representations and Arithmetic

Digital Logic Lecture 2 Number Systems

The Design of C: A Rational Reconstruction"

EMBEDDED SYSTEMS. Part I: Introduction and Hardware

Integer Representation

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

CC312: Computer Organization

Expression and Operator

Chapter 2 - Introduction to C Programming

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

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

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

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

A Java program contains at least one class definition.

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

The Design of C: A Rational Reconstruction

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

AGENDA Binary Operations CS 3330 Samira Khan

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

C-string format with scanf/printf

1.1. Unit 1. Integer Representation

C: How to Program. Week /Mar/05

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

C and Java some big differences

Memory Addressing, Binary, and Hexadecimal Review

int main() { return 0; }

Fundamentals of Programming

Variables and Constants

CS 107 Lecture 2: Bits and Bytes (continued)

Transcription:

large units CIS 2107 Chapter 2 Notes Part 1 Representing and Manipulating Information kilo 10 3 1,000 mega 10 6 1,000,000 giga 10 9 1,000,000,000 tera 10 12 1,000,000,000,000 peta 10 15 1,000,000,000,000,000 exa 10 18 1,000,000,000,000,000,000 zetta 10 21 1,000,000,000,000,000,000,000 yotta 10 24 1,000,000,000,000,000,000,000,000 units < 1 milli 10 3 micro 10 6 nano 10 9 pico 10 12 femto 10 15 atto 10 18 zepto 10 21 yocto 10 24 some names for large numbers kilo 10 3 thousand mega 10 6 million giga 10 9 billion tera 10 12 trillion peta 10 15 quadrillion exa 10 18 quintillion zetta 10 21 sextillion yotta 10 24 septillion

kilo: 1,000 or 1,024? powers of 10 powers of 2 kilo 10 3 1,000 2 10 1,024 mega 10 6 1,000,000 2 20 1,048,576 giga 10 9 1,000,000,000 2 30 1,073,741,824 tera 10 12 1,000,000,000,000 2 40 1,099,511,627,776 peta 10 15 1,000,000,000,000,000 2 50 1,125,899,906,842,624 exa 10 18 1,000,000,000,000,000,000 2 60 1,152,921,504,606,846,976 zetta 10 21 1,000,000,000,000,000,000,000 2 70 1,180,591,620,717,411,303,424 yotta 10 24 1,000,000,000,000,000,000,000,000 2 80 1,208,925,819,614,629,174,706,176 usually use: powers of 2 for storage powers of 10 for just about everything else proposed prefixes for powers of 2 powers of 10 powers of 2 kilo 10 3 kibi 2 10 1,024 mega 10 6 mebi 2 20 1,048,576 giga 10 9 gibi 2 30 1,073,741,824 tera 10 12 tebi 2 40 1,099,511,627,776 peta 10 15 pebi 2 50 1,125,899,906,842,624 exa 10 18 exbi 2 60 1,152,921,504,606,846,976 zetta 10 21 zebi 2 70 1,180,591,620,717,411,303,424 yotta 10 24 yobi 2 80 1,208,925,819,614,629,174,706,176 haven t exactly taken the world by storm Trick for approximating large numbers What is 2 22? 2 20 is about a million 2 2 is 4 2 22 is about 4 million What is 2 36? 2 30 is about a billion 2 6 is 64 2 36 is about 64 billion kilo 10 3 2 10 mega 10 6 2 20 giga 10 9 2 30 tera 10 12 2 40 peta 10 15 2 50 exa 10 18 2 60 zetta 10 21 2 70 yotta 10 24 2 80 powers of 2. memorize. 2 0 1 2 1 2 2 2 4 2 3 8 2 4 16 2 5 32 2 6 64 2 7 128 2 8 256 2 9 512 2 10 1,024

some powers of 16 number system: position is important. Think grade school. Decimal number 5,342. 16 0 1 16 1 16 16 2 256 16 3 4,096 16 4 65,536 Don t have to memorize Notice how these are also powers of 2 5,000 300 40 + 2 5,342 5 * 1,000 3 * 100 4 * 10 + 2 * 1 5,342 5 thousands 3 hundreds 4 tens + 2 ones 5,342 5 * 10 3 3 * 10 2 4 * 10 1 + 2 * 10 0 5,342 binary numbers positions are powers of 2, not 10. binary number 0b1101: 1*2 3 1*2 2 0*2 1 + 1 * 2 0 1*8 1*4 0*2 + 1 * 1 13 converting from decimal to binary two ways: repeated subtraction repeated division

by repeated subtraction Example: 119 10 Want to convert to base 2 so we want to fill this in: 119 128 64 32 16 8 4 2 1 Biggest power of 2 < 119? 128 64 32 16 8 4 2 1 At each step: what s the largest power of 2 smaller than our current sum? 13 14 119 128 64 32 16 8 4 2 1 Biggest power of 2 < 119? 64 put a 1 in the 64 s column subtract 64 from 119 continue. 119-64 128 64 32 16 8 4 2 1 0 1 Biggest power of 2 < 119? 64 put a 1 in the 64 s column subtract 64 from 119 continue. 15 16

119-64 55 128 64 32 16 8 4 2 1 0 1 119-64 55 128 64 32 16 8 4 2 1 0 1 biggest power of 2 < 55? 17 18 119-64 55-32 128 64 32 16 8 4 2 1 0 1 1 biggest power of 2 < 55? 32 1 in the 32s column subtract 32 from 55 119-64 55-32 23 128 64 32 16 8 4 2 1 0 1 1 23 left. Biggest power of 2 < 23? continue 19 20

119-64 55-32 23-16 7 128 64 32 16 8 4 2 1 0 1 1 1 119-64 55-32 23-16 7 128 64 32 16 8 4 2 1 0 1 1 1 0 21 22 119-64 55-32 23-16 7-4 3 128 64 32 16 8 4 2 1 0 1 1 1 0 1 119-64 55-32 23-16 7-4 3-2 1 128 64 32 16 8 4 2 1 0 1 1 1 0 1 1 23 24

119-64 55-32 23-16 7-4 3-2 1-1 0 128 64 32 16 8 4 2 1 0 1 1 1 0 1 1 1 25 Sanity check. We calculated 119 10 as 01110111 2. Double check: (0)(2 7 ) + (1)(2 6 ) + (1)(2 5 ) + (1)(2 4 ) + (0)(2 3 ) + (1)(2 2 ) + (1)(2 1 ) + (1)(2 0 ) =(0)(128) + (1)(64) + (1)(32) + (1)(16) + (0)(8) + (1)(4) + (1)(2) + (1)(1) =64 + 32 + 16 + 4 + 2 + 1 =119 26 repeated division example 29 10 in binary: 29 = 2 14 + 1 14 = 2 7+0 7=2 3+1 3=2 1+1 1=2 0+1 29 10 =11101 2 double check: 29 = (1)(2 4 )+(1)(2 3 )+(1)(2 2 )+(0)(2 1 )+(1)(2 0 ) = 16+8+4+1 Convert 119 10 to binary another example

another example yet another example Convert 119 10 to binary (I think that we ve done this one before.) solution: 111 0111 2 119 = 59 2+1 59 = 29 2+1 29 = 14 2+1 14 = 7 2+0 7=3 2+1 3=1 2+1 1=0 2+1 Convert 105 10 to binary 30 yet another example HEX Convert 105 10 to binary Solution: 110 1001 2 105 = 52 2+1 52 = 26 2+0 26 = 13 2+0 13 = 6 2+1 6=3 2+0 3=1 2+1 1=0 2+1 Bit strings get long More compact representation HEX 4 bits represented by 1 HEX digit 31

Hex. Memorize. dec hex bin 0 0 0 1 1 1 2 2 10 3 3 11 4 4 100 5 5 101 6 6 110 7 7 111 8 8 1000 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111 bits and groups of bits bit. binary digit. can either be a 0 or 1 8 bits are a byte 4 bits are nibble 2 bits are a half-nibble converting from binary to hex Break bit string into groups of 4 (starting from the right) Convert each 4-bit group to HEX Example: 10110100000101 10 1101 0000 0101 2 D 0 5 0010 1101 0000 0101 what s decimal 65,536 in hex? 65, 536 = 16 4096 + 0 4, 096 = 16 256 + 0 256 = 16 16 + 0 16 = 16 1+0 1=16 0+1 same as bin method: look at the remainders 65,536 = 0x10000

another example. 47. 47 = 16 2 + 15 2 = 16 0+2 47 10 in hex is 0x2F Note: 47 10 means 47 in base 10 Double check: 47 = (2)(16 1 ) + (15)(16 0 ) = (2)(16) + (15)(1) = 32 + 15 = 47 Another way to think about it: How do we count in base 10? What happens next? 0 1 2 3 4 5 6 7 8 9 38 Another way to think about it: How do we count in base 10? What happens next? 0 1 2 3 4 5 6 7 8 9 10 39 and then 0 1 2 9 10 11 99 100 999 1000 40

What about base 2? What about base 2? What happens next? 0 1 What happens next? 0 1 10 41 42 Representing Colors: RGB Three primary colors: red green blue Represent a color by strength of each primary one byte per color three bytes (or 6 hex digits) total HTML: <font color = #FF0000 > for red #00FF00 > for green #0000FF > for blue 43 All three of our bases dec bin hex dec bin hex dec bin hex 0 0 0 20 10100 14 40 101000 28 1 1 1 21 10101 15 41 101001 29 2 10 2 22 10110 16 42 101010 2a 3 11 3 23 10111 17 43 101011 2b 4 100 4 24 11000 18 44 101100 2c 5 101 5 25 11001 19 45 101101 2d 6 110 6 26 11010 1a 46 101110 2e 7 111 7 27 11011 1b 47 101111 2f 8 1000 8 28 11100 1c 48 110000 30 9 1001 9 29 11101 1d 49 110001 31 10 1010 a 30 11110 1e 50 110010 32 11 1011 b 31 11111 1f 51 110011 33 12 1100 c 32 100000 20 52 110100 34 13 1101 d 33 100001 21 53 110101 35 14 1110 e 34 100010 22 54 110110 36 15 1111 f 35 100011 23 55 110111 37 16 10000 10 36 100100 24 56 111000 38 17 10001 11 37 100101 25 57 111001 39 18 10010 12 38 100110 26 58 111010 3a 19 10011 13 39 100111 27 59 111011 3b

There s octal too Binary to octal dec bin oct hex dec bin oct hex dec bin oct hex 0 0 0 0 20 10100 24 14 40 101000 50 28 1 1 1 1 21 10101 25 15 41 101001 51 29 2 10 2 2 22 10110 26 16 42 101010 52 2a 3 11 3 3 23 10111 27 17 43 101011 53 2b 4 100 4 4 24 11000 30 18 44 101100 54 2c 5 101 5 5 25 11001 31 19 45 101101 55 2d 6 110 6 6 26 11010 32 1a 46 101110 56 2e 7 111 7 7 27 11011 33 1b 47 101111 57 2f 8 1000 10 8 28 11100 34 1c 48 110000 60 30 9 1001 11 9 29 11101 35 1d 49 110001 61 31 10 1010 12 a 30 11110 36 1e 50 110010 62 32 11 1011 13 b 31 11111 37 1f 51 110011 63 33 12 1100 14 c 32 100000 40 20 52 110100 64 34 13 1101 15 d 33 100001 41 21 53 110101 65 35 14 1110 16 e 34 100010 42 22 54 110110 66 36 15 1111 17 f 35 100011 43 23 55 110111 67 37 16 10000 20 10 36 100100 44 24 56 111000 70 38 17 10001 21 11 37 100101 45 25 57 111001 71 39 18 10010 22 12 38 100110 46 26 58 111010 72 3a 19 10011 23 13 39 100111 47 27 59 111011 73 3b Same idea as binary to hex: Start from RHS Break into groups of 3 3 bits to 1 octal digit Binary to octal Same idea as binary to hex: Start from RHS Break into groups of 3 3 bits to 1 octal digit 1010111101000100 1 010 111 101 000 100 > ls -la Unix Permissions and chmod drwxr-xr-x@ 7 jfiore staff 238 Aug 26 11:29. drwxr-xr-x@ 7 jfiore staff 238 Jul 29 15:31.. -rw-r--r--@ 1 jfiore staff 489 Jul 6 19:38 academic_calendar.csv -rw-r--r--@ 1 jfiore staff 635 Jul 12 12:02 exam_schedule_url.txt drwxr-xr-x@ 5 jfiore staff 170 Aug 26 08:49 office_sign -rw-r--r--@ 1 jfiore staff 163947 Mar 22 16:11 offic_tu_exam_sched.pdf -rw-r--r--@ 1 jfiore staff 922 Jul 12 11:34 schedule.csv 1 2 7 5 0 4

Unix Permissions and chmod Unix Permissions and chmod -rw-r--r--@ 1 jfiore staff 922 Jul 12 11:34 schedule.csv -rw-r--r--@ 1 jfiore staff 922 Jul 12 11:34 schedule.csv -rw-r--r-- user group other -rw-r--r-- user group other to get these permissions, could have typed: chmod 644 schedule.csv adding decimal numbers 1 2 3 4 + 9 3 7 adding decimal numbers 1 1 1 2 3 4 + 9 3 7 2 1 7 1 51 52

binary addition tables adding bit strings 0 + 0 0 0 + 1 1 1 + 0 1 1 0 1 1 0 + 0 0 1 1 1 1 + 1 1 0 1 1 + 1 1 1 53 54 adding bit strings 1 0 1 1 0 + 0 0 1 1 1 Adding Hex 6 B 9 7 7 16 + C A 9 2 16 1 1 1 0 1 1 0 + 0 0 1 1 1 1 1 1 0 1 55 56

Adding Hex 1 1 1 6 B 9 7 7 16 + C A 9 2 16 7 8 4 0 9 16 Another Example. 3 B B B 3 16 + E 8 1 4 16 57 58 Another Example. Solution. 1 1 3 B B B 3 16 + E 8 1 4 16 4 A 3 C 7 16 What happens? #include <stdio.h> int main(int argc, char **argv) { int prod = 200*300*400*500; printf("prod = %d\n", prod); prod = (200)*(300*400*500); printf("prod = %d\n", prod); prod = (200*300*400)*500; printf("prod = %d\n", prod); return 0; 59 60

How about in Java? Python? public class Overflow { public static void main(string args[]) { int prod = 200*300*400*500; System.out.println("prod = " + prod); prod = (200)*(300*400*500); System.out.println("prod = " + prod); prod = (200*300*400)*500; System.out.println("prod = " + prod); #!/usr/bin/env python prod = 200*300*400*500 print "prod =", prod prod = (200)*(300*400*500) print "prod =", prod prod = (200*300*400)*500; print "prod =", prod 61 62 what happens here? word sizes think width of the odometer word size fundamental system parameter nominal size of an int, pointer sizes: most machines today: 4 bytes high-end machines: 8 bytes so how much RAM can we address on each? be careful, Intel program documentation: word = 16 bits 63 64

aside: Intel programmer terms sizes byte word doubleword quadword double quadword 1 byte 2 bytes 4 bytes 8 bytes 16 bytes printf("sizeof(char)=%lu\n", sizeof(char)); printf("sizeof(short)=%lu\n", sizeof(short)); printf("sizeof(int)=%lu\n", sizeof(int)); printf("sizeof(long)=%lu\n", sizeof(long)); printf("sizeof(void*)=%lu\n", sizeof(void*)); 65 66 when I run on my laptop: sizeof(char)=1 sizeof(short)=2 sizeof(int)=4 sizeof(long)=4 sizeof(void*)=4 results sizeof(char)=1 sizeof(short)=2 sizeof(int)=4 sizeof(long)=8 sizeof(void*)=8 when I run on Temple CIS dept Linux box: NOT A A 0 1 1 0 67 68

Example: NOT A 11010010 A 00101101 A A 0 1 1 0 AND A B A&B 0 0 0 0 1 0 1 0 0 1 1 1 69 70 Example: AND A 11010010 B 01111010 A&B 01010010 A B A&B 0 0 0 0 1 0 1 0 0 1 1 1 OR A B A B 0 0 0 0 1 1 1 0 1 1 1 1 71 72

Example: A 11110000 B 00001111 A B 11111111 OR A B A B 0 0 0 0 1 1 1 0 1 1 1 1 XOR A B A B 0 0 0 0 1 1 1 0 1 1 1 0 73 74 XOR More on XOR Example: A 11111100 B 00111111 A B 11000011 A B A B 0 0 0 0 1 1 1 0 1 1 1 0 a XOR a =? a XOR b XOR b =? 75 76

More on XOR a XOR a = 0 a XOR b XOR b = a interesting trick void swap1(int *a, int *b) { int tmp = *a; *a=*b; *b=tmp; void swap2(int *a, int *b) { *a^=*b; /* a = a XOR b */ *b^=*a; *a^=*b; 77 78 some bit operators in C Example int x=9, y=5; printf("a=%d, NOT a=%d\n", a, ~a); printf("a=%d, b=%d, a AND b = %d\n", a, b, a&b); printf("a=%d, b=%d, a OR b = %d\n", a, b, a b); printf("a=%d, b=%d, a XOR b = %d\n", a, b, a^b); printf("x=%d, y=%d, x&y=%d\n", x, y, x&y); printf("x=%d, y=%d, x y=%d\n", x, y, x y); printf("x=%d, y=%d, x^y=%d\n", x, y, x^y); Output? 79

int x=9, y=5; Example printf("x&y=%d\n", x&y); printf("x y=%d\n", x y); printf("x^y=%d\n", x^y); Even or Odd In program how to tell if int is even or odd? What about the binary representation? Output? x&y=1 x y=13 x^y=12 Even or Odd int is_odd(int x) { return x%2==1; Even or Odd int is_odd(int x) { return x%2==1; int is_odd(int x) { return x&1==1;

Even or Odd int is_odd(int x) { return x%2==1; int is_odd(int x) { return x&1==1; int is_odd(int x) { return x&1; int is_odd(int x) { return x&1; Example: 9 0 0 0 0 1 0 0 1 & 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 Example: 6 Divisible by 8? int is_odd(int x) { return x&1; Same idea: how to tell if int is divisible by 8? What s the binary representation? 0 0 0 0 0 1 1 0 & 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

Counting by 8 0 0000 0000 8 0000 1000 16 0001 0000 24 0001 1000 32 0010 0000 40 0010 1000 48 0011 0000 56 0011 0000 Counting by 8 0 0000 0000 8 0000 1000 16 0001 0000 24 0001 1000 32 0010 0000 40 0010 1000 48 0011 0000 56 0011 1000 In C int div_by_8(int x) { return x%8==0; In C int div_by_8(int x) { return x%8==0; int div_by_8(int x) { return x & 7 == 0;

In C int div_by_8(int x) { return x%8==0; int div_by_8(int x) { return x & 7 == 0; int div_by_8(int x) { return!(x & 7); int div_by_8(int x) { return x & 7 == 0; Example: 24 0 0 0 1 1 0 0 0 & 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 int div_by_8(int x) { return x & 7 == 0; Example: 29 0 0 0 1 1 1 0 1 & 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 1 #include <stdio.h> 2 3 #define SMART 0x0001 4 #define HANDSOME 0x0010 5 #define FUNNY 0x0100 6 #define FUN_TO_BE_AROUND 0x1000 7 8 typedef int attr; 9 10 int issmart(attr); 11 int ishandsome(attr); 12 int isfunny(attr); 13 int isfuntobearound(attr); 14 15 int main(void) 16 { 17 attr you = SMART HANDSOME FUNNY; 48 18 19 printf("your attributes:"); 20 21 if (issmart(you)) 22 printf(" smart,"); 23 else 24 printf(" not smart,"); 25 26 if (ishandsome(you)) 27 printf(" handsome,"); 28 else 29 printf(" not handsome,"); 30 31 if (isfunny(you)) 32 printf(" funny,"); 33 else 34 printf(" not funny,"); 35 36 if (isfuntobearound(you)) 37 printf(" fun to be around\n"); 38 else 39 printf(" not fun to be around\n"); 40 41 return 0; 42 43 44 int issmart(attr a) 45 { 46 return a & SMART; 47 49 int ishandsome(attr a) 50 { 51 return a & HANDSOME; 52 53 54 int isfunny(attr a) 55 { 56 return a & FUNNY; 57 58 59 int isfuntobearound(attr a) 60 { 61 return a & FUN_TO_BE_AROUND; 62 96

reminder about logical operators don t confuse logical and bit ops! unsigned char x=0x31; 0x0 is false anything else is true printf("~x=0x%x\n", ~x); printf("~~x=0x%x\n", ~~x); printf("!x=0x%x\n",!x); printf("!!x=0x%x\n",!!x); 97 98 don t confuse logical and bit ops! unsigned char x=0x31; printf("~x=0x%x\n", ~x); printf("~~x=0x%x\n", ~~x); printf("!x=0x%x\n",!x); printf("!!x=0x%x\n",!!x); ~x=0xffffffce ~~x=0x31!x=0x0!!x=0x1 don t confuse logical and bit ops! ~x=0xffffffce ~~x=0x31!x=0x0!!x=0x1 0x31 = 00000000000000000000000000110001 2 0x31 = 11111111111111111111111111001110 2 99 100

What s this number? 657 What s this number? 657 six hundred fifty seven not seven hundred fifty six? 101 102 What s this number? Byte ordering 657 Most significant digit first six hundred fifty seven Least significant digit first seven hundred fifty six Big endian most significant byte first Examples: SPARC, old PowerPC Macs, Internet (aka network byte order ) Little endian least significant byte first Examples: x86, DEC Alpha 103 104

Byte ordering machine with 4 byte ints int i=0x01234567 big endian address value 1000 01 1001 23 1002 45 1003 67 little endian address value 1000 67 1001 45 1002 23 1003 01 Reminder: Don t confuse byte ordering with bit ordering 105 106 book s show_bytes( ) 1 typedef unsigned char *byte_pointer; 2 void show_bytes(byte_pointer start, int len) { 3 int i; 4 for (i = 0; i < len; i++) 5 printf(" %.2x", start[i]); 6 printf("\n"); 7 8 void show_int(int x) { 9 show_bytes((byte_pointer)&x,sizeof(int)); 10 11 void show_float(float x) { 12 show_bytes((byte_pointer)&x,sizeof(float)); 13 14 void show_pointer(void *x) { 15 show_bytes((byte_pointer)&x,sizeof(void*)); 16 107