140 likes | 291 Views
Ch. 7 Bitwise Operations. 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. Shift left instructions. Bits move left
E N D
Ch. 7 Bitwise Operations Comp Sci 251 -- Bitwise operations
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 251 -- Bitwise operations
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 0 1 0 0 1 1 1 0 1 Comp Sci 251 -- Bitwise operations
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 251 -- Bitwise operations
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 Practical use of shift left Comp Sci 251 -- Bitwise operations
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 0 1 0 0 1 1 1 0 1 Arithmetic shift right 1 0 0 1 1 1 0 1 Comp Sci 251 -- Bitwise operations
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 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 0 0 1 1 1 0 1 Comp Sci 251 -- Bitwise operations
Logical instructions 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 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 251 -- Bitwise operations
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 Uses of shift/logical instructions Comp Sci 251 -- Bitwise operations
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 251 -- Bitwise operations
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 251 -- Bitwise operations