Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Bit manipulation instructions

From Wikipedia, the free encyclopedia
(Redirected fromBit Manipulation Instruction Sets)
Hardware-level Bit manipulation instructions
This article is about the general topic of Instruction set bit manipulation subsets. For bit manipulation extensions unique to AMD and Intel, seex86 Bit manipulation instruction set.
Further information on bit manipulation in general, see:bit manipulation
icon
This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources.
Find sources: "Bit manipulation instructions" – news ·newspapers ·books ·scholar ·JSTOR
(August 2025) (Learn how and when to remove this message)
Machine code
General concepts
Instructions

Bit manipulation instructions areinstructions that performbit manipulation operations in hardware, rather than requiring several instructions for those operations as illustrated withexamples in software.[1] Several leading as well as historic architectures have bit manipulation instructions includingARM,WDC 65C02, theTX-2 and thePower ISA.[2]

Bit manipulation is usually divided into subsets as individual instructions can be costly to implement in hardware when the target application has no justification. Conversely, if thereis a justification then performance may suffer if the instruction is excluded. Carrying out the cost-benefit analysis is a complex task: one of the most comprehensive efforts in bit manipulation was a collaboration headed by Clare Wolfe, providing justifications, use-cases, c code, proofs and Verilog for each proposed RISC-V instruction.[3][4]

Particular practical examples includeBit banging ofGPIO using a low-costEmbedded controller such as theWDC 65C02,8051 andAtmel PIC. At the slow clock rate of these CPUs, if bit-set/clear/test bit manipulation were not available the use of that low-cost CPU would, self-evidently, not be viable for the target application.


Hardware bit manipulation

[edit]
Further information on categorisation of all types of bit and bitwise manipulation:Bit_manipulation § Bit_manipulation_operations
Further information on a more complete list of find-first style instructions:Find_first_set § Hardware_support

All the architectures below have instruction subsets and groups where the bit manipulation is provided in hardware. From the list it can be seen thatDSPs andEmbeddedMicrocontrollers have at least test/set/clear bit, however there are much more comprehensive instructions such asCount leading zeros,Popcount,Galois field arithmetic,Binary-coded decimal, bit-matrix multiply and transpose, byte-permute, bit permute includingbit-reversal, specialised cryptographic instructions and many more.

Intel and AMD (x86)

[edit]
  • The x86 instruction core set contains:
    • BSR Bit Scan Reverse - Returns bit index of highest set bit in input, effectively backwards count leading zeros, not defined for 0.
    • BSF Bit Scan Forward - Returns bit index of lowest set bit in input, effectively count trailing zeros, but not defined for 0.
  • SSE4 and theBMI instruction set extensions contains instructions for:
    • Count leading zeros -lzcnt
    • Count trailing zeros -tzcnt
    • Population count -popcnt
    • Bit extract/bit deposit -pext/pdep
    • Bit test -ptest andvptest, given two inputs, do both anAND operation and anANDN operation between them, and set the ZF and CF EFLAGS bits on whether the results of the AND and ANDN, respectively, are 0. This can be used to test if all masked bits are zero, all masked bits are set, or a mix.
  • TheAVX-512 ternary extension includes aBitwise ternary logic instruction,vpternlog. Also noteworthy is a conflict detection instruction.VPCONFLICTD
  • Also present in the AVX/AVX-512GFNI subset is bit-matrix affine transformation and its inverse:GF2P8AFFINEQB is effectively an 8x8 bit-matrix multiply in theGalois field GF(2^8).[5]
  • AVX-512 BITALG besides AVX-512 version of existing bit manipulation instruction, also addedVPSHUFBITQMB which is a bit-level shuffle instruction, that picks bits from one source using indexes in the second source.
  • An Intel GNFI technology guide on that AVX/AVX512 GNFI Extension also lists numerous uses including parallel byte-wise set/clear/invert bitmanipulation, 5-bit sign-extension and points out the potential is much greater.[6]
  • Intel BCD opcodes

Power ISA

[edit]

Power ISA has a large range of bit manipulation instructions,[7] largely due to its history and relationship with IBM mainframes and thez/Architecture:

  • Count leading zeros and trailing, and masked versions of the same.[8] There is a mixture ofPopcount[8]parity[9] andSWAR-style instructions, but not a full set of each:popcntb is SWAR byte-level 8x8-bit but there is no 4x16-bitpopcnth yet there is 2x32-bitpopcntw and 64-bit scalarpopcntd. Likewise,prtyw is SWAR half-word 4x16-bit but there is noprtyb
  • masked bit-extractpextd and bit-depositpdepd these drop and distribute bits in place according to a mask instead of the more usual technique of a offset and a length.;[10] An unusual centrifuge instruction which moves masked-bits to the left and unmasked bits to the right, preserving their relative order in both instances. Most ISAs would have an operand expressing the number of sequential bits to extract, plus the length:cfuged combines these into one general-purpose bitmask.[10]
  • 8x8-bit transposevgbbd[11] which treats a 64-bit quantity as an 8x8 2D matrix, and performs a matrix transpose operation. Each bit 0 of each byte therefore becomes the first byte, each bit 1 of each byte becomes the second and so on.
  • a strange but very useful indexing instruction, (bpermd)[12] which allows selection of up to eight individual bits from a 64-bit source, by treating each byte of a second 64-bit register as bit-indices into the first.
  • Ternary 8-bitBitwise ternary logic instructionxxeval[13] similar toAVX-512
  • strategic instructions for acceleratingPacked BCD.[14]
  • Power v3.1 also introduced a number of additional bit manipulation instructions including swapping the order of bytes within half-words, words, and the whole 64-bit register.

Cray supercomputers

[edit]

Cray patented BMM (Bit matrix multiply) in 1990 which could cope with up to 64x64-bit operands.[15] The closest equivalent today is the 8x8 GF(2) Affine Transform instruction of AVX512.

IBM System/360 through z/Architecture

[edit]

IBM System/360

[edit]

TheIBM System/360 has RR, RX and SI instructions for bit-wise and, exclusive or and or, RS arithmetic and logical shift[a] instructions, an SI test under mask[b] and an atomic RX test and set instruction. These instructions and their extensions remain available through z/Architecture.

IBM System/370

[edit]

Toward the end of theS/370 life cycle, IBM made move characters inverse, previously an RPQ, a standard instruction.

IBM S/370, S/370-XA, ESA/370, and ESA/390 vector operations

[edit]

TheIBM 3090 introduced an optionalvector facility[16] to theSystem/370-XA andEnterprise Systems Architecture/370 instruction sets. In addition to integer and floating-point vector arithmetic and logical operations on multiple integer and floating-point values, it introduced vector bit manipulation operationscount leading zerosvczvm andpopulation countvcovm.[17]

ESA/390

[edit]

Towards the end of theESA/390 life cycle, IBM introduced some z/Architecture instructions in ESA/390. These included the rotate left single logical, load reversed and store reversed instructions.

z/Architecture scalar

[edit]

z/Architecture inherited all of the bit manipulation instructions of its predecessors, and added 64-bit (grande) and long (20-bit) displacement versions of some.

  • General-instructions-extension facility[18] adds
    • ROTATE THEN AND SELECTED BITS[19]
    • ROTATE THEN EXCLUSIVE OR SELECTED BITS[20]
    • ROTATE THEN INSERT SELECTED BITS[20]
    • ROTATE THEN OR SELECTED BITS[20]
  • high-word facility[21] adds
    • ROTATE THEN INSERT SELECTED BITS HIGH[22]
    • ROTATE THEN INSERT SELECTED BITS Low[22]
  • Interlocked-Access Facility 1[23] adds
    • LOAD AND AND (LAN, LANG)[24]
    • LOAD AND EXCLUSIVE OR (LAX, LAXG)[25]
    • LOAD AND OR (LAO, LAOG) (LAX, LAXG)[26]
  • Miscellaneous-Instruction-Extensions Facility 1[27] adds
    • ROTATE THEN INSERT SELECTED BITS (RISBGN)[28]
  • Miscellaneous-instruction-extensions facility 3 adds
    • AND WITH COMPLEMENT (NCRK, NCGRK)
    • MOVE RIGHT TO LEFT
    • NAND (NNRK, NNGRK)
    • NOT EXCLUSIVE OR (NXRK, NXGRK)
    • NOR (NORK, NOGRK)
    • OR WITH COMPLEMENT (OCRK, OCGRK)
    • SELECT (SELR, SELGR)
    • SELECT HIGH (SELFHR)
  • Miscellaneous-Instruction-Extensions Facility 4[29] adds
    • BIT DEPOSIT (BDEPG)[30]
    • BIT EXTRACT (BEXTG)[31]
    • COUNT LEADING ZEROS (CLZG)
    • COUNT TRAILING ZEROS (CTZG)

z/Architecture vector operations

[edit]

z/Architecture does not support the previous vector facility.[32] However, starting with the 11th edition of the z/Architecture Principles of Operation:[33] it supports the following instructions:

DEC PDP-10

[edit]

The DECPDP-6 andPDP-10 had logical operations covering the full suite of 2-operandhardware lookup table (LUT2)Boolean functions[41] (rather than the 3-operand functions that AVX512 and Power ISA have).

Later models of the PDP-10 had instructions to convert betweenpacked BCD and binary.[42]

Also present is unusual (variable-bit-length) byte load and store instructions that usebyte pointers for memory operands: in modern terminology these are bit-field insert and extract. In addition to a word address, the bit length (S) and the bit offset (P) of the byte from which to load or into which to store are specified. These instructions can specify a byte size of 0-36, but a byte may not straddle a word boundary.[43] The string manipulation,[44] BCD/binary conversion,[45] and string editing[46] instructions in later models use byte pointers and have the same restrictions.

GE-600 series

[edit]

TheGE-600 series and its successors had Gray-to-binary conversion; without such an instruction,converting from Gray code requires multiple steps. Binary-to-Gray is simplyx^(x>>1) and does not justify a dedicated instruction. Gray coding has significantpractical applications.

ARM

[edit]
  • ARM11 has bitwise test-ANDed (a bitmasked test) and test-XOR, standard logicalbitwise operations including OR-complement; byte halfword and bit-reversing, and conditional byte-selection/merging. Shift and rotate are available on Operand2.[47]
  • ARM Cortex-A has bit-field set, clear, extract and reverse.[48]
  • ARM A64 hasSWAR-style half-word byte-swapping, bit-field insert and extract, and bit-reversing.[49]

RISC-V

[edit]

In the standard extensions RISC-V has scalarbitwise operations including shift and arithmetic shift, but no rotate. The omissions are compensated for with additional extensions.

  • RISC-V Zb* extensions contain a significant number of bit manipulation instructions.[50] The four groups are broken down into useful categories (the integer subset has min/max, rotate andPopcount for example), and have very well-researched justifications for their inclusion and the improvements they bring.[51]
  • The RISC-V Vector Extension (RVV) has instructions that qualify as hardware-level bit manipulation, but on Vector masks rather than Scalar registers as is normally the case. For example, a Vector-maskPopcount is available.[52] RVV also has per-elementbitwise operations.[53]

Embedded microcontrollers

[edit]

Intel

[edit]
  • The8086 hasTEST, as well asbitwise operations[54]
  • The8051 hasSETB,CLR andCPL - set clear and invert bit instructions - and a considerable percentage of its instructions are bit manipulation.[55] Also included is Or-complement and And-complement, present in RISC-V Zb*.[56]

Zilog Z80

[edit]
  • TheZilog Z80instruction set includesBIT,RES, andSET instructions. These test, reset, and set individual bits in registers or memory pointed to by HL, IX, or IY.[57]

MOS 6502

[edit]
See also:MOS Technology 6502 § Variations and derivatives
  • TheWDC 65C02 addedbit-manipulation:test and set (TSB) and test and reset (TRB) on individual bits.
  • Rockwell added similar extensions (RMB, SMB, BBR and BBS) to the R65C00 series[58]

Microchip PICs

[edit]

Others

[edit]
  • Texas InstrumentsDSPs such as theTMS320C6000 series have set, clear, invert, test, extract and insert bit (or bit-field) instructions.[59]
  • TheTX-2 from 1958 had"skip on bit" predication, as well as set, clear, invert and permute bits, and shift and otherbitwise operations.[60][61]
  • SuperH has comprehensive memory-based bit manipulation including And-complement and Or-complement, but also has standard register-based test/set/clear and an unusual instruction that replaces bit N (in the range 0 to 7) and copies the replaced bit into the Test register.[62]
  • TheSignetics8X300 is a microprocessor introduced in 1976. The processor normally manipulates 8-bit data bytes, but the mask and rotate units makes it possible to manipulate single or multiple bits, making this a variable data-length processor.
  • TheDECPDP-11 architecture from 1970 supports bit testing, setting, and clearing on both wordsBITBISBIC and bytesBITBBISBBICB. The very similarWD16 supports only the word forms of these instructions plusBISB. The WD16 additionally supports faster byte-addressed flags with itsTSTBSETBCLRB andCOMB (compliment) instructions. The PDP-11 is missing theSETB instruction.
  • TheMotorola 68000 supports bit test and manipulation of memory or data registers. The bit number may either be an immediate or a value in a data register. The instructions are:BSET (set to 1),BCLR (clear to 0),BCHG (invert) andBTST (no change). All of these instructions first test the destination bit and set the CCR Z bit if the destination bit is 0.

Notes

[edit]
  1. ^There are no rotate instructions inS/360,S/370 orESA/370..
  2. ^The immediate mask is 8 bits.

See also

[edit]

References

[edit]
z/Architecture Principles of Operation(PDF) (First ed.).IBM. December 2000. SA22-7832-00. RetrievedAugust 8, 2025.
z/Architecture Principles of Operation(PDF) (Eleventh ed.).IBM. March 2015. SA22-7832-10. RetrievedAugust 8, 2025.
z/Architecture Principles of Operation(PDF) (Fifteenth ed.).IBM. April 2025. SA22-7832-14. RetrievedJuly 3, 2025.
Power ISA™ Version 3.1(PDF) (v3.1 ed.).IBM. May 1, 2020. SA22-7832-14. RetrievedAug 7, 2025.
IBM System/370 Vector Operations(PDF) (Third ed.). IBM Corporation. August 1986. SA22-7125-2. RetrievedSep 20, 2018.
DECsystem-10 - DECSYSTEM--20 - Processor Reference Manual(PDF).Digital Equipment Corporation. AA-H391A-TK, AD-4391A-T1. RetrievedAugust 8, 2025 – via bitsavers.org.
  1. ^"Bit Twiddling Hacks".
  2. ^"Advanced bit manipulation instructions: Architecture, implementation and applications".ProQuest.
  3. ^"GitHub - riscv/Riscv-bitmanip at v0.93".GitHub.
  4. ^Claire Wolf, ed. (20 January 2021)."RISC-V Bitmanip Extension Document Version 0.94-draft"(PDF).
  5. ^"GF2P8AFFINEQB — Galois Field Affine Transformation".
  6. ^"Galois Field New Instructions (GFNI) Technology Guide".networkbuilders.intel.com.
  7. ^power3.1, IBM Power ISA v3.1.
  8. ^abpower3.1, p. 104, Power ISA Book I Chapter 3.3.13 Fixed-Point.
  9. ^power3.1, p. 103, Power ISA Book I Chapter 3.3.13 Fixed-Point.
  10. ^abpower3.1, p. 106, Power ISA Book I Chapter 3.3.13 Fixed-Point.
  11. ^power3.1, p. 445, Power ISA Book I Chapter 6.12.1 Vector Facility.
  12. ^power3.1, p. 105, Power ISA Book I Chapter 3.3.13 Fixed-Point.
  13. ^power3.1, p. 967, Power ISA Book I Chapter 7. Vector-Scalar Extension Facility.
  14. ^power3.1, p. 117, Power ISA Book I Chapter 3.3.15 Fixed-Point.
  15. ^"Vector bit-matrix multiply functional unit".
  16. ^ibm370, IBM System/370 Vector Operations.
  17. ^ibm370, pp. 3-7–3-8.
  18. ^z15, p. 1-16.
  19. ^z15, p. 7-426.
  20. ^abcz15, p. 7-427.
  21. ^z15, p. 1-17.
  22. ^abz15, pp. 7-430 –&#32, 7–431.
  23. ^z15, p. 1-18.
  24. ^z15, pp. 7-306 –&#32, 7–307.
  25. ^z15, p. 7-307.
  26. ^z15, pp. 7-307 –&#32, 7–308.
  27. ^z15, p. 1-25.
  28. ^z15, pp. 7-428 –&#32, 7–430.
  29. ^z15, p. 1-26.
  30. ^z15, pp. 7-35 –&#32, 7–36.
  31. ^z15, p. 7-36.
  32. ^z1, p. 1-1.
  33. ^z11, p. xxviii.
  34. ^z15, pp. 22-11–22-12.
  35. ^z15, pp. 7-289–7-290.
  36. ^z15, pp. 22–26, 7–424.
  37. ^z15, p. 22-37.
  38. ^z15, p. 22-16.
  39. ^z15, pp. 8-1–8-14.
  40. ^z15, pp. 7-458–7-459.
  41. ^pdp10, p. 2-38, 2.4 Boolean Functions.
  42. ^pdp10, pp. 2–99.
  43. ^pdp10, pp. 2-85–2-89, Byte manipulation.
  44. ^pdp10, pp. 2-91–2-99, 2.12 String Manipulation.
  45. ^pdp10, pp. 2-98–2-103, 2.13 Decimal Conversion.
  46. ^pdp10, pp. 2-104–2-112, 2.14 String Editing.
  47. ^"ARM Instruction Set Quick Reference Card"(PDF). October 2003.
  48. ^"Documentation – Arm Developer".
  49. ^"Documentation – Arm Developer".
  50. ^"Riscv-bitmanip/Bitmanip/Index.adoc at main · riscv/Riscv-bitmanip".GitHub.
  51. ^"Riscv-bitmanip/Bitmanip/Overview.adoc at main · riscv/Riscv-bitmanip".GitHub.
  52. ^"Riscv-v-spec/V-spec.adoc at master · riscvarchive/Riscv-v-spec".GitHub.
  53. ^"Riscv-v-spec/V-spec.adoc at master · riscvarchive/Riscv-v-spec".GitHub.
  54. ^"Bit Manipulation Instructions in 8086 | Logical Instructions". 11 August 2018.
  55. ^"8051 Instructions"(PDF).
  56. ^"Boolean (Bitwise) instructions in 8051 for bit manipulation". 29 April 2020.
  57. ^Z80 Family CPU User Manual(PDF).Zilog. 2016. UM008011-0816.Archived(PDF) from the original on December 26, 2023. RetrievedJanuary 5, 2024.
  58. ^"Rockwell R6500/11, R6500/12 and R6500/15 One-Chip Microcomputers". 7 June 1987. Archived fromthe original on 3 September 2023. Retrieved30 April 2020.
  59. ^"TMS320C6000 Programmer's Guide"(PDF).Texas Instruments. July 2011.
  60. ^"TX-2 Documentation".
  61. ^"Chapter 3 - Operation Code"(PDF).TX-2 User's Handbook.
  62. ^"Renesas SH Instruction Set Summary".

Further reading

[edit]
SIMD (RISC)
SIMD (x86)
Bit manipulation
  • BMI (ABM: 2007, BMI1: 2012, BMI2: 2013, TBM: 2012)
  • ADX (2014)
Compressed instructions
Security andcryptography
Transactional memory
Virtualization
Suspended extensions' dates arestruck through.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Bit_manipulation_instructions&oldid=1322281798"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp