Slides for P3104R3
Bit permutations

Document number:
P3730R0
Date:
2025-06-04
Audience:
LEWG
Project:
ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
Reply-to:
Jan Schultke <janschultke@gmail.com>
Source:
github.com/Eisenwave/cpp-proposals/blob/master/src/bit-permutations-slides.cow

This document has custom controls:

  • ,  ↓ : go to the next slide
  • ,  ↑ : go to previous slide

Bit permutations
P3104R3

Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 1

History

Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 2

Synopsis

template<unsigned-integer T> constexpr T bit_repeat(T x, int length) /* not noexcept */; template<unsigned-integer T> constexpr T bit_reverse(T x) noexcept; template<unsigned-integer T> constexpr T bit_compress(T x, T m) noexcept; template<unsigned-integer T> constexpr T bit_expand(T x, T m) noexcept;

unsigned-integer means "Constraints: T is an unsigned integer type".

Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 3

std::bit_repeat

template<unsigned-integer T> constexpr T bit_repeat(T x, int length) /* not noexcept */;
bit_repeat(0b****'****'****'0110u, 4)
                               │
                ┌────┬────┬────┤
                │    │    │    │
        == 0b0110'0110'0110'0110u
Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 4

std::bit_reverse

template<unsigned-integer T> constexpr T bit_reverse(T x) noexcept;
bit_reverse(0b1000'1111'0000'1010u)
               ┌───────────────┘│
              ┌─────────────────┘
              ││        …
         == 0b0101'0000'1111'0001u
Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 5

std::bit_compress

template<unsigned-integer T> constexpr T bit_compress(T x, T m) noexcept;
unsigned m = 0b0001'0010'0000'1000u;
bit_compress(0b***1'**1*'****'0***u, m)
                  │   │       └─┐
                  │   └────────┐│
                  └───────────┐││
         == 0b0000'0000'0000'0110u
Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 6

std::bit_expand

template<unsigned-integer T> constexpr T bit_expand(T x, T m) noexcept;
unsigned m = 0b0001'0010'0000'1000u;
bit_expand(  0b****'****'****'*110u, m)
                  ┌────────────┘││
                  │   ┌─────────┘│
                  │   │       ┌──┘
          == 0b0001'0010'0000'0000u
Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 7



              _____________________________
            < vote SF for bit permootations >
              -----------------------------
                     \   ^__^ 
                      \  (oo)\_______
                         (__)\       )\/\\
                             ||----w |
                             ||     ||



Jan Schultke  |  Slides for P3104R3 — Bit permutations  |  Sofia 2025  |  Slide 8