24/06/2015cse1303 part b lecture notes 1 words, bits and pieces lecture b05 lecture notes section...

42
06/16/22 CSE1303 Part B lecture notes 1 Words, bits and Words, bits and pieces pieces Lecture B05 Lecture B05 Lecture notes section B05 Lecture notes section B05

Post on 21-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 1

Words, bits and Words, bits and piecespieces

Lecture B05Lecture B05

Lecture notes section B05Lecture notes section B05

Page 2: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 2

Last timeLast time

Floating pointFloating point Anatomy of floating pointAnatomy of floating point

signsign exponentexponent mantissamantissa

Floating point arithmeticFloating point arithmetic multiplicationmultiplication additionaddition

Limitations of floating pointLimitations of floating point

Page 3: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 3

In this lectureIn this lecture

Working with large dataWorking with large data byte orderbyte order

Working with small dataWorking with small data bitwise operationsbitwise operations

• AND, OR, NOT, XORAND, OR, NOT, XOR• maskingmasking

shiftingshifting• relationship to multiplication and divisionrelationship to multiplication and division

Page 4: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 4

Byte orderByte order

ProblemProblem memory is addressed by bytes (8 memory is addressed by bytes (8

bits)bits) how to store a 32-bit value in how to store a 32-bit value in

memory?memory?0x13579BCF0x13579BCF

32-bit value 32-bit value (written in hex (written in hex for compact-for compact-

ness, but really ness, but really in binary)in binary)

ad

dre

sses

ad

dre

sses

12481248

12491249

12501250

12511251

......

......

??

8 bits8 bits

Page 5: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 5

Byte orderByte order

SolutionSolution break up value into byte-sized chunksbreak up value into byte-sized chunks store each chunk in successive bytesstore each chunk in successive bytes

0x13579BCF0x13579BCF

each hex digit each hex digit represents four represents four bits, so two hex bits, so two hex digits to a bytedigits to a byte

ad

dre

sses

ad

dre

sses

12481248

12491249

12501250

12511251

......

......

one byte one byte herehere

next byte next byte herehere

another another byte herebyte here

final byte final byte herehere

8 bits8 bits

Page 6: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 6

Byte orderByte order

Which order to store the bytes?Which order to store the bytes? starting at the starting at the most significantmost significant (“ (“bigbig”) ”)

endend??

ad

dre

sses

ad

dre

sses

12481248

12491249

12501250

12511251

......

......

0x13579BCF0x13579BCF

0x130x13

0x570x57

0x9B0x9B

0xCF0xCFwork this waywork this way

Page 7: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 7

Byte orderByte order

Which order to store the bytes?Which order to store the bytes? starting at the starting at the least significantleast significant

(“(“littlelittle”) ”) endend??

ad

dre

sses

ad

dre

sses

12481248

12491249

12501250

12511251

......

......

0x13579BCF0x13579BCF

0x130x130x570x570x9B0x9B0xCF0xCF

work this waywork this way

Page 8: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 8

Byte orderByte order

Two common ways of storing word into Two common ways of storing word into memorymemory

Big EndianBig Endian store store most significant bytemost significant byte in in lowest addresslowest address store least significant byte in highest store least significant byte in highest

addressaddress

Little EndianLittle Endian store store least significant byteleast significant byte in in lowest addresslowest address store most significant byte in highest store most significant byte in highest

addressaddress

Page 9: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 9

Byte orderByte order

Which byte order is used depends on the Which byte order is used depends on the computer’s Central Processing Unit (CPU)computer’s Central Processing Unit (CPU) Big EndianBig Endian

• Motorola 680x0 (Amiga/Atari ST/Mac classic)Motorola 680x0 (Amiga/Atari ST/Mac classic)• IBM/Motorola PowerPC (current Macintosh)IBM/Motorola PowerPC (current Macintosh)• MIPS (SGI Indy/Nintendo 64)MIPS (SGI Indy/Nintendo 64)• Motorola 680X familyMotorola 680X family

Little EndianLittle Endian• Intel 80x86/Pentium (IBM PC compatible)Intel 80x86/Pentium (IBM PC compatible)• Rockwell 6502 (Commodore 64)Rockwell 6502 (Commodore 64)• MIPS (Sony Playstation/Digital DECstation)MIPS (Sony Playstation/Digital DECstation)

Some processors (e.g., MIPS) can be configured Some processors (e.g., MIPS) can be configured to be either Big Endian or Little Endianto be either Big Endian or Little Endian

Page 10: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 10

Logic operationsLogic operations

Boolean logic operationsBoolean logic operations operate on one or two operate on one or two binarybinary values values

(true/false, 1/0)(true/false, 1/0) logical ANDlogical AND (on two values) (on two values)

• C operator: C operator: &&&&• result is true if both operands are true, else falseresult is true if both operands are true, else false

logical ORlogical OR (on two values) (on two values)• C operator: C operator: ||||• result is true if either operand is true, else falseresult is true if either operand is true, else false

logical NOTlogical NOT (on one value) (on one value)• C operator: C operator: !!• invert operand: true invert operand: true false false

Page 11: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 11

Bitwise operationsBitwise operations

Bitwise logic operationsBitwise logic operations operate on operate on each biteach bit of one or two values of one or two values bitwise ANDbitwise AND (on two values) (on two values)

• for each bit:for each bit: result is 1 if both operands are 1, else 0 result is 1 if both operands are 1, else 0 bitwise ORbitwise OR (on two values) (on two values)

• for each bit:for each bit: result is 1 if either operand is 1, else 0 result is 1 if either operand is 1, else 0 bitwise Exclusive ORbitwise Exclusive OR (“XOR”) (on two values) (“XOR”) (on two values)

• for each bit:for each bit: result is 1 if either operand (but not result is 1 if either operand (but not both) is 1, else 0both) is 1, else 0

bitwise NOTbitwise NOT (on one value) (on one value)• for each bit:for each bit: invert (or “flip”) the bit: 1 invert (or “flip”) the bit: 1 0 0• like flip in “flip_and_add_1” negation of signed valuelike flip in “flip_and_add_1” negation of signed value

Page 12: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 12

Bitwise operations: NOTBitwise operations: NOT

Bitwise NOTBitwise NOT Invert every bit in a valueInvert every bit in a value

00 11 11 00 11 00 11 00

11 00 00 11 00 11 00 11

each bit inverted each bit inverted independently of the othersindependently of the others

truth truth tabletable

inin

ou

tou

t

00 11

11 00

Page 13: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 13

Bitwise operations: ANDBitwise operations: AND

Bitwise ANDBitwise AND performs logical performs logical ANDAND on on

corresponding bits of two valuescorresponding bits of two values

result is 1 if result is 1 if bothboth corresponding corresponding input bits are 1, 0 otherwiseinput bits are 1, 0 otherwise

truth truth tabletable

00 11 11 00 11 00 11 0011 11 00 00 00 00 11 11

00 11 00 00 00 00 11 00

in 1

in 1

in 2

in 2

ou

tou

t

00 00 00

00 11 00

11 00 00

11 11 11

Page 14: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 14

Bitwise operations: ORBitwise operations: OR

Bitwise ORBitwise OR performs logical performs logical OROR on corresponding on corresponding

bits of two valuesbits of two values

result is 1 if result is 1 if eithereither corresponding corresponding input bit (or both) are 1, 0 input bit (or both) are 1, 0

otherwiseotherwise

truth truth tabletable

00 11 11 00 11 00 11 0011 11 00 00 00 00 11 11

11 11 11 00 11 00 11 11

in 1

in 1

in 2

in 2

ou

tou

t

00 00 00

00 11 11

11 00 11

11 11 11

Page 15: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 15

Bitwise operations: XORBitwise operations: XOR

Bitwise Exclusive OR (XOR)Bitwise Exclusive OR (XOR) performs logical performs logical Exclusive ORExclusive OR (XOR) (XOR)

on corresponding bits of two valueson corresponding bits of two values

result is 1 if result is 1 if eithereither corresponding corresponding input bit (but input bit (but not bothnot both) are 1, 0 ) are 1, 0

otherwiseotherwisetruth truth tabletable

00 11 11 00 11 00 11 0011 11 00 00 00 00 11 11

11 00 11 00 11 00 00 11

in 1

in 1

in 2

in 2

ou

tou

t

00 00 00

00 11 11

11 00 11

11 11 00

Page 16: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 16

Bitwise operationsBitwise operations

Some operations don’t affect bitsSome operations don’t affect bits yy AND AND 11 = = yy yy OR OR 00 = = yy yy XOR XOR 00 = = yy

Some operations force bits to 0 or 1Some operations force bits to 0 or 1 yy AND AND 00 = = 00 yy OR OR 11 = = 11

Some operations change bitsSome operations change bits yy XOR XOR 11 = not_ = not_yy

Page 17: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 17

Extracting bitsExtracting bits

Sometimes need to isolate one or Sometimes need to isolate one or more bits in a valuemore bits in a value e.g., find sign in signed e.g., find sign in signed intint//floatfloat

1001101010011010

extract this bit to extract this bit to find out if value find out if value

is positive or is positive or negativenegative

but need to ignore but need to ignore all the other bitsall the other bits

Page 18: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 18

Extracting bitsExtracting bits

Use bitwise Use bitwise ANDAND to to extractextract bits bits for desired bits, AND with 1for desired bits, AND with 1

• 00 AND 1 AND 1 00, , 11 AND 1 AND 1 11• bit remains unchangedbit remains unchanged

for other bits, AND with 0for other bits, AND with 0• 00 AND 0 AND 0 00, , 11 AND 0 AND 0 00• bit is cleared (set to 0)bit is cleared (set to 0)

only desired bits remain, others are only desired bits remain, others are clearedcleared

Page 19: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 19

Extracting bitsExtracting bits

Example: extract sign bit of C Example: extract sign bit of C floatfloat sign bitsign bit

ANDAND

00 0110101101101011 1100110110100011001100011001101101000110011000

11 0000000000000000 0000000000000000000000000000000000000000000000

00 0000000000000000 0000000000000000000000000000000000000000000000

a 1 in a 1 in this this

column column allows allows

the sign the sign bit bit

throughthrough

zeroes zeroes in these in these columns columns

stop stop any any

value value from from

getting getting throughthrough

resulting value is zero: resulting value is zero: number is positivenumber is positive

Page 20: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 20

Extracting bitsExtracting bits

Example: extract sign bit of C Example: extract sign bit of C floatfloat

ANDAND

00 0110101101101011 1100110110100011001100011001101101000110011000

11 0000000000000000 0000000000000000000000000000000000000000000000

a value like this which is used to a value like this which is used to extract, set or clear individual bits is extract, set or clear individual bits is

usually called a usually called a maskmask

Page 21: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 21

Clearing bitsClearing bits

Sometimes need to clear a bit to zeroSometimes need to clear a bit to zero e.g., clear sign bit of a e.g., clear sign bit of a floatfloat, to make , to make

positive (like positive (like fabsfabs function) function)

Use bitwise Use bitwise ANDAND to to clearclear bits bits for bits to clear, AND with 0for bits to clear, AND with 0

• 00 AND 0 AND 0 00, , 11 AND 0 AND 0 00• bit is cleared (set to 0)bit is cleared (set to 0)

for other bits, AND with 1for other bits, AND with 1• 00 AND 1 AND 1 00, , 11 AND 1 AND 1 11• bit is unchangedbit is unchanged

desired bits are cleared to zero, the other desired bits are cleared to zero, the other remain unchangedremain unchanged

Page 22: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 22

Clearing bitsClearing bits

Example: set sign bit of C Example: set sign bit of C floatfloat to 0 to 0

sign bitsign bit

ANDAND

11 0110101101101011 1100110110100011001100011001101101000110011000

00 1111111111111111 1111111111111111111111111111111111111111111111

00 0110101101101011 1100110110100011001100011001101101000110011000

a 0 in a 0 in this this

column column forces forces

the sign the sign bit to bit to zerozero

ones in ones in these these

columns columns allow allow the the

value to value to show show

throughthroughresulting number is resulting number is positive: 0 in MSBpositive: 0 in MSB

Page 23: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 23

Setting bitsSetting bits

Sometimes need to set a bit to oneSometimes need to set a bit to one e.g., set sign bit of a e.g., set sign bit of a floatfloat, to make negative , to make negative

Use bitwise Use bitwise OROR to to setset bits bits for bits to set, OR with 1for bits to set, OR with 1

• 00 OR 1 OR 1 11, , 11 OR 1 OR 1 11• bit is set (to 1)bit is set (to 1)

for other bits, OR with 0for other bits, OR with 0• 00 OR 0 OR 0 00, , 11 OR 0 OR 0 11• bit is unchangedbit is unchanged

desired bits are set to one, others remain desired bits are set to one, others remain unchangedunchanged

Page 24: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 24

Setting bitsSetting bits

Example: set sign bit of C Example: set sign bit of C floatfloat to to 11 sign bitsign bit

OROR

00 0110101101101011 1100110110100011001100011001101101000110011000

11 0000000000000000 0000000000000000000000000000000000000000000000

11 0110101101101011 1100110110100011001100011001101101000110011000

a 1 in a 1 in this this

column column forces forces

the sign the sign bit to bit to oneone

zeroes zeroes in these in these columns columns

allow allow the the

value to value to show show

throughthroughresulting number is resulting number is negative: 1 in MSBnegative: 1 in MSB

Page 25: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 25

Toggling bitsToggling bits

Sometimes need to flip a bit’s valueSometimes need to flip a bit’s value e.g., toggle sign bit to negate a e.g., toggle sign bit to negate a floatfloat

Use bitwise Use bitwise XORXOR ( (exclusive ORexclusive OR) to ) to toggletoggle bits bits for bits to toggle, XOR with 1for bits to toggle, XOR with 1

• 00 XOR 1 XOR 1 11, , 11 XOR 1 XOR 1 00• bit is flipped (0 bit is flipped (0 1) 1)

for other bits, XOR with 0for other bits, XOR with 0• 00 XOR 0 XOR 0 00, , 11 XOR 0 XOR 0 11• bit is unchangedbit is unchanged

desired bits are flipped, others remain desired bits are flipped, others remain unchangedunchanged

Page 26: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 26

Toggling bitsToggling bits

Example: negate a C Example: negate a C floatfloat

sign bitsign bit

XORXOR

11 0110101101101011 1100110110100011001100011001101101000110011000

11 0000000000000000 0000000000000000000000000000000000000000000000

00 0110101101101011 1100110110100011001100011001101101000110011000

a 1 in a 1 in this this

column column causes causes the sign the sign

bit to bit to flipflip

zeroes zeroes in these in these columns columns

cause cause the the

original original bit to bit to

remain remain the the

samesameresulting number has resulting number has

opposite sign bitopposite sign bit

Page 27: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 27

Bitwise operations in CBitwise operations in C

bitwise ANDbitwise AND (on two values) (on two values) C operator: C operator: && (ampersand) (ampersand)

bitwise ORbitwise OR (on two values) (on two values) C operator: C operator: || (vertical bar) (vertical bar)

bitwise Exclusive ORbitwise Exclusive OR (XOR) (on two (XOR) (on two values)values) C operator: C operator: ^̂ (caret) (caret)

bitwise NOTbitwise NOT (on one value) (on one value) C operator: C operator: ~~ (tilde) (tilde)

Page 28: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 28

Bitwise operations in CBitwise operations in C/* Examples of bitwise operations. *//* Examples of bitwise operations. */

int main()int main(){{ /* 16-bit value, binary 0000001101011110. *//* 16-bit value, binary 0000001101011110. */ unsigned short value = 0x035E;unsigned short value = 0x035E; /* 16-bit mask, binary 0000000011111111. *//* 16-bit mask, binary 0000000011111111. */ unsigned short mask = 0x00FF;unsigned short mask = 0x00FF; /* %04X printf format prints as 4 hex digits*//* %04X printf format prints as 4 hex digits*/ printf("%04X\n", value ); printf("%04X\n", value ); /* /* 035E035E */ */ printf("%04X\n", mask ); printf("%04X\n", mask ); /* /* 00FF00FF */ */ printf("%04X\n", ~value ); printf("%04X\n", ~value ); /* /* FCA1FCA1 */ */ printf("%04X\n", value & mask ); printf("%04X\n", value & mask ); /* /* 005E005E */ */ printf("%04X\n", value | mask ); printf("%04X\n", value | mask ); /* /* 03FF03FF */ */ printf("%04X\n", value ^ mask ); printf("%04X\n", value ^ mask ); /* /* 03A103A1 */ */ return 0;return 0;}}

Page 29: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 29

ShiftingShifting Example: extracting exponent from Example: extracting exponent from

a C a C floatfloat

ANDAND

00 0110101101101011 1100110110100011001100011001101101000110011000

00 1111111111111111 0000000000000000000000000000000000000000000000

00 0110101101101011 0000000000000000000000000000000000000000000000

valuevalue

maskmask

00000000000000000000000000000000000000000000000110101101101011

if you if you use a use a

mask to mask to get the get the

exponenexponent ...t ...

8975810568975810561010

... you ... you get this:get this:

1071071010

really really want want this:this:shift right 23 bitsshift right 23 bits

Page 30: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 30

ShiftingShifting

Shifting is the moving of patterns Shifting is the moving of patterns of bits left or right within a wordof bits left or right within a word left or rightleft or right 0 ≤ amount to shift < word size0 ≤ amount to shift < word size

00 11 11 00 11 00 11 00

00 11 00 11 00 00 00 00

example: example: shifting shifting left by left by

three bitsthree bits

fill with fill with zeroeszeroes

these bits these bits are shifted are shifted

“off the “off the end” and end” and

are are discardeddiscarded

Page 31: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 31

Shifting in CShifting in C

Binary (two value) operatorsBinary (two value) operators Shift leftShift left operator: operator: <<<< Shift rightShift right operator: operator: >>>> Value to shift goes on left of the Value to shift goes on left of the

operatoroperator Amount to shift goes on the right Amount to shift goes on the right

of the operatorof the operator

Page 32: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 32

Shifting in CShifting in C/* Example using C shift operators. *//* Example using C shift operators. */

int main()int main(){{ /* 16-bit value, binary 1000111100000010. *//* 16-bit value, binary 1000111100000010. */ unsigned short value = 0x8F02;unsigned short value = 0x8F02; int i = 4; int i = 4; /* used as a shift amount *//* used as a shift amount */

/* Shift right or left by 4 bits. *//* Shift right or left by 4 bits. */ printf("%04X\n", value >> 4 ); printf("%04X\n", value >> 4 ); /* /* 08F008F0 */ */ printf("%04X\n", value << 4 ); printf("%04X\n", value << 4 ); /* /* F020F020 */ */ /* Either side can be variable or literal. *//* Either side can be variable or literal. */ printf("%04X\n", value >> i ); printf("%04X\n", value >> i ); /* /* 08F008F0 */ */ /* Shifting by 1 harder to see in hex;/* Shifting by 1 harder to see in hex; write these in binary to see what’s happening. */write these in binary to see what’s happening. */ printf("%04X\n", value >> 1 ); printf("%04X\n", value >> 1 ); /* /* 47814781 */ */ printf("%04X\n", value << 1 ); printf("%04X\n", value << 1 ); /* /* 1E041E04 */ */

return 0;return 0;}}

Page 33: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 33

Shifting and Shifting and multiplicationmultiplication

8-bit unsigned binary8-bit unsigned binary

00 00 00 00 11 00 00 11

00 00 00 11 00 00 11 00

991010

18181010

00 00 11 00 00 11 00 00 36361010

00 11 00 00 11 00 00 00 72721010

shift left 1shift left 1

shift left 1shift left 1

shift left 1shift left 1

x2x2

×2×2

×2×2

Page 34: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 34

Shifting and divisionShifting and division

8-bit unsigned binary8-bit unsigned binary

00 11 11 00 00 11 00 00

00 00 11 11 00 00 11 00

1001001010

50501010

00 00 00 11 11 00 00 11 25251010

00 00 00 00 11 11 00 00 12121010

shift right shift right 11

shift right shift right 11

shift right shift right 11

÷2÷2

÷ 2÷ 2

÷ 2÷ 2

Page 35: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 35

ShiftingShifting

Shifting leftShifting left a value by a value by nn places is places is equivalent to equivalent to multiplying by 2multiplying by 2nn

<< 1 << 1 × 2; << 2 × 2; << 2 × 4; << 3 × 4; << 3 × 8; etc. × 8; etc. Shifting rightShifting right a value by a value by nn places is places is

equivalent to equivalent to dividing by 2dividing by 2nn

>> 1 >> 1 ÷ 2; etc. ÷ 2; etc. Shifting is quicker than Shifting is quicker than

multiplication/divisionmultiplication/division for efficiency, compiler will replace for efficiency, compiler will replace

multiplication or division by constant power multiplication or division by constant power of two with a shift left or shift rightof two with a shift left or shift right

Page 36: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 36

Shifting signed valuesShifting signed values

8-bit signed (two’s complement)8-bit signed (two’s complement)

00 11 11 00 00 00 00 00

00 00 00 00 11 11 00 00

96961010

12121010

shift right shift right 33 ÷8÷8

MSB = 0: positiveMSB = 0: positive

fill with 0fill with 0

Page 37: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 37

Shifting signed valuesShifting signed values

8-bit signed (two’s complement)8-bit signed (two’s complement)

11 00 11 00 00 00 00 00

00 00 00 11 00 11 00 00

––96961010

+20+201010

shift right shift right 33 ÷8÷8

MSB = 1: MSB = 1: negativenegative

fill with 0fill with 0 got 00010100got 0001010022, wanted , wanted

–12–121010 = 11110100 = 1111010022: : differs in first three bitsdiffers in first three bits

Page 38: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 38

Shifting signed valuesShifting signed values

8-bit signed (two’s complement)8-bit signed (two’s complement)

11 00 11 00 00 00 00 00

11 11 11 11 00 11 00 00

––96961010

––12121010

shift right shift right 33 ÷8÷8

MSB = 1: MSB = 1: negativenegative

fill with fill with copies of copies of original original

MSBMSB

by filling with copies of the by filling with copies of the MSB, positive numbers stay MSB, positive numbers stay

positive, and negative positive, and negative numbers stay negativenumbers stay negative

Page 39: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 39

ShiftingShifting Need to distinguish two kinds of right Need to distinguish two kinds of right

shiftshift logicallogical: always fill left bits with : always fill left bits with 00 arithmeticarithmetic: fill left bits with copies of : fill left bits with copies of MSBMSB C C >>>> operator is logical for unsigned values, operator is logical for unsigned values,

unknown for signed valuesunknown for signed values• gccgcc uses arithmetic right shift on signed values uses arithmetic right shift on signed values

Not a problem with left shiftNot a problem with left shift logical left shift same as arithmetic left shiftlogical left shift same as arithmetic left shift signed: overflow occurred if MSB changessigned: overflow occurred if MSB changes unsigned: overflow if a 1 bit “unsigned: overflow if a 1 bit “exits stage exits stage

left”left”

Page 40: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 40

Covered in this lectureCovered in this lecture

Byte orderByte order bitwise operationsbitwise operations

AND, OR, NOT, XORAND, OR, NOT, XOR maskingmasking

shiftingshifting relationship to multiplication and divisionrelationship to multiplication and division right shiftsright shifts

• arithmetic shift rightarithmetic shift right• logical shift rightlogical shift right

Page 41: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 41

Going furtherGoing further

Boolean algebraBoolean algebra the mathematics of binary valuesthe mathematics of binary values

Digital logicDigital logic hardware for doing AND, OR, ...hardware for doing AND, OR, ...

Bit fieldsBit fields another way of referring to bits in Canother way of referring to bits in C

Page 42: 24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05

04/18/23 CSE1303 Part B lecture notes 42

Next timeNext time

PointersPointers Aggregation of dataAggregation of data

arraysarrays structsstructs

CharactersCharacters ASCIIASCII

StringsStrings

Reading:Reading:

Lecture notes section B06Lecture notes section B06