SlideServe
Menu
Préférences
© SlideServe · v2026

Ch. 7 Bitwise Operations

chidi avatar
14 diapositives · 320 Views · Sep 16, 2014

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

Ch. 7 Bitwise Operations - slide 1
01 / 14Ch. 7 Bitwise Operations
1 / 17

Télécharger la présentation

Ch. 7 Bitwise Operations
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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