Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Binary shifts

What are binary shifts?

  • A binary shift is an operation that moves all the bits in a binary number left or right by a certain number of positions

  • Often used in programming and computer systems for fast multiplication or division by powers of 2, or for manipulating individual bits

  • There are three main types of binary shift:

    • Logical

    • Arithmetic

    • Cyclic

Logical

  • Moves bits left or right and fills the gap with 0s

  • Used for unsigned binary numbers or raw bit manipulation

Direction

What happens

Left

All bits shift left, a 0 fills the rightmost bit

Right

All bits shift right, a 0 fills the leftmost bit

Left

  • The following number is shifted by two places to the left 

Step

128

64

32

16

8

4

2

1

Original

0

0

0

0

1

1

1

0

Left shift by 2

0

0

1

1

1

0

0

0

  • Original number: 0000 1110 = 14

  • Left shift (2) result: 0011 1000 = 56

  • Each left shift has doubled the number:

    • Original value = 14

    • Left shift 1 – Doubled the number to 28

    • Left shift 2 – Doubled the number to 56

  • The following number is shifted by three places to the right

Step

128

64

32

16

8

4

2

1

Original

1

1

0

0

1

0

0

0

Right shift by 3

0

0

0

1

1

0

0

1

  • Original number: 1100 1000 = 200

  • Right shift (3) result: 0001 1001 = 25

  • Each right shift has halved the number:

    • Original value = 200

    • Right shift 1 – Halved the number to 100

    • Right shift 2 – Halved the number to 50

    • Right shift 3 – Halved the number to 25

Arithmetic

  • Moves bits, but preserves the sign bit (used for signed binary numbers in two’s complement)

Direction

What happens

Left

Same as logical left (shift left, 0 in)

Right

Shift right, copy the sign bit (MSB) into the new leftmost bit

  • The following number is shifted by three places to the right

Step

128

64

32

16

8

4

2

1

Original

1

1

1

0

1

0

0

0

Right shift by 3

1

1

1

1

1

0

1

0

  • Original number: 1110 1000 = −24

  • Right shift (3) result: 1111 1010 = −6

  • Each arithmetic right shift divides the number by 2, rounding towards negative infinity (preserving the sign bit)

    • Original value = −24

    • Right shift 11111 0100 = −12

    • Right shift 21111 1010 = −6

    • Right shift 31111 1101 = −3

Cyclic

  • Rotates the bits around, nothing is lost, no 0s added

  • The bit that falls off one end is reused at the other end

Direction

What happens

Left

Leftmost bit moves to the rightmost position

Right

Rightmost bit moves to the leftmost position

  • The following number is shifted by three places to the left and right

Left and Right

Step

128

64

32

16

8

4

2

1

Original

1

0

1

1

0

0

0

1

Cyclic left (×3)

1

0

0

0

1

1

0

1

Cyclic right (×3)

0

0

1

1

0

0

1

0

  • The 3 leftmost bits 101 wrap around to the right side

  • The 3 rightmost bits 001 wrap around to the left side

  • Original: 1011 0001 = 177

  • Cyclic left (3): 1000 1101 = 141

  • Cyclic right (3): 0011 0010 = 50

Shift type

Left shift

Right shift

Used for

Logical Shift

Shift bits left, fill with 0

Shift bits right, fill with 0

Unsigned numbers, raw bits

Arithmetic Shift

Same as logical left

Shift bits right, preserve sign bit

Signed numbers (two’s complement)

Cyclic Shift

Rotate bits left (no loss)

Rotate bits right (no loss)

Bit rotation, encryption, checksums

Device control & bit masking

What is device control?

  • In computer systems and embedded devices, each bit in a byte can be used to represent the state of a device or feature (e.g. turning a light on, checking if a sensor is active, or flagging an error)

  • You can control or monitor these bits using bitwise operations and bit masking

What is bit masking?

  • Bit masking uses binary patterns (masks) to test, set, clear, or toggle specific bits without affecting the others

Bitwise AND operation (&)

  • Used to test if a specific bit is set to 1

  • Only returns 1 when both the binary value and the mask have 1 in the same position

  • Useful for checking the status of a device

<th class

Description

128

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注