bit-manipulation

Count number of bits in an unsigned integer

最后都变了- 提交于 2019-11-29 23:34:23
问题 I want to write a function named bitCount() in the file: bitcount.c that returns the number of bits in the binary representation of its unsigned integer argument. Here is what I have so far: #include <stdio.h> int bitCount (unsigned int n); int main () { printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n", 0, bitCount (0)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 1, bitCount (1)); printf ("# 1-bits in base 2 representation of %u = %d, should be

Number of bits set in a number [closed]

馋奶兔 提交于 2019-11-29 23:12:09
The following the magical formula which gives the number of bits set in a number (Hamming weight). /*Code to Calculate count of set bits in a number*/ int c; int v = 7; v = v - ((v >> 1) & 0x55555555); // reuse input as temporary v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count printf(" Number of Bits is %d",c); /*-----------------------------------*/ from: http://graphics.stanford.edu/~seander/bithacks.html Can anyone please explain me the rationale behind this? It's really quite clever code, and is obviously a lot more

Bit Shifting, Masking or a Bit Field Struct?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 22:54:12
问题 I'm new to working with bits. I'm trying to work with an existing protocol, which can send three different types of messages. Type 1 is a 16-bit structure: struct digital { unsigned int type:2; unsigned int highlow:1; unsigned int sig1:5; unsigned int :1; unsigned int sig2:7; }; The first two bits (type, in my struct above) are always 1 0 . The third bit, highlow, determines whether the signal is on or off, and sig1 + sig2 together define the 12-bit index of the signal. This index is split

Why in Java (high + low) / 2 is wrong but (high + low) >>> 1 is not?

陌路散爱 提交于 2019-11-29 22:15:28
I understand the >>> fixes the overflow: when adding two big positive longs you may endup with a negative number. Can someone explain how this bitwise shift magically fixes the overflow problem? And how it is different than >> ? My suspicious: I think it has to do with the fact that Java uses two-compliments so the overflow is the right number if we had the extra space but because we don't it becomes negative. So when you shift and paddle with zero it magically gets fixed due to the two-compliments. But I can be wrong and someone with a bitwise brain has to confirm. :) In short, (high + low) >

Java right shift integer by 32 [duplicate]

徘徊边缘 提交于 2019-11-29 22:12:24
问题 This question already has an answer here: Findbugs warning: Integer shift by 32 — what does it mean? 2 answers I am trying to right shift an integer by 32 but the result is the same number. (e.g. 5 >> 32 is 5.) If I try to do same operation on Byte and Short it works. For example, "(byte)5 >> 8" is 0. What is wrong with Integer? 回答1: JLS 15.19. Shift Operators ... If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the

Do bitwise operators (other than shifts) make any mathematical sense in base-10?

笑着哭i 提交于 2019-11-29 20:57:05
According to wiki shifts can be used to calculate powers of 2: A left arithmetic shift by n is equivalent to multiplying by 2^n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2^n and rounding toward negative infinity. I was always wondering if any other bitwise operators ( ~ , | , & , ^ ) make any mathematical sense when applied to base-10? I understand how they work, but do results of such operations can be used to calculate anything useful in decimal world? "yep base-10 is what I mean" In that case, yes,

Using Bitwise operators on flags

本小妞迷上赌 提交于 2019-11-29 20:50:41
I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All = 0x7 Say I receive the two flags Past and Future ( setFlags(PAST | FUTURE) ). How can I tell if Past is in it? Likewise how can I tell that Current is not in it? That way I don't have to test for every possible combination. If you want all bits in the test mask to match: if((value & mask) == mask) {...} If you want any single bit in the test mask to match: if((value & mask) != 0) {...} The difference is most apparent when you are testing a value for multiple things. To test for exclusion: if ((value & mask) == 0) { } First of all -

Bit reversal of an integer, ignoring integer size and endianness

只愿长相守 提交于 2019-11-29 20:50:22
问题 Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I have the following code to reverse the bits of an integer: TYPE max_bit= (TYPE)-1; void reverse_int_setup() { TYPE bits= (TYPE)max_bit; while (bits <<= 1) max_bit= bits; } TYPE reverse_int(TYPE arg) { TYPE bit_setter= 1, bit_tester= max_bit, result= 0; for (result= 0; bit_tester; bit_tester>>= 1, bit_setter<<= 1) if (arg & bit_tester) result|= bit_setter; return result; } One just needs first to run reverse

Change a bit of an integer [duplicate]

自作多情 提交于 2019-11-29 20:26:33
This question already has an answer here: How do you set, clear, and toggle a single bit? 27 answers We have an integer number int x = 50; in binary, it's 00110010 How can I change the fourth (4th) bit programatically? You can set the fourth bit of a number by OR-ing it with a value that is zero everywhere except in the fourth bit. This could be done as x |= (1u << 3); Similarly, you can clear the fourth bit by AND-ing it with a value that is one everywhere except in the fourth bit. For example: x &= ~(1u << 3); Finally, you can toggle the fourth bit by XOR-ing it with a value that is zero

What does AND 0xFF do?

若如初见. 提交于 2019-11-29 20:17:07
In the following code: short = ((byte2 << 8) | (byte1 & 0xFF)) What is the purpose of &0xFF ? Because other somestimes I see it written as: short = ((byte2 << 8) | byte1) And that seems to work fine too? John Colanduoni Anding an integer with 0xFF leaves only the least significant byte. For example, to get the first byte in a short s , you can write s & 0xFF . This is typically referred to as "masking". If byte1 is either a single byte type (like uint8_t ) or is already less than 256 (and as a result is all zeroes except for the least significant byte) there is no need to mask out the higher