1 / 14

Ch. 7 Bitwise Operations

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

chidi
Télécharger la présentation

Ch. 7 Bitwise Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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 0 1 0 0 1 1 1 0 1 Comp Sci 251 -- 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 251 -- Bitwise operations

  5. 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

  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 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

  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 251 -- Bitwise operations

  8. 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

  9. Logical instructions Comp Sci 251 -- Bitwise operations

  10. 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

  11. 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

  12. 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

  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 251 -- 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 251 -- Bitwise operations

More Related