Category "bit-manipulation"

Worst-case time complexity for adding two binary numbers using bit manipulation

I'm looking over the solution to adding 2 binary numbers, using the bit manipulation approach. The input are 2 binary-number strings, and the expected output is

What does this char string related piece of C++ code do?

bool check(const char *text) { char c; while (c = *text++) { if ((c & 0x80) && ((*text) & 0x80)) { return true;

How to get char from first bits per byte in uint?

I have uint64_t variable with some value (for example 0x700a06fffff48517). I want to get char with the first bit of each byte in the uint (so from 0x700a06fffff

Optimal Selection for minimum total sum

This is a problem from competitive programmer's handbook: We are given the prices of k products over n days, and we want to buy each product exactly once. Howev

Comparing the Most Significant Bit of two numbers: ==, <, <=

Is there a quick bit operation to implement msb_equal: a function to check if two numbers have the same most significant bit? For example, 0b000100 and 0b0001

Switching the first 4 bits of a byte and the last half

I need to switch the first half and the second half of a byte: Make 0011 0101 to 0101 0011 for example I thought it might work this way: For example, i have 10

Apparent no-op in Rust assembly output?

I was studying the x86_64 assembly for the following function: /// Returns the greatest power of two less than or equal to `self`, or 0 otherwise. pub const fn

How to store a *signed* 24 bit int into another variable?

I need to encode a 24 bit integer into the end of 32 bit int. (the first byte contains other data, the other three are empty for use by the 24 bit int) I alr

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also f

When bitwise and & operation is greater or equal to bitwise xor ^ operation?

When bitwise & operation is greater or equal to bitwise ^ operation? For example, 4&7 >= 4^7 is true. How do we know when it will be true? The one wa

How to find if there are n consecutive set bits in a 32 bit buffer?

Maybe you can help me with the following problem that can help me speed a memory manager I am thinking of (I am not sure a solution exists – I did not fin

XOR Operation Intuition

I recently came across this question on Leetcode and figured out a solution that I need some clarification with: Given an array of integers, every element a

bitwise operators with an if

I am reading K&R book for c language and in section 2.10 they give the following example: /*bitcount: count 1 bits in x*/ int bitcount(unsigned x) { in

What are assembly instructions like PEXT actually used for?

I watched a youtube video on the Top 10 Craziest Assembly Language Instructions and some of these instructions have no obvious application to me. What's the poi

Finding consecutive bit string of 1 or 0

How to find the length of the longest consecutive bit string(either 1 or 0)? 00000000 11110000 00000000 00000000 -> If it is 0 then length will be 20 111