BitHacks | PDF Versions of Sean Anderson
kandi X-RAY | BitHacks Summary
kandi X-RAY | BitHacks Summary
Sean Anderson's wonderful compilation of Bit Twiddling Hacks, converted to a more readable Markdown version and accompanying PDF.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of BitHacks
BitHacks Key Features
BitHacks Examples and Code Snippets
Community Discussions
Trending Discussions on BitHacks
QUESTION
I've been reviewing bit manipulation and have been looking at this page: http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs
...ANSWER
Answered 2021-Mar-21 at 20:26It is 31 bits, assuming that sizeof(int) == 4
and CHAR_BIT == 8
. The binary *
operator has higher precedence than binary -
, so this is parsed as (sizeof(int) * CHAR_BIT) - 1
, which is (4 * 8) - 1 == 31
.
QUESTION
in the past week I struggled with this problem, and it seems I can't handle it finally. Given an arbitrary 64bit unsigned int number, if it contains the binary pattern of 31 (0b11111) at any bit position, at any bit settings, the number is valid, otherwise not.
E.g.:
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1111 valid
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 1110 valid
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 1100 valid
0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1000 0000 0000 0000 0000 0000 valid
0000 0000 0011 1110 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 1110 valid etc...
also:
0000 0000 0000 1100 0000 0100 0000 0000 1100 1100 0000 0000 0100 0000 0001 1111 valid
1110 0000 0000 0100 0000 0000 0011 0000 0000 0000 0000 0000 0000 0000 0011 1110 valid
0000 0000 1000 0010 0000 0010 0000 0000 0000 0000 0010 0000 0000 0000 0111 1100 valid
0000 0010 0000 0110 0000 0000 0000 0000 0000 1111 1000 0000 0000 0100 0110 0000 valid
0000 0000 0011 1110 0000 0000 0011 0000 0000 1000 0000 0000 0000 0000 0011 1110 valid etc...
but:
0000 0000 0000 1100 0000 0100 0000 0000 1100 1100 0000 0000 0100 0000 0000 1111 invalid
1110 0000 0000 0100 0000 0000 0011 0000 0000 0000 0000 0000 0000 0000 0011 1100 invalid
0000 0000 1000 0010 0000 0010 0000 0000 0000 0000 0010 0000 0000 0000 0101 1100 invalid
0000 0010 0000 0110 0000 0000 0000 0000 0000 1111 0000 0000 0000 0100 0110 0000 invalid
0000 0000 0011 1010 0000 0000 0011 0000 0000 1000 0000 0000 0000 0000 0001 1110 invalid etc...
You've got the point...
But that is just the first half of the problem. The second one is, it needs to be implemented without loops or branches (which was done already) for speed increasing, using only one check by arithmetic and/or logical, bit manipulation kind of code.
The closest I can get, is a modified version of Bit Twiddling Hacks "Determine if a word has a zero byte" ( https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord ) to check five bit blocks of zeros (negated 11111). But it still has the limit of the ability to check only fixed blocks of bits (bit 0 to 4, bit 5 to 9, etc...) not at any bit position (as in the examples above).
Any help would be greatly appreciated, since I'm totally exhausted.
Sz
...ANSWER
Answered 2021-Mar-03 at 11:29Let me restate your goal in a slightly different formulation:
I want to check whether an integer contains 5 consecutive high bits.
From this formulation the following solution explains itself. It is written in C++.
QUESTION
I saw this code called "Counting bits set, Brian Kernighan's way". I am puzzled as to how "bitwise and'ing" an integer with its decrement works to count set bits, can someone explain this?
...ANSWER
Answered 2020-Jan-09 at 23:08Each time though the loop one bit is counted, and one bit is cleared (set to zero).
How this works is: when you subtract one from a number you change the least significant one bit to a zero, and the even less significant bits to one -- though that doesn't matter. It doesn't matter because they are zero in the values you're decrementing, so they will be zero after the and-operation anyway.
QUESTION
Looking to do modulo operator, A mod K
where...
- K is a uint32_t constant, is not a power of two, and I will be using it over and over again.
- A is a uint32_t variable, possibly as much as ~2^13 times larger than K.
- The ISA does not have single cycle modulo or division instructions. (8-bit micro)
The naive approach seems to coincide with the naive approach to division; repeat subtraction until underflow, then keep the remainder. This would obviously have fairly bad worst case performance, but would work for any A and K.
A known fast approach which works well for a K that is some power of two, is to logical AND with that power of two -1.
From Wikipedia...
A % 2^n == A & (2^n - 1)
My knee jerk reaction is to use these two things together, and I'm wondering if that is valid?
Specifically, I figure I can use the power of two mod trick to narrow the worst case for the above subtraction method. In other words, quickly mod to the nearest power of two above my constant, then subtract my constant if necessary. Here's the code that is in the actual question, fully expanded.
...ANSWER
Answered 2019-Dec-26 at 10:47You can't combine both the approaches. First understand why does the below equation holds true.
QUESTION
I am working with an algorithm that performs many popcount/sideways addition up to a given index for a 32 bit type. I am looking to minimize the operations required to perform what I have currently implemented as this:
...ANSWER
Answered 2019-May-12 at 14:42Thanks everyone for the suggestions, I decided to pit all the methods I had come across head to head as I couldn't find any similar tests.
N.B. The population counts shown are for indexes up to argv[1]
, not a popcount of argv[1]
- 8x 32-bit arrays make up 256 bits. The code used to produce these results can be seen here.
On my Ryzen 1700,For my usage, the fastest population count was (often) the one on page 180 of the Software Optimization Guide for AMD64 Processors. This (often) remains true for larger population counts too.
QUESTION
I am looking for the fastest way to convert a stream of integers into a list that counts consecutive ones and zeros.
For example the integers
[4294967295,4194303,3758096384]
are at bit level:
...ANSWER
Answered 2018-Mar-16 at 09:58Earlier I had misunderstood the question. Now i know what You were asking. This should work, I've tested it:
QUESTION
I need to merge two variables into one like in example below:
...ANSWER
Answered 2018-Sep-25 at 21:48This should do the trick in C++ and C:
QUESTION
I'm currently working to create a function which accepts two 4 byte unsigned integers, and returns an 8 byte unsigned long. I've tried to base my work off of the methods depicted by this research but all my attempts have been unsuccessful. The specific inputs I am working with are: 0x12345678
and 0xdeadbeef
, and the result I'm looking for is 0x12de34ad56be78ef
. This is my work so far:
ANSWER
Answered 2018-Sep-06 at 23:02You could do it like this:
QUESTION
I have a boolean array A of size n that I want to transform in the following way : concatenate every prefix of size 1,2,..n.
For instance, for n=5, it will transform "abcde" into "aababcabcdabcde".
Of course, a simple approach can loop over every prefix and use elementary bit operations (shift, mask, plus); so the complexity of this approach is obviously O(n).
There are some well known tricks regarding bit manipulations like here.
My question is: is it possible to achieve a faster algorithm for the transformation described above with a complexity better than O(n) by using bit manipulations ?
I am aware that the interest of such an improvement may be just theoretical because the simple approach could be the fastest in practice, but I am still curious about the fact that there exists a theoretical improvement or not.
As a precision, I need to perform this transformation p times, where p can be much bigger than n; some pre-computations could be done for a given n and used later for the computation of the p transformations.
...ANSWER
Answered 2018-Jul-18 at 14:25I'm not sure if this is what you're looking for, but here is a different algorithm, which may be interesting depending on your assumptions.
First compute two masks that depend only on n
, so for any particular n
these are just constants:
C
(copy mask), a mask that has every n'th bit set and is n² bits long. So forn = 5
,C = 0000100001000010000100001
. This will be used to create n copies of A concatenated together.E
(extract mask), a mask that indicates which bits to take from the big concatenation, which is built up from n times a block of n bits, with values 1, 3, 7, 15 ... eg forn = 5
,E = 1111101111001110001100001
. Pad the left with zeroes if necessary.
Then the real computation that takes an A and constructs the concatenation of prefixes is:
QUESTION
I'm trying to implement a lock-free queue that uses a linear circular buffer to store data. In contrast to a general-purpose lock-free queue I have the following relaxing conditions:
- I know the worst-case number of elements that will ever be stored in the queue. The queue is part of a system that operates on a fixed set of elements. The code will never attempt to store more elements in the queue as there are elements in this fixed set.
- No multi-producer/multi-consumer. The queue will either be used in a multi-producer/single-consumer or a single-producer/multi-consumer setting.
Conceptually, the queue is implemented as follows
- Standard power-of-two ring buffer. The underlying data-structure is a standard ring-buffer using the power-of-two trick. Read and write indices are only ever incremented. They are clamped to the size of the underlying array when indexing into the array using a simple bitmask. The read pointer is atomically incremented in
pop()
, the write pointer is atomically incremented inpush()
. - Size variable gates access to
pop()
. An additional "size" variable tracks the number of elements in the queue. This eliminates the need to perform arithmetic on the read and write indices. The size variable is atomically incremented after the entire write operation has taken place, i.e. the data has been written to the backing storage and the write cursor has been incremented. I'm using a compare-and-swap (CAS) operation to atomically decrement size inpop()
and only continue, if size is non-zero. This waypop()
should be guaranteed to return valid data.
My queue implementation is as follows. Note the debug code that halts execution whenever pop()
attempts to read past the memory that has previously been written by push()
. This should never happen, since ‒ at least conceptually ‒ pop()
may only proceed if there are elements on the queue (there should be no underflows).
ANSWER
Answered 2018-Apr-30 at 13:40You have two bugs, one of which can cause the failure you observe.
Let's look at your push code, except we'll allow only one operation per statement:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install BitHacks
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page