bits

What is the fastest way to count set bits in UInt32

主宰稳场 提交于 2019-11-28 09:26:26
What is the fastest way to count the number of set bits (i.e. count the number of 1s) in an UInt32 without the use of a look up table? Is there a way to count in O(1) ? Manuel Amstutz Is a duplicate of: how-to-implement-bitcount-using-only-bitwise-operators or best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer And there are many solutions for that problem. The one I use is: int NumberOfSetBits(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; } The bit-twiddling hacks page has a

What is the best way to do Bit Field manipulation in Python?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 07:20:53
I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the way C does bit fields (without having to revert to C). Suggestions? It's an often-asked question. There's an ASPN Cookbook entry on it that has served me in the past. And there is an extensive page of requirements one person would like to see from a module doing this. The bitstring

How is arctan implemented?

家住魔仙堡 提交于 2019-11-28 07:00:43
Many implementation of the library goes deep down to FPATAN instuction for all arc-functions. How is FPATAN implemented? Assuming that we have 1 bit sign, M bits mantissa and N bits exponent, what is the algorithm to get the arctangent of this number? There should be such algorithm, since the FPU does it. Trigonometric functions do have pretty ugly implementations that are hacky and do lots of bit fiddling. I think it will be pretty hard to find someone here that is able to explain an algorithm that is actually used. Here is an atan2 implementation: https://sourceware.org/git/?p=glibc.git;a

Portable code - bits per char

主宰稳场 提交于 2019-11-28 06:49:24
I know that the C/C++ standards only guarantee a minimum of 8 bits per char, and that theoretically 9/16/42/anything else is possible, and that therefore all sites about writing portable code warn against assuming 8bpc. My question is how "non-portable" is this really? Let me explain. As I see it, there a 3 categories of systems: Computers - I mean desktops, laptops, servers, etc. running Mac/Linux/Windows/Unix/*nix/posix/whatever (I know that list isn't strictly correct, but you get the idea). I would be very surprised to hear of any such system where char is not exactly 8 bits. (please

How is 0x80000000 equated to -2147483648 in java?

ぃ、小莉子 提交于 2019-11-28 03:25:57
问题 Taking the binary of 0x80000000 we get 1000 0000 0000 0000 0000 0000 0000 0000 How does this equate to -2147483648 . I got this question with this program. class a { public static void main(String[] args) { int a = 0x80000000; System.out.printf("%x %d\n",a,a); } } meow@VikkyHacks:~/Arena/java$ java a 80000000 -2147483648 EDIT I learned that 2's complement is used to represent negative numbers. When I try to equate this with that 1's complement would be 1's Comp. :: 0111 1111 1111 1111 1111

How do I extract bits from 32 bit number

非 Y 不嫁゛ 提交于 2019-11-28 01:39:35
问题 I have do not have much knowledge of C and im stuck with a problem since one of my colleague is on leave. I have a 32 bit number and i have to extract bits from it. I did go through a few threads but im still not clear how to do so. I would be highly obliged if someone can help me. Here is an example of what i need to do: Assume hex number= 0xD7448EAB. In binary= 1101 01 11 0100 0100 1000 11 10 1010 1011 I need to extract 16 bits, and output that value. I want bits 10 through 25. The lower 10

Byte to Binary String C# - Display all 8 digits

守給你的承諾、 提交于 2019-11-27 23:25:31
I want to display one byte in textbox. Now I'm using: Convert.ToString(MyVeryOwnByte, 2); But when byte is has 0's at begining those 0's are being cuted. Example: MyVeryOwnByte = 00001110 // Texbox shows -> 1110 MyVeryOwnByte = 01010101 // Texbox shows -> 1010101 MyVeryOwnByte = 00000000 // Texbox shows -> <Empty> MyVeryOwnByte = 00000001 // Texbox shows -> 1 I want to display all 8 digits. WraithNath Convert.ToString(MyVeryOwnByte, 2).PadLeft(8, '0'); This will fill the empty space to the left with '0' for a total of 8 characters in the string How you do it depends on how you want your output

Shifting a 32 bit integer by 32 bits

好久不见. 提交于 2019-11-27 23:19:47
I'm slinging some C code and I need to bitshift a 32 bit int left 32 bits. When I run this code with the parameter n = 0, the shifting doesn't happen. int x = 0xFFFFFFFF; int y = x << (32 - n); Why doesn't this work? Shift at your own peril. Per the standard, what you want to do is undefined behavior . C99 §6.5.7 3 - The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined. In other words

How to check if value has even parity of bits or odd?

拜拜、爱过 提交于 2019-11-27 21:33:12
A value has even parity if it has an even number of 1 bits. A value has an odd parity if it has an odd number of 1 bits. For example, 0110 has even parity, and 1110 has odd parity. I have to return 1 if x has even parity. int has_even_parity(unsigned int x) { return } Try: int has_even_parity(unsigned int x){ unsigned int count = 0, i, b = 1; for(i = 0; i < 32; i++){ if( x & (b << i) ){count++;} } if( (count % 2) ){return 0;} return 1; } valter TypeIA x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; x ^= x >> 2; x ^= x >> 1; return (~x) & 1; Assuming you know ints are 32 bits. Let's see how this works.

Find out number of bits needed to represent a positive integer in binary?

本秂侑毒 提交于 2019-11-27 21:06:43
This is probably pretty basic, but to save me an hour or so of grief can anyone tell me how you can work out the number of bits required to represent a given positive integer in Java? e.g. I get a decimal 11, (1011). I need to get the answer, 4. I figured if I could work out how to set all the bits other than the most significant bit to 0, and then >>> it, I'd get my answer. But... I can't. i_am_jorf Well, you can just count how many times you shift right before you're left with just zero: int value = 11; int count = 0; while (value > 0) { count++; value = value >> 1; } Varkhan Well, the