bit-manipulation

Getting the Leftmost Bit

橙三吉。 提交于 2019-12-03 18:11:12
问题 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 回答1: 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. 回答2: You can build a lookup table, with 32 elements: 0, 1, 2, 2, 3, etc. 回答3: This is effectively the same operation as

Why does XOR swap with integers trigger a warning?

若如初见. 提交于 2019-12-03 18:10:26
问题 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

What is the most efficient way in Java to pack bits into byte[] and read it back?

被刻印的时光 ゝ 提交于 2019-12-03 17:03:27
问题 I currently use these two functions to pack and read bits in a byte array. Wondering if anybody has any better ideas or faster ways to do it? Edited the program with a few more optimization and tabled a few calculations. Currently 100mil Put and Get takes about 12 secs instead of 16 secs now. If anybody is using the current code make sure the value passed in to Put is a positive number as it's expecting unsigned numbers coming down. If there is interest I can put up signed and unsigned

Is it possible to rewrite modulo (2^n - 1) using bitwise and restricted operators

两盒软妹~` 提交于 2019-12-03 17:02:57
问题 For unsigned int x, is it possible to calculate x % 255 (or 2^n - 1 in general) using only the following operators (plus no loop, branch or function call)? ! , ~ , & , ^ , | , + , << , >> . 回答1: Yes, it's possible. For 255, it can be done as follows: unsigned int x = 4023156861; x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); // At this point, x will be in the range: 0 <= x < 256. // If the answer 0, x could potentially be 255 which is

How to find number of 1's in a binary number in O(1) time?

空扰寡人 提交于 2019-12-03 17:02:02
I know this has been asked before, but I'm looking at this particular solution listed here : int BitCount(unsigned int u) { unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } How does it work? Are there any caveats involved here? Theoretically is it possible to find the answer in constant time? I mean don't we actually have to iterate through the bits to count? Counting bits An unsigned 32-bit integer u can be written like this: u = a 31 * 2 31 + a 30 * 2 30 + ... + a 0 * 2 0 We want the value of a

Concatenate binary numbers of different lengths

丶灬走出姿态 提交于 2019-12-03 16:05:58
So I have 3 numbers. One is a char , and the other two are int16_t (also known as short s, but according to a table I found shorts won't reliably be 16 bits). I'd like to concatenate them together. So say that the values of them were: 10010001 1111111111111101 1001011010110101 I'd like to end up with a long long containing: 1001000111111111111111011001011010110101000000000000000000000000 Using some solutions I've found online, I came up with this: long long result; result = num1; result = (result << 8) | num2; result = (result << 24) | num3; But it doesn't work; it gives me very odd numbers

How to use bitwise operators in if statements?

荒凉一梦 提交于 2019-12-03 16:05:10
问题 I want to write something like this: if [[ ( releases["token"] & $MASK ) -eq 1 ]]; then but I get the error that: unexpected token `&', conditional binary operator expected How do I use bitwise operators in if statements? 回答1: You can use Arithmetic Expansion: (((5&3)==1)) && echo YES || echo NO It will print YES 回答2: Logic vs Syntax .... or: "What's more to say?" At first sight, as pointed out in @chepner's comment, the question might only be one of syntax, which causes the compilation error

How to copy bits from one variable to another?

邮差的信 提交于 2019-12-03 15:13:37
Let's say I have this int variable v1 : 1100 1010 And this variable int v2 : 1001 1110 I need to copy the last four bits from v2 to the last four bits of v1 so that the result is: 1100 1110 ^ ^ last four bits of v2 | | first four bits of v1 How would I got about doing this in C or C++? I read a few articles about bitwise operations but I couldn't find any information specifically about this. Bitwise operations were the right things to look for. v1 = (v1 & ~0xf) | (v2 & 0xf); Is there something specific you didn't understand from the articles you read? How about v1 = (v1 & 0xf0) | (v2 & 0xf);

Why is abs(0x80000000) == 0x80000000?

寵の児 提交于 2019-12-03 14:59:20
问题 I just started reading Hacker's Delight and it defines abs(-2 31 ) as -2 31 . Why is that? I tried printf("%x", abs(0x80000000)) on a few different systems and I get back 0x80000000 on all of them. 回答1: For a 32bit datatype there is no expression of +2^31, because the biggest number is 2^31-1 ... read more about the two's complement ... 回答2: Actually, in C, the behavior is undefined. From the C99 standard, §7.20.6.1/2: The abs , labs , and llabs functions compute the absolute value of an

Bitwise operators and signed types

依然范特西╮ 提交于 2019-12-03 14:42:44
I'm reading C++ Primer and I'm slightly confused by a few comments which talk about how Bitwise operators deal with signed types. I'll quote: Quote #1 (When talking about Bitwise operators) "If the operand is signed and its value is negative, then the way that the “sign bit” is handled in a number of the bitwise operations is machine dependent. Moreover, doing a left shift that changes the value of the sign bit is undefined" Quote #2 (When talking about the rightshift operator) "If that operand is unsigned, then the operator inserts 0-valued bits on the left; if it is a signed type, the result