the hardware/software interface › courses › cse351 › ... · the hardware/software interface...

147
The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang Lecture 3

Upload: others

Post on 07-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

The Hardware/Software Interface CSE351 Spring 2015

Instructor: Katelin Bailey

Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang

Lecture 3

Page 2: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Announcements

2

Page 3: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

Announcements

2

Page 4: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

Announcements

2

Page 5: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

• Lab 1 goes out today, by the end of class

Announcements

2

Page 6: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

• Lab 1 goes out today, by the end of class• Stuck on something? Confused at the end of class?

• Post on the discussion board• Come by office hours (mine or the TAs: posted online)• Send mail to [email protected]

Announcements

2

Page 7: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

• Lab 1 goes out today, by the end of class• Stuck on something? Confused at the end of class?

• Post on the discussion board• Come by office hours (mine or the TAs: posted online)• Send mail to [email protected]

• Readings for each lecture are posted on the website

Announcements

2

Page 8: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

• Lab 1 goes out today, by the end of class• Stuck on something? Confused at the end of class?

• Post on the discussion board• Come by office hours (mine or the TAs: posted online)• Send mail to [email protected]

• Readings for each lecture are posted on the website• Slides for each lecture are posted ~1 day ahead of time

• A 1-page summary of concepts and definitions will go up after class Not everything you need to know but most of the important topics.

Announcements

2

Page 9: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Roadmap

3

‣Memory, data, & addressing ‣ Integers & floats ‣Machine code & C ‣ x86 assembly ‣ Procedures & stacks ‣ Arrays & structs ‣Memory & caches ‣ Processes ‣ Virtual memory ‣Memory allocation ‣ Java vs. C

car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c);

Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG();

get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret

Java:C:

Assembly language:

Machine code: 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111

Computer system:

OS:

Page 10: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Brief review of topics from last lecture• Arrays and address arithmetic• Strings as arrays• Boolean algebra and bitwise manipulations

Today

4

Page 11: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Brief review of topics from last lecture • Arrays and address arithmetic• Strings as arrays• Boolean algebra and bitwise manipulations

Today

5

Page 12: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Binary, Hex, Decimal Format (review)

6

Page 13: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte = 8 bits (binary digits) = 2 hex digits

Binary, Hex, Decimal Format (review)

6

Page 14: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte = 8 bits (binary digits) = 2 hex digits

• 0x 00 00 01 5F is hex format • 4 bytes —> 32 bits

Binary, Hex, Decimal Format (review)

6

Page 15: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte = 8 bits (binary digits) = 2 hex digits

• 0x 00 00 01 5F is hex format • 4 bytes —> 32 bits

• In Binary?• 0000 0000 0000 0000 0000 0001 0101 1111• 101011111

Binary, Hex, Decimal Format (review)

6

Page 16: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte = 8 bits (binary digits) = 2 hex digits

• 0x 00 00 01 5F is hex format • 4 bytes —> 32 bits

• In Binary?• 0000 0000 0000 0000 0000 0001 0101 1111• 101011111

Binary, Hex, Decimal Format (review)

6

1 0 1 0 1 1 1 1 1 binary number8 7 6 5 4 3 2 1 0 bit numbering

28 27 26 25 24 23 22 21 20 bit numbering

Page 17: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte = 8 bits (binary digits) = 2 hex digits

• 0x 00 00 01 5F is hex format • 4 bytes —> 32 bits

• In Binary?• 0000 0000 0000 0000 0000 0001 0101 1111• 101011111

• In Decimal?• 28 + 26 + 24 + 23 + 22 + 21 + 20

• 256+64+16+8+4+2+1 =>351• Bits are numbered from 0!

Binary, Hex, Decimal Format (review)

6

1 0 1 0 1 1 1 1 1 binary number8 7 6 5 4 3 2 1 0 bit numbering

28 27 26 25 24 23 22 21 20 bit numbering

Page 18: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Words & Addresses (review)

7

Page 19: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A word is fixed number of contiguous bytes in memory, chosen by HW

• the largest unit of data a machine instruction can use

Words & Addresses (review)

7

Page 20: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A word is fixed number of contiguous bytes in memory, chosen by HW

• the largest unit of data a machine instruction can use• word size = address size = register size (Always!)

• take advantage of all bytes we’re given, can’t use more than we have!

Words & Addresses (review)

7

Page 21: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A word is fixed number of contiguous bytes in memory, chosen by HW

• the largest unit of data a machine instruction can use• word size = address size = register size (Always!)

• take advantage of all bytes we’re given, can’t use more than we have!• Word size bounds the size of the address space and memory.

• word size = w bits —> 2w addresses• 32-bit (4-byte) words —> 32-bit (4-byte) addresses —> 232 bytes of space• 64-bit (8-byte) words —> 64-bit (8-byte) addresses —> 264 bytes of space

Words & Addresses (review)

7

Page 22: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A word is fixed number of contiguous bytes in memory, chosen by HW

• the largest unit of data a machine instruction can use• word size = address size = register size (Always!)

• take advantage of all bytes we’re given, can’t use more than we have!• Word size bounds the size of the address space and memory.

• word size = w bits —> 2w addresses• 32-bit (4-byte) words —> 32-bit (4-byte) addresses —> 232 bytes of space• 64-bit (8-byte) words —> 64-bit (8-byte) addresses —> 264 bytes of space

• Doesn’t mean we have 232 bytes (4GB) of memory, just means we could find all of those bytes if we had them.

Words & Addresses (review)

7

Page 23: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

• Data of size n only stored at addresses a where a mod n = 0

• n is usually a power of 2.• A 32-bit (4-byte) word-aligned

view of memory:• Each row is a word composed of 4 bytes.• Cells in a row are the word’s bytes.

Memory Alignment (review)

8

More about alignment later in the course.

0x00 0x01 0x02 0x03(note hex addresses)

0x04 0x05 0x06 0x07

Page 24: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

• An address is a location in memory• A pointer is a data object that holds an address.• The value 351 is stored at address 0x04.

• 35110 = 15F16 = 0x00 00 01 5F• A pointer stored at address 0x1C

points to address 0x04.• A pointer to a pointer is stored at address 0x24. • The value 12 is stored

at address 0x14. • Is it a pointer?

Addresses and Pointers (review)

9

5F010000

04000000

1C000000

1C000000

Page 25: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

Big & Little Endian (review)

10

5F010000

04000000

1C000000

1C000000

Page 26: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

Big & Little Endian (review)

10

5F010000

04000000

1C000000

1C000000

Big-Endian

Page 27: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

Big & Little Endian (review)

10

5F010000

04000000

1C000000

1C000000

Little-Endian Big-Endian

Page 28: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

0x000x040x080x0C0x100x140x180x1C0x200x24

Big & Little Endian (review)

10

5F010000

04000000

1C000000

1C000000

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

00000004

0000001C

0000001C

Little-Endian Big-Endian

Page 29: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z

Assignment in C (review)

11

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z

Page 30: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z = &y + 3;

• // Get address of y, add 12 put it in z

Assignment in C (review)

12

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z00000024

Page 31: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z = &y + 3;

• // Get address of y, add 12 put it in z

Assignment in C (review)

12

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z

Pointer arithmetic is scaled by size of target type.

00000024

Page 32: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z = &y + 3;

• // Get address of y, add 12 put it in z

Assignment in C (review)

12

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z

Pointer arithmetic is scaled by size of target type.

00000024

0x18 = 24 (decimal) + 12 36 = 0x24

Page 33: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z = &y + 3;

• // Get address of y, add 12 put it in z

Assignment in C (review)

12

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z

Pointer arithmetic is scaled by size of target type.

Pointer arithmetic can be dangerous.

00000024

0x18 = 24 (decimal) + 12 36 = 0x24

Page 34: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Left-hand-side = right-hand-side;• LHS must evaluate to a memory location.• RHS must evaluate to a value. (Could be an address!)• Store RHS value at LHS location.

• int x, y;• x = 0;• y = 0x3CD02700;• x = y + 3;

• // Get value at y, add 3, put it in x.• int *z = &y + 3;

• // Get address of y, add 12 put it in z• *z = y;

• // Get value of y, put it at the address stored in z

Assignment in C (review)

13

0x000x040x080x0C0x100x140x180x1C0x200x24

3CD02703

3CD02700

& = ‘address of ’* = ‘value at address’ or ‘dereference’

x

y

z00000024

3CD02700

Page 35: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Brief review of topics from last lecture• Arrays and address arithmetic • Strings as arrays• Boolean algebra and bitwise manipulations

Today

14

Page 36: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

15

Declaration: int a[6]; a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

0x000x040x080x0C0x100x140x180x1C0x200x24

Page 37: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

15

Declaration: int a[6]; a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

element type

0x000x040x080x0C0x100x140x180x1C0x200x24

Page 38: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

15

Declaration: int a[6]; a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

element type

name

0x000x040x080x0C0x100x140x180x1C0x200x24

Page 39: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

15

Declaration: int a[6]; a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

element type

name

number of elements

0x000x040x080x0C0x100x140x180x1C0x200x24

Page 40: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

16

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

a[0]a[1]

a[5]

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

Page 41: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

16

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

Page 42: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

16

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

Page 43: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

16

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

Page 44: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

16

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

sizeof(int) = 4 =word size (convenient)

Page 45: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

17

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

No boundscheck:

Page 46: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

17

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

Page 47: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

17

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

Page 48: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

17

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

Page 49: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

18

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers:

Page 50: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

18

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers: int* p; p = a; p = &a[0];

Page 51: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

18

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers: int* p; p = a; p = &a[0];

equivalent {

Page 52: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

18

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers: int* p; p = a; p = &a[0];

equivalent {

00000004 p

Page 53: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

19

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000015F

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

00000004 p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

Page 54: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

20

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

00000004 p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

Page 55: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Arrays in C

21

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

00000004 p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

p[1] = 0xB;

Page 56: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

22

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

00000004 p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

p[1] = 0xB;

Page 57: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

23

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

00000004 p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

p[1] = 0xB; *(p + 1) = 0xB;

equivalent {

Page 58: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

24

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

equivalent {p[1] = 0xB; *(p + 1) = 0xB; p = p + 2; p00000004 p

Page 59: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

25

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BADPointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

equivalent {p[1] = 0xB; *(p + 1) = 0xB; p = p + 2; p0000000C

Page 60: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

26

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

equivalent {p[1] = 0xB; *(p + 1) = 0xB; p = p + 2; 0000000C

*p = a[1] + 1;

Page 61: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

array indexing = address arithmetic Both are scaled by the size of the type.

Arrays in C

27

Declaration: int a[6];

0x000x040x080x0C0x100x140x180x1C0x200x24

0000000A

0000015F

a[0]a[1]

a[5]

a[0] = 0x015f; a[5] = a[0];

Indexing: The address of a[i] is the address of a[0] plus i times the element size in bytes.

a is a name for the array’s address,not a pointer to the array.

Arrays are adjacent locations in memory storing the same type of data object.

a[6] = 0xBAD; a[-1] = 0xBAD;

No boundscheck:

00000BAD

00000BAD

p

Pointers:

equivalent {int* p; p = a; p = &a[0]; *p = 0xA;

0000000B

equivalent {p[1] = 0xB; *(p + 1) = 0xB; p = p + 2; 0000000C

*p = a[1] + 1;

0000000C

Page 62: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Brief review of topics from last lecture• Arrays and address arithmetic• Strings as arrays • Boolean algebra and bitwise manipulations

Today

28

Page 63: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A C-style string is represented by an array of bytes (char).— Elements are one-byte ASCII codes for each character. — ASCII = American Standard Code for Information Interchange

Representing strings

29

Page 64: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• A C-style string is represented by an array of bytes (char).— Elements are one-byte ASCII codes for each character. — ASCII = American Standard Code for Information Interchange

32 space 48 0 64 @ 80 P 96 ` 112 p33 ! 49 1 65 A 81 Q 97 a 113 q34 ” 50 2 66 B 82 R 98 b 114 r35 # 51 3 67 C 83 S 99 c 115 s36 $ 52 4 68 D 84 T 100 d 116 t37 % 53 5 69 E 85 U 101 e 117 u38 & 54 6 70 F 86 V 102 f 118 v39 ’ 55 7 71 G 87 W 103 g 119 w40 ( 56 8 72 H 88 X 104 h 120 x41 ) 57 9 73 I 89 Y 105 I 121 y42 * 58 : 74 J 90 Z 106 j 122 z43 + 59 ; 75 K 91 [ 107 k 123 {44 , 60 < 76 L 92 \ 108 l 124 |45 - 61 = 77 M 93 ] 109 m 125 }46 . 62 > 78 N 94 ^ 110 n 126 ~47 / 63 ? 79 O 95 _ 111 o 127 del

Representing strings

29

Page 65: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Null-terminated Strings

30

Page 66: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• For example, “Harry Potter” can be stored as a 13-byte array.

Null-terminated Strings

30

Page 67: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• For example, “Harry Potter” can be stored as a 13-byte array.

Null-terminated Strings

72 97 114 114 121 32 80 111 116 116 101 114 0

H a r r y P o t t e r \0

30

Page 68: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• For example, “Harry Potter” can be stored as a 13-byte array.

• Why do we put a 0, or null zero, at the end of the string?• Note the special symbol: string[12] = '\0';

Null-terminated Strings

72 97 114 114 121 32 80 111 116 116 101 114 0

H a r r y P o t t e r \0

30

Page 69: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• For example, “Harry Potter” can be stored as a 13-byte array.

• Why do we put a 0, or null zero, at the end of the string?• Note the special symbol: string[12] = '\0';

• How do we compute the string length?

Null-terminated Strings

72 97 114 114 121 32 80 111 116 116 101 114 0

H a r r y P o t t e r \0

30

Page 70: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Endianness and Strings

31

Page 71: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte ordering (endianness) is not an issue for 1-byte values.• Arrays are not values; elements are values; chars are single bytes.

Endianness and Strings

31

Page 72: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte ordering (endianness) is not an issue for 1-byte values.• Arrays are not values; elements are values; chars are single bytes.

• Unicode characters – up to 4 bytes/character• ASCII codes still work (just add leading zeros).

Unicode can support the many characters in all languages in the world.• Java and C have libraries for Unicode (Java commonly uses 2 bytes/char)

Endianness and Strings

31

Page 73: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte ordering (endianness) is not an issue for 1-byte values.• Arrays are not values; elements are values; chars are single bytes.

• Unicode characters – up to 4 bytes/character• ASCII codes still work (just add leading zeros).

Unicode can support the many characters in all languages in the world.• Java and C have libraries for Unicode (Java commonly uses 2 bytes/char)

Endianness and Strings

31

char s[6] = "12345";

IA32, x86-64 SPARC

3334

3132

3500

3334

3132

3500

C (char = 1 byte)

Page 74: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Byte ordering (endianness) is not an issue for 1-byte values.• Arrays are not values; elements are values; chars are single bytes.

• Unicode characters – up to 4 bytes/character• ASCII codes still work (just add leading zeros).

Unicode can support the many characters in all languages in the world.• Java and C have libraries for Unicode (Java commonly uses 2 bytes/char)

Endianness and Strings

31

char s[6] = "12345";

IA32, x86-64 SPARC

3334

3132

3500

3334

3132

3500

C (char = 1 byte)String s = "123";

IA32, x86-64 SPARC

0032

0031

0033

3200

3100

3300

Java (char = 2 bytes)

(not all of the String representation is shown)

?

Page 75: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Brief review of topics from last lecture• Arrays and address arithmetic• Strings as arrays• Boolean algebra and bitwise manipulations

Today

32

Page 76: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Code to print byte representation of data• Any data type can be treated as a byte array by casting it to char.

• C has unchecked casts. << DANGER >>

Examining Data Representations

printf directives:%p Print pointer \t Tab

%x Print value as hex \n New line

33

Page 77: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Code to print byte representation of data• Any data type can be treated as a byte array by casting it to char.

• C has unchecked casts. << DANGER >>

Examining Data Representations

typedef char byte; // size of char == 1 byte

void show_bytes(byte* start, int len) { int i; for (i = 0; i < len; i++) printf("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); }

printf directives:%p Print pointer \t Tab

%x Print value as hex \n New line

33

Page 78: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Code to print byte representation of data• Any data type can be treated as a byte array by casting it to char.

• C has unchecked casts. << DANGER >>

Examining Data Representations

typedef char byte; // size of char == 1 byte

void show_bytes(byte* start, int len) { int i; for (i = 0; i < len; i++) printf("%p\t0x%.2x\n", start+i, *(start+i)); printf("\n"); }

printf directives:%p Print pointer \t Tab

%x Print value as hex \n New line

33

void show_int (int x) { show_bytes( (byte *) &x, sizeof(int)); }

Page 79: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

show_bytes Execution Example

34

Page 80: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

show_bytes Execution Example

int a = 12345; // represented as 0x00003039 printf("int a = 12345;\n"); show_int(a); // show_bytes((pointer) &a, sizeof(int));

34

Page 81: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

show_bytes Execution Example

int a = 12345; // represented as 0x00003039 printf("int a = 12345;\n"); show_int(a); // show_bytes((pointer) &a, sizeof(int));

Result (Linux):int a = 12345; 0x11ffffcb8 0x39 0x11ffffcb9 0x30 0x11ffffcba 0x00 0x11ffffcbb 0x00

34

Page 82: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Boolean Algebra

35

Page 83: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

Boolean Algebra

35

Page 84: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic

Boolean Algebra

35

Page 85: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

Boolean Algebra

35

Page 86: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1

Boolean Algebra

35

Page 87: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1

Boolean Algebra

35

Page 88: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both

Boolean Algebra

35

Page 89: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa

Boolean Algebra

35

Page 90: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa• DeMorgan’s Law: ~(A | B) = ~A & ~B

Boolean Algebra

35

Page 91: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa• DeMorgan’s Law: ~(A | B) = ~A & ~B

& 0 10 0 01 0 1

Boolean Algebra

35

Page 92: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa• DeMorgan’s Law: ~(A | B) = ~A & ~B

& 0 10 0 01 0 1

| 0 10 0 11 1 1

Boolean Algebra

35

Page 93: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa• DeMorgan’s Law: ~(A | B) = ~A & ~B

& 0 10 0 01 0 1

| 0 10 0 11 1 1

^ 0 10 0 11 1 0

Boolean Algebra

35

Page 94: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Developed by George Boole in 19th Century

• Algebraic representation of logic• Encode “True” as 1 and “False” as 0

• AND: A&B = 1 when both A is 1 and B is 1• OR: A|B = 1 when either A is 1 or B is 1• XOR: A^B = 1 when either A is 1 or B is 1, but not both• NOT: ~A = 1 when A is 0 and vice-versa• DeMorgan’s Law: ~(A | B) = ~A & ~B

& 0 10 0 01 0 1

~0 11 0

| 0 10 0 11 1 1

^ 0 10 0 11 1 0

Boolean Algebra

35

Page 95: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

General Boolean Algebras

36

Page 96: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

General Boolean Algebras

36

Page 97: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

01101001 & 01010101 01000001

General Boolean Algebras

36

Page 98: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

01101001 & 01010101 01000001

01101001 | 01010101 01111101

General Boolean Algebras

36

Page 99: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

01101001 & 01010101 01000001

01101001 | 01010101 01111101

01101001 ^ 01010101 00111100

General Boolean Algebras

36

Page 100: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

01101001 & 01010101 01000001

01101001 | 01010101 01111101

01101001 ^ 01010101 00111100

~ 01010101 10101010

General Boolean Algebras

36

Page 101: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

• All of the properties of Boolean algebra apply

01101001 & 01010101 01000001

01101001 | 01010101 01111101

01101001 ^ 01010101 00111100

~ 01010101 10101010

General Boolean Algebras

36

Page 102: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

• All of the properties of Boolean algebra apply

01101001 & 01010101 01000001

01101001 | 01010101 01111101

01101001 ^ 01010101 00111100

~ 01010101 10101010

01010101 ^ 01010101 00000000

General Boolean Algebras

36

Page 103: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Operate on bit vectors• Operations applied bitwise

• All of the properties of Boolean algebra apply

• How does this relate to set operations?

01101001 & 01010101 01000001

01101001 | 01010101 01111101

01101001 ^ 01010101 00111100

~ 01010101 10101010

01010101 ^ 01010101 00000000

General Boolean Algebras

36

Page 104: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Representing & Manipulating Sets

37

Page 105: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation

Representing & Manipulating Sets

37

Page 106: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}

Representing & Manipulating Sets

37

Page 107: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

Representing & Manipulating Sets

37

Page 108: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 }

Representing & Manipulating Sets

37

Page 109: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

Representing & Manipulating Sets

37

Page 110: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

Representing & Manipulating Sets

37

Page 111: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 }

Representing & Manipulating Sets

37

Page 112: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

Representing & Manipulating Sets

37

Page 113: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

• Operations

Representing & Manipulating Sets

37

Page 114: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

• Operations• & Intersection 01000001 { 0, 6 }

Representing & Manipulating Sets

37

Page 115: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

• Operations• & Intersection 01000001 { 0, 6 } • | Union 01111101 { 0, 2, 3, 4, 5, 6 }

Representing & Manipulating Sets

37

Page 116: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

• Operations• & Intersection 01000001 { 0, 6 } • | Union 01111101 { 0, 2, 3, 4, 5, 6 } • ^ Symmetric difference 00111100 { 2, 3, 4, 5 }

Representing & Manipulating Sets

37

Page 117: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Representation• A w-bit vector represents subsets of {0, …, w–1}• aj = 1 iff j ∈ A

01101001 { 0, 3, 5, 6 } 76543210

01010101 { 0, 2, 4, 6 } 76543210

• Operations• & Intersection 01000001 { 0, 6 } • | Union 01111101 { 0, 2, 3, 4, 5, 6 } • ^ Symmetric difference 00111100 { 2, 3, 4, 5 } • ~ Complement 10101010 { 1, 3, 5, 7 }

Representing & Manipulating Sets

37

Page 118: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Bit-Level Operations in C

38

Page 119: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~

Bit-Level Operations in C

38

Page 120: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type

Bit-Level Operations in C

38

Page 121: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

Bit-Level Operations in C

38

Page 122: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors

Bit-Level Operations in C

38

Page 123: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)

Bit-Level Operations in C

38

Page 124: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

Bit-Level Operations in C

38

Page 125: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

Bit-Level Operations in C

38

Page 126: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF

Bit-Level Operations in C

38

Page 127: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

Bit-Level Operations in C

38

Page 128: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

• 0x69 & 0x55 --> 0x41

Bit-Level Operations in C

38

Page 129: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

• 0x69 & 0x55 --> 0x41011010012 & 010101012 --> 010000012

Bit-Level Operations in C

38

Page 130: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

• 0x69 & 0x55 --> 0x41011010012 & 010101012 --> 010000012

• 0x69 | 0x55 --> 0x7D

Bit-Level Operations in C

38

Page 131: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

• 0x69 & 0x55 --> 0x41011010012 & 010101012 --> 010000012

• 0x69 | 0x55 --> 0x7D011010012 | 010101012 --> 011111012

Bit-Level Operations in C

38

Page 132: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• & | ^ ~• Apply to any “integral” data type• long, int, short, char, unsigned

• View arguments as bit vectors• Examples (char data type)• ~0x41 --> 0xBE

~010000012 --> 101111102

• ~0x00 --> 0xFF~000000002 --> 111111112

• 0x69 & 0x55 --> 0x41011010012 & 010101012 --> 010000012

• 0x69 | 0x55 --> 0x7D011010012 | 010101012 --> 011111012

• Many bit-twiddling puzzles in Lab 1

Bit-Level Operations in C

38

Page 133: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

Contrast: Logic Operations in C

39

Page 134: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators

Contrast: Logic Operations in C

39

Page 135: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

Contrast: Logic Operations in C

39

Page 136: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

Contrast: Logic Operations in C

39

Page 137: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

Contrast: Logic Operations in C

39

Page 138: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

Contrast: Logic Operations in C

39

Page 139: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

Contrast: Logic Operations in C

39

Page 140: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)

Contrast: Logic Operations in C

39

Page 141: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

Contrast: Logic Operations in C

39

Page 142: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

• !0x00 --> 0x01

Contrast: Logic Operations in C

39

Page 143: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

• !0x00 --> 0x01

• !!0x41 --> 0x01

Contrast: Logic Operations in C

39

Page 144: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

• !0x00 --> 0x01

• !!0x41 --> 0x01

• 0x69 && 0x55 --> 0x01

Contrast: Logic Operations in C

39

Page 145: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

• !0x00 --> 0x01

• !!0x41 --> 0x01

• 0x69 && 0x55 --> 0x01

• 0x69 || 0x55 --> 0x01

Contrast: Logic Operations in C

39

Page 146: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Contrast to logical operators• && || !

• 0 is “False”

• Anything nonzero is “True”

• Always return 0 or 1

• Early termination a.k.a. short-circuit evaluation

• Examples (char data type)• !0x41 --> 0x00

• !0x00 --> 0x01

• !!0x41 --> 0x01

• 0x69 && 0x55 --> 0x01

• 0x69 || 0x55 --> 0x01

• p && *p++ (avoids null pointer access, null pointer = x00000000 )

Contrast: Logic Operations in C

39

Page 147: The Hardware/Software Interface › courses › cse351 › ... · The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt,

• Everyone waiting to get in should have heard by now• If we talked about auditing, please send me mail so I have your UWnetIDs

• Lab 0 due on Monday, still having fun?• Covering arrays today in lecture

• Lab 1 goes out today, by the end of class• Stuck on something? Confused at the end of class?

• Post on the discussion board• Come by office hours (mine or the TAs: posted online)• Send mail to [email protected]

• Readings for each lecture are posted on the website• Slides for each lecture are posted ~1 day ahead of time

• A 1-page summary of concepts and definitions will go up after class Not everything you need to know but most of the important topics.

Announcements

40