Docsity

Bits, Bytes, and Integers, Lecture notes of C programming

Partial preview of the text

Download Bits, Bytes, and Integers and more Lecture notes C programming in PDF only on Docsity!

Carnegie Mellon

Bits, Bytes, and IntegersBits,

Bytes,

and

Integers

Introduction

to

Computer

Systems

p^

y

nd 2

and

rd Lectures,

Jan

and

Jan

Instructors: Todd

C.

Mowry

Anthony

Rowe

Carnegie Mellon

Today:

Bits,

Bytes,

and

Integers

y

,^

y

,^

g

Representing

information

as

bits

Bit l

l^

i^

l ti

Bit

‐l

evel

manipulations

Integers^ 

Representation: unsigned and signedRepresentation:

unsigned

and

signed

^

Conversion,

casting

^

Expanding,

truncating

^

Additi

ti^

lti li

ti^

hifti

^

Addition,

negation,

multiplication,

shifting

^

Summary

Representations

in

memory,

pointers,

strings

Carnegie Mellon

Encoding

Byte

Values

g

y

Byte

bits

^

Binary 00000000

to 11111111

^

Binary

to 2

2

^

Decimal:

10

to

10

^

Hexadecimal

16

to

FF

16

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

^

Base

number

representation

^

Use

characters

to

and

‘A’

to

‘F’

^

Write

FA1D37B

16

in

C

as

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

6

  • 0xFA1D37B– 0xfa1d37b

8

8

1000

9

9

1001

A^

10

1010

B^

11

1011

C^

12

1100

C^

12

1100

D^

13

1101

E^

14

1110

F^

15

1111

Carnegie Mellon

Data

Representations

p

C Data Type

Typical 32-bit

Intel IA

x86-

char

1

1

1

short

2

2

2

int

4

4

4

long

4

4

8

long long

8

8

8

float

4

4

4

double

8

8

8

long double

8

10/

10/

i t

4

4

8

pointer

4

4

8

Carnegie Mellon

Boolean

Algebra

g

Developed

by

George

Boole

in

19th

Century

^

Algebraic representation of logic

^

Algebraic

representation

of

logic

^

Encode

“True”

as

and

“False”

as

And

Or

^

A&B

when

both

A=

and

B=

^

A|B

when

either

A=

or

B=

Not

Exclusive

‐Or (

Xor

Not ^

~A

when

A=

Exclusive Or

Xor

^

A^B

when

either

A=

or

B=1,

but

not

both

Carnegie Mellon

General

Boolean

Algebras

g

Operate

on

Bit

Vectors

^

Operations applied bitwise

^

Operations

applied

bitwise

^

All

of

the

Properties

of

Boolean

Algebra

Apply

Carnegie Mellon

Bit

‐Level

Operations

in

C

p

Operations

,^

,^

^

Available

in

C

^

Apply to any “integral” data type

^

Apply

to

any

“integral”

data

type

^

long, int, short, char, unsigned

^

View

arguments

as

bit

vectors

^

Arguments

applied

bit

‐wise

Examples

(Char

data

type)

^

~0x

0

xBE

0

0

^

~

2 

10111110

2

^

~0x

0xFF

^

~

2 

11111111

2

00000000

2 

11111111

2

^

0x69 & 0x

0x

^

01101001

& 01010101 2

2 

01000001

2

^

0x69 | 0x

0x7D

0x

| 0x

0x7D

^

01101001

| 01010101 2

01111101

2

Carnegie Mellon

Contrast:

Logic

Operations

in

C

g

p

Contrast

to

Logical

Operators

^

^

^

View

as

“False”

^

Anything

nonzero

as

“True”

^

Always

return

or

^

Early

termination

Examples (char data type)

Examples

(char

data

type)

^

!0x

0x

^

!0x

0x

^

!!0x

0x

^

!!0x

0x

^

0x69 && 0x

0x

^

0x69 || 0x

0x

||

^

p && *p

(avoids

null

pointer

access)

Carnegie Mellon

Shift

Operations

p

Left

Shift:

x << y

^

Shift bit

vector

x

left

y

positions

01100010

Argument

x

^

Shift

bit

‐vector

x

left

y

positions

  • Throw

away

extra

bits

on

left

^

Fill

with

0

’s

on

right

00010

000

<< 3

00

011000

Log.

>> 2

00

011000

Arith

>> 2

00010

000

00010

000

00

011000 00

011000 00

011000 00

011000

Right

Shift:

x >> y

^

Shift

bit

‐vector

x

right

y

positions

^

Throw away extra bits on right

00

011000

Arith

.^ >>

2

10100010

Argument

x

00

011000 00

011000

^

Throw

away

extra

bits

on

right

^

Logical

shift

^

Fill

with

0

’s

on left

g

00010

000

<< 3

00

101000

Log.

>> 2

00010

000

00

101000 00010

000

00

101000

^

Arithmetic

shift

^

Replicate

most

significant

bit

on left

Undefined

Behavior

11

101000

Arith.

>> 2

11

101000 11

101000

^

Shift

amount

or

word

size

Carnegie Mellon

Today:

Bits,

Bytes,

and

Integers

y

,^

y

,^

g

Representing

information

as

bits

Bit l

l^

i^

l ti

Bit

‐l

evel

manipulations

Integers^ 

Representation: unsigned and signedRepresentation:

unsigned

and

signed

^

Conversion,

casting

^

Expanding,

truncating

^

Additi

ti^

lti li

ti^

hifti

^

Addition,

negation,

multiplication,

shifting

^

Summary

Representations

in

memory,

pointers,

strings

Summary

Carnegie Mellon

Encoding

Example

(Cont.)

x =

15213: 00111011 01101101

y =

-15213: 11000100 10010011

Weight

15213

‐^15213

Weight

15213

‐^15213

1

1

1

1

1

2

0

0

1

2

4

1

4

0

0

8

1

8

0

0

16

0

0

1

16

32

1

32

0

0

64

1

64

0

0

64

1

64

0

0

128

0

0

1

128

256

1

256

0

0

512

1

512

0

0

1024

0

0

1

1024

2048

1

2048

0

0

4096

1

4096

0

0

8192

1

8192

0

0

8192

1

8192

0

0

16384

0

0

1

16384

‐^32768

0

0

1

‐^32768

Sum

15213

‐^15213

Carnegie Mellon

Numeric

Ranges

g

^

Unsigned

Values

^

UMin

=^

^

Two’s

Complement

Values

^

TMin

=^

–^2

w –

1

000…

^

UMax

=^

w 2

111…

TMin

=^

100…

^

TMax

=^

w 2

011

1

011…

^

Other

Values

^

Minus

111…

Decimal

Hex

Binary

Values

for

W

y

UMax

65535

FF

FF

11111111

11111111

TMax

32767

7 F

FF

01111111

11111111

TMin

-

80

00

10000000

00000000

1

1

FF

FF

11111111

11111111

- -

FF

FF

11111111

11111111

0

0

00

00

00000000

00000000

Carnegie Mellon

Unsigned

Signed

Numeric

Values

Equivalence^ 

Same

encodings

for

nonnegative

l

X^

B2T(

X )

B2U(

X )

0000

0

0001

1

0 1

values

Uniqueness^ 

Every

bit

pattern

represents

0001

1

0010

2

0011

3

0100

4

1 2 3 4

unique

integer

value

^

Each

representable integer

has

unique

bit

encoding

0100

4

0101

5

0110

6

0111

7

4 5 6 7

Can

Invert

Mappings

^

U2B(

x )

B2U

‐^1 (

x )

Bit

tt

f^

i^

d

8

9

10

100010011010

^

Bit

pattern

for

unsigned

integer

^

T2B(

x )

B2T

‐^1 (

x )

11

12

13

2

14

1011110011011110

^

Bit

pattern

for

two’s

comp

integer

14

15

11101111

Carnegie Mellon

Today:

Bits,

Bytes,

and

Integers

y

,^

y

,^

g

Representing

information

as

bits

Bit l

l^

i^

l ti

Bit

‐l

evel

manipulations

Integers^ 

Representation: unsigned and signedRepresentation:

unsigned

and

signed

^

Conversion,

casting

^

Expanding,

truncating

^

Additi

ti^

lti li

ti^

hifti

^

Addition,

negation,

multiplication,

shifting

^

Summary

Representations

in

memory,

pointers,

strings