ArticleslgStudy

science

Bit manipulation instructions

Bit manipulation instructions is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bit manipulation instructions rather than just read about it. In short: Bit manipulation instructions are instructions that perform bit manipulation operations in hardware, rather than requiring several instructions for those operations as illustrated with examples in software. Several leading as well as historic architectures have bit manipulation instructions including ARM, WDC 65C02, the TX-2 and the Power ISA.

Key takeaways

  • Bit manipulation instructions belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bit manipulation instructions to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bit manipulation instructions from memory before moving on to harder problems.

Reference excerpt

Bit manipulation instructions are instructions that perform bit manipulation operations in hardware, rather than requiring several instructions for those operations as illustrated with examples in software. Several leading as well as historic architectures have bit manipulation instructions including ARM, WDC 65C02, the TX-2 and the Power ISA. 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 there is 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. Particular practical examples include bit banging of GPIO using a low-cost embedded controller such as the WDC 65C02, 8051 and Atmel PIC. At the slow clock rate of these CPUs, if bit-set/clear/test bit manipulation were not available these low-cost CPUs would not be fast enough to be for the target application.

Hardware bit manipulation

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

Intel and AMD (x86) 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 the BMI 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 and vptest, given two inputs, do both an AND operation and an ANDN 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. The AVX-512 ternary extension includes a bitwise ternary logic instruction, vpternlog. Also noteworthy is a conflict detection instruction, VPCONFLICTD Also present in the AVX/AVX-512 GFNI subset is bit-matrix affine transformation and its inverse: GF2P8AFFINEQB is effectively an 8x8 bit-matrix multiply in the Galois field GF(2^8). AVX-512 BITALG besides AVX-512 version of existing bit manipulation instruction, also added VPSHUFBITQMB 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. Intel BCD opcodes

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

Count leading zeros and trailing, and masked versions of the same. There is a mixture of popcount parity and SWAR-style instructions, but not a full set of each: popcntb is SWAR byte-level 8x8-bit but there is no 4x16-bit popcnth yet there is 2x32-bit popcntw and 64-bit scalar popcntd. Likewise, prtyw is SWAR half-word 4x16-bit but there is no prtyb masked bit-extract pextd and bit-deposit pdepd these drop and distribute bits in place according to a mask instead of the more usual technique of a offset and a length.; 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. 8x8-bit transpose vgbbd 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) 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-bit bitwise ternary logic instruction xxeval similar to AVX-512 strategic instructions for accelerating packed BCD 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 Cray patented BMM (Bit matrix multiply) in 1990 which could cope with up to 64x64-bit operands. The closest equivalent today is the 8x8 GF(2) Affine Transform instruction of AVX512.

IBM System/360 through z/Architecture

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

IBM System/370 Toward the end of the S/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 The IBM 3090 introduced an optional vector facility to the System/370-XA and Enterprise 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 operations count leading zeros vczvm and population count vcovm.

ESA/390 Towards the end of the ESA/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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Bit manipulation instructions

Start with the simplest possible case. Write down what Bit manipulation instructions claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bit manipulation instructions before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bit manipulation instructions ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bit manipulation instructions

In research
Bit manipulation instructions appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bit manipulation instructions in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bit manipulation instructions is common in secondary-school and first-year university syllabi. It links to neighbouring topics Instruction set extensions, so understanding it makes those chapters shorter.
In everyday life
Look for Bit manipulation instructions outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bit manipulation instructions” →

How to study Bit manipulation instructions in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bit manipulation instructions means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bit manipulation instructions out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bit manipulation instructions in simple terms?

Bit manipulation instructions are instructions that perform bit manipulation operations in hardware, rather than requiring several instructions for those operations as illustrated with examples in software. Several leading as well as historic architectures have bit manipulation instructions includi…

Why does Bit manipulation instructions matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bit manipulation instructions?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bit manipulation instructions.

Tags

  • Instruction set extensions

Keep exploring