bit-manipulation

Getting the Leftmost Bit

泪湿孤枕 提交于 2019-11-29 16:03:36
I have a 5 bit integer that I'm working with. Is there a native function in Objective-C that will let me know which bit is the leftmost? i.e. I have 01001, it would return 8 or the position. Thanks NSInteger value = 9; NSInteger shift = 1; for(NSInteger bit = value; bit > 1; bit = value >> ++shift); NSInteger leftmostbit = 1 << shift; Works for every number of bits. You can build a lookup table, with 32 elements: 0, 1, 2, 2, 3, etc. Paul R This is effectively the same operation as counting he number of leading 0s. Some CPUs have an instruction for this, otherwise you can use tricks such as

Is it possible to write a function adding two integers without control flow and strictly bitwise operations?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 16:03:36
I misunderstood a question that said to add two integers using bitwise operations. I did not use any control flow and could not do it. After giving up, all the solutions I found use control flow to accomplish this whether it be an if , while , for , recursion, etc,. Is there a proof that is can\cannot be accomplished? For a fixed length integer, you can just unroll a ripple carry adder. In the worst case, a carry signal has to propagate all the way from the least significant bit to the most significant bit. Like this (only slightly tested) (to avoid the C-purists' wrath, I will call this C#

Convert Byte Array to Bit Array?

只愿长相守 提交于 2019-11-29 16:02:42
问题 How would I go about converting a bytearray to a bit array? 回答1: The obvious way; using the constructor that takes a byte array: BitArray bits = new BitArray(arrayOfBytes); 回答2: It depends on what you mean by "bit array"... If you mean an instance of the BitArray class, Guffa's answer should work fine. If you actually want an array of bits, in the form of a bool[] for instance, you could do something like that : byte[] bytes = ... bool[] bits = bytes.SelectMany(GetBits).ToArray(); ...

What does “|=” mean in Java?

馋奶兔 提交于 2019-11-29 15:21:12
Note my question is not regarding != but |= A usage example is here I assume that x |= y is the same as x = x | y but I could not find confirming documentation and wanted to be sure Thanks Yes, it's a bitwise inclusive or assignment: http://www.cafeaulait.org/course/week2/03.html It's a bitwise "or" plus assignment, so you are quite correct in your assumption. More correctly, x |= y is actually computed as x = x | (y). Here is an interesting example of why this is important. int c = 2; c %= c++ * ++c; The interesting consequence here is that it would be written as c = c % (c++ * ++c); Java

Why does XOR swap with integers trigger a warning?

霸气de小男生 提交于 2019-11-29 14:55:01
I typed the following program: #include <stdio.h> int main(void) { int a = 3; int b = 42; printf("a = %d\nb = %d\n", a, b); printf("Exchanging values.\n"); a ^= b ^= a ^= b; printf("a = %d\nb = %d\n", a, b); return 0; } and it's ok. When I try to compile it, I get this: $ gcc test.c -o test -Wall -Wextra -ansi -pedantic-errors test.c: In function ‘main’: test.c:11: warning: operation on ‘a’ may be undefined That's pretty much standard code, isn't it? Why does it trigger a warning? As far as I know, bitwise XOR is implemented by default for int as long as you are using a standard implementation

Multiword addition in C

牧云@^-^@ 提交于 2019-11-29 14:54:05
I have a C program which uses GCC's __uint128_t which is great, but now my needs have grown beyond it. What are my options for fast arithmetic with 196 or 256 bits? The only operation I need is addition (and I don't need the carry bit, i.e., I will be working mod 2^192 or 2^256). Speed is important, so I don't want to move to a general multiprecision if at all possible. (In fact my code does use multiprecision in some places, but this is in the critical loop and will run tens of billions of times. So far the multiprecision needs to run only tens of thousands of times.) Maybe this is simple

Bit shifting in internet checksums

筅森魡賤 提交于 2019-11-29 14:46:04
This is almost certainly a very silly question, but for some reason I'm having trouble with internet checksum calculations. All of the algorithms basically look something like this: WORD chksm(WORD *startpos, WORD checklen){ ulong sum = 0; WORD answer = 0; while (checklen > 1) { sum += *startpos++; checklen -= 2; } if (checklen == 1) { *(BYTE *)(&answer) = *(BYTE *)startpos; sum += answer; } sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); answer = ~sum; return answer;} I'm clear on everything except for the line: sum += (sum >> 16); It looks like the line immediately before it adds the

Signed extension from 24 bit to 32 bit in C++

纵饮孤独 提交于 2019-11-29 14:32:57
I have 3 unsigned bytes that are coming over the wire separately. [byte1, byte2, byte3] I need to convert these to a signed 32-bit value but I am not quite sure how to handle the sign of the negative values. I thought of copying the bytes to the upper 3 bytes in the int32 and then shifting everything to the right but I read this may have unexpected behavior. Is there an easier way to handle this? The representation is using two's complement. You could use: uint32_t sign_extend_24_32(uint32_t x) { const int bits = 24; uint32_t m = 1u << (bits - 1); return (x ^ m) - m; } This works because: if

What does the pipe operator do in SQL?

点点圈 提交于 2019-11-29 14:14:37
I am redeveloping an application and have found this sql statement, what does the | character do in this part (au.ExNetBits | 8) , I haven't seen before and can't find any answer online? SELECT au.AccountID,au.ExNetBits FROM AccountUser au (NOLOCK) WHERE au.CDAGUserId=102 and (au.ExNetBits | 8) = au.ExNetBits from the MSDN documentation , | (Bitwise OR) (Transact-SQL) Performs a bitwise logical OR operation between two specified integer values as translated to binary expressions within Transact-SQL statements. ... The bitwise | operator performs a bitwise logical OR between the two expressions

Number for each enum item?

我只是一个虾纸丫 提交于 2019-11-29 14:08:24
Is it possible to define something like this in java? C# code: public enum Character { A = 1, B = 2, C = 4, D = 8 } ... Character ch = /* from user */ if(ch & Character.A) { // some operation... } for example if ch set to Character.B then result of if will be false : ch = 00000000 00000000 00000000 00000010 A = 00000000 00000000 00000000 00000001 ------------------------------------------ & 00000000 00000000 00000000 00000000 I want to implement something like that! Is it ever possible in Java? Well, you can nearly do that: public enum CharEnum // Let's avoid java.lang.* clashes { A(1), B(2),