Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch. 7 Bitwise Operations Comp Sci 251 -- Bitwise operations.

Similar presentations


Presentation on theme: "Ch. 7 Bitwise Operations Comp Sci 251 -- Bitwise operations."— Presentation transcript:

1 Ch. 7 Bitwise Operations Comp Sci Bitwise operations

2 Comp Sci 251 -- Bitwise operations
Shift and rotate Move bits around within a register Fast implementation of certain arithmetic Logical operations (and, or, xor, nor, not) Combine two bit patterns Affect selected bits in a word Comp Sci Bitwise operations

3 Shift left instructions
Bits move left Rightmost bits are cleared sll Rdest, Rsrc1, immediate sllv Rdest, Rsrc1, Rsrc2 Bits from Rsrc1 shifted left by immediate or Rsrc2 Result  Rdest (Rsrc1 unchanged) Sslv ssl variable 1 1 1 1 1 Comp Sci Bitwise operations

4 Shift left and multiplication
sll $t0, $t0, 1 # multiply by 2 sll $t0, $t0, 2 # multiply by 4 sll $t0, $t0, 3 # multiply by 8 Shift left n multiplies by 2n Comp Sci Bitwise operations

5 Practical use of shift left
int x[50]; x[3] = -1; .text li $t0, -1 li $t1, 3 sll $t1, $t1, 2 sw $t0, x($t1) .data .align 2 x:.space 200 Comp Sci Bitwise operations

6 Shift right instructions
Bits move right Leftmost bits Cleared (logical shift) Retain original leftmost bit value (arithmetic shift) srl Rdest, Rsrc1, immediate srlv Rdest, Rsrc1, Rsrc2 sra Rdest, Rsrc1, immediate srav Rdest, Rsrc1, Rsrc2 Logical shift right 1 1 1 1 1 Arithmetic shift right 1 1 1 1 1 Comp Sci Bitwise operations

7 Shift right and division
srl $t0, $t0, 1 # divide by 2 srl $t0, $t0, 2 # divide by 4 Shift right logical n divides by 2n (unsigned) Shift right arithmetic n divides by 2n (signed) Comp Sci Bitwise operations

8 Comp Sci 251 -- Bitwise operations
Rotate instructions Similar to shift Bits pushed out wrap around rol Rdest, Rsrc1, Src2 ror Rdest, Rsrc1, Src2 Bits from Rsrc1 rotated by Src2 Result  Rdest (Rsrc1 unchanged) Rotate left 1 1 1 1 1 Comp Sci Bitwise operations

9 Comp Sci 251 -- Bitwise operations
Logical instructions x y x and y x or y x xor y x nor y not x 1 Comp Sci Bitwise operations

10 Comp Sci 251 -- Bitwise operations
Logical instructions and Rdest, Rsrc1, Src2 or Rdest, Rsrc1, Src2 xor Rdest, Rsrc1, Src2 nor Rdest, Rsrc1, Src2 not Rdest, Rsrc Corresponding bits of Rsrc1 and Src2 are combined w/ logical operator Resulting word  Rdest Comp Sci Bitwise operations

11 Comp Sci 251 -- Bitwise operations
Logical Instructions Name Format Operation And R and Rdest, Rsrc1, Src2 R[rd] = R[rs] & R[rt] And immediate I andi R[rt] = R[rs] & ZeroExtImm Comp Sci Bitwise operations

12 Uses of shift/logical instructions
Clear: AND with 0 Set: OR with 1 Toggle: XOR with 1 Applications: Hashing Data compression Checksums Graphics Exercises: Clear low order 4 bits of $t0 Set the most significant bit of $t1 Count number of "1" bits in $t2 Comp Sci Bitwise operations

13 Examples in /shared/huen/251/ch07_bitwise
bit2.a – extracts three fields of different widths from a 32 bit word xor.a – XOR operation. Use XSPIM or PCSPIM to see how the bit pattern changes in registers. xori.a – another XOR operation example. Comp Sci Bitwise operations

14 Examples in /shared/huen/251/ch07_shift
dec.a - asks user for decimal number, converts to ASCII string, print the result. hex.a – asks user for decimal number, converts to hexadecimal, print the result logic1.a – sums the elements that are NOOT multiples of 4 in an array. Oct.a - asks user for decimal number, converts to octal, print the result Comp Sci Bitwise operations


Download ppt "Ch. 7 Bitwise Operations Comp Sci 251 -- Bitwise operations."

Similar presentations


Ads by Google