# \#bit-twiddling

**URL:** https://discourse.julialang.org/tag/bit-twiddling/247.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Decompose positive integer into 2^p+m](https://discourse.julialang.org/t/decompose-positive-integer-into-2-p-m/138893)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 12\
**Last updated:** [August 18, 2026, 3:02pm UTC](https://discourse.julialang.org/t/decompose-positive-integer-into-2-p-m/138893 "2026-08-18T15:02:50Z")

</div>

This is a bit-wrangling question: I would like to decompose a positive integer x into 2^p+m, for the highest p such that m \\ge 0. Expected output: julia\> for i in 1:7 println(i =\> decompose(i)) end 1 =\> (…

---

## [Bit instructions from C++. Do we have them?](https://discourse.julialang.org/t/bit-instructions-from-c-do-we-have-them/113900)

<div class="topic-metadata">

**Author:** [@Dan](https://discourse.julialang.org/u/Dan)\
**Replies:** 1\
**Last updated:** [May 6, 2024, 12:56pm UTC](https://discourse.julialang.org/t/bit-instructions-from-c-do-we-have-them/113900 "2024-05-06T12:56:03Z")

</div>

From a Twitter post: Do we have all these? Seems like we do have some of them. Could be good practice to adopt the missing ones. The source is: C23 stdbit.h quick reference

---

## [Calling AVX-512 intrinsics from Julia](https://discourse.julialang.org/t/calling-avx-512-intrinsics-from-julia/101079)

<div class="topic-metadata">

**Author:** [@giacomogiudice](https://discourse.julialang.org/u/giacomogiudice)\
**Replies:** 6\
**Last updated:** [July 4, 2023, 8:45am UTC](https://discourse.julialang.org/t/calling-avx-512-intrinsics-from-julia/101079 "2023-07-04T08:45:48Z")

</div>

I am having trouble calling some AVX-512 intrinsics from Julia, coming directly from this post. The example on the blog post compiles and runs fine on the CPU, since it has the avx512\_bitalg CPU flag. The problematic i…

---

## [How do I find the number of bits a number needs for storage](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bits-a-number-needs-for-storage/93334)

<div class="topic-metadata">

**Author:** [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Replies:** 5\
**Last updated:** [January 21, 2023, 11:40pm UTC](https://discourse.julialang.org/t/how-do-i-find-the-number-of-bits-a-number-needs-for-storage/93334 "2023-01-21T23:40:41Z")

</div>

I was looking into compression algorithms and storage schemes (like Simple-8b and xor-based compression ) and one thing they do is determine the highest set bit for a given number. I wrote some code, but I feel this ca…

---

## [\[ANN\] BitPermutations.jl: efficient routines for repeated bit permutations](https://discourse.julialang.org/t/ann-bitpermutations-jl-efficient-routines-for-repeated-bit-permutations/93312)

<div class="topic-metadata">

**Author:** [@giacomogiudice](https://discourse.julialang.org/u/giacomogiudice)\
**Replies:** 0\
**Last updated:** [January 21, 2023, 1:29pm UTC](https://discourse.julialang.org/t/ann-bitpermutations-jl-efficient-routines-for-repeated-bit-permutations/93312 "2023-01-21T13:29:45Z")

</div>

I am happy to announce BitPermutations.jl, a package to efficiently permute the bits in some bitstring, given an arbitrary permutation. Given a permutation over n bits, we can actually reshuffle bitstring in O(log n) ti…

---

## [Rand-based Function Optimization](https://discourse.julialang.org/t/rand-based-function-optimization/65874)

<div class="topic-metadata">

**Author:** [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Replies:** 1\
**Last updated:** [August 5, 2021, 12:53pm UTC](https://discourse.julialang.org/t/rand-based-function-optimization/65874 "2021-08-05T12:53:54Z")

</div>

Given a non-zero UInt8, I would like to randomly and uniformly select the index of a bit that is one. For example, if I get UInt8(14), the bitstring would be 00001110, and thus it would return UInt8(2), UInt8(3), and UIn…

---

## [Flipping Bits in a Sparse Matrix- Expanding stored entries](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265)

<div class="topic-metadata">

**Author:** [@Phillip\_Sutton](https://discourse.julialang.org/u/Phillip_Sutton)\
**Replies:** 2\
**Last updated:** [September 25, 2020, 2:38pm UTC](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265 "2020-09-25T14:38:26Z")

</div>

My sparse bit array is not dropping non zeros when flipping bits between rows (assignment optimization problem) 8766×11125 SparseArrays.SparseMatrixCSC{Bool,Int64} with 8766 stored entries: \[7244, 1\] = 1 \[2111,…

---

## [What is the best way to regroup the ones to the most right of every nibbles in a UInt?](https://discourse.julialang.org/t/what-is-the-best-way-to-regroup-the-ones-to-the-most-right-of-every-nibbles-in-a-uint/18116)

<div class="topic-metadata">

**Author:** [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Replies:** 7\
**Last updated:** [January 6, 2019, 11:57pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-regroup-the-ones-to-the-most-right-of-every-nibbles-in-a-uint/18116 "2019-01-06T23:57:53Z")

</div>

Deleting both “binary gaps” and “leading zeros” for every nibbles of Integer’s binary representation,(packing the ones, bits packing,deleting/removing zeros in the middle). example: #Let's take N a {UInt32} N=0xa1c7bf2…
