bit-manipulation

Adding two numbers without + operator (Clarification)

我只是一个虾纸丫 提交于 2019-12-17 15:34:36
问题 I know that we can use the logic of binary adder where Sum = a XOR b and Carry = a AND b I have also got a solution: int add(int a, int b) { if(b == 0) return sum; sum = a ^ b; carry = (a & b) << 1; return add(sum,carry); } What I don't understand here is why is the carry bit shifted, or multiplied by 2 during each recursion? 回答1: I find this a bit tricky to explain, but here's an attempt; think bit by bit addition, there are only 4 cases; 0+0=0 0+1=1 1+0=1 1+1=0 (and generates carry) The two

Need help understanding “getbits()” method in Chapter 2 of K&R C

∥☆過路亽.° 提交于 2019-12-17 15:25:26
问题 In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works. Here's the method provided: unsigned int getbits(unsigned int x, int p, int n) { return (x >> (p + 1 - n)) & ~(~0 << n); } The idea is that, for the given number x , it will return the n bits starting at position p , counting from the right (with the farthest right bit being position 0). Given the following main() method: int main(void) { int x = 0xF994, p = 4, n

Android In App Billing: securing application public key

杀马特。学长 韩版系。学妹 提交于 2019-12-17 15:19:44
问题 From Android In App Billing version 3 (TrivialDrive)sample application coming with sdk MainActivity.java /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY * (that you got from the Google Play developer console). This is not your * developer public key, it's the *app-specific* public key. * * Instead of just storing the entire literal string here embedded in the * program, construct the key at runtime from pieces or * use bit manipulation (for example, XOR with some other

Generate all sequences of bits within Hamming distance t

家住魔仙堡 提交于 2019-12-17 14:55:46
问题 Given a vector of bits v , compute the collection of bits that have Hamming distance 1 with v , then with distance 2, up to an input parameter t . So for 011 I should get ~~~ 111 001 010 ~~~ -> 3 choose 1 in number 101 000 110 ~~~ -> 3 choose 2 100 ~~~ -> 3 choose 3 How to efficiently compute this? The vector won't be always of dimension 3, e.g. it could be 6. This will run numerous time in my real code, so some efficiency would be welcome as well (even by paying more memory). My attempt:

What happens when you bit shift beyond the end of a variable?

*爱你&永不变心* 提交于 2019-12-17 14:01:50
问题 If you have some variable (on the stack) and you left or right bit shift beyond its end what happens? i.e. byte x = 1; x >> N; What if x is a pointer to memory cast to a byte and you do the same thing? byte* x = obtain pointer from somewhere; *x = 1; *x >> N; 回答1: It does not (necessarily) become zero. The behavior is undefined (C99 §6.5.7, "Bitwise shift operators"): If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the

Determine which single bit in the byte is set

▼魔方 西西 提交于 2019-12-17 13:00:47
问题 I have a byte I'm using for bitflags. I know that one and only one bit in the byte is set at any give time. Ex: unsigned char b = 0x20; //(00100000) 6th most bit set I currently use the following loop to determine which bit is set: int getSetBitLocation(unsigned char b) { int i=0; while( !((b >> i++) & 0x01) ) { ; } return i; } How do I most efficiently determine the position of the set bit? Can I do this without iteration? 回答1: Can I do this without iteration? It is indeed possible. How do I

Weird result of Java Integer left shift

ⅰ亾dé卋堺 提交于 2019-12-17 13:00:31
问题 I'm a little confused now by java left shift operation, 1<<31 = 0x80000000 --> this I can understand But 1<<32 = 1 Why is this? 1<<33 = 2 Looks like more shifting values, modulus 32 of the value is taken. Thanks everybody for the replying and giving the quote from JLS. I just want to know more. Any idea of the reason why it's designed in this way? Or is it just some convention? Apparently C doesn't have this quirk? Thanks to @paxdiablo. Looks like C declares this behaviour undefined. I have

XSLT Bitwise Logic

一个人想着一个人 提交于 2019-12-17 12:39:34
问题 I have an existing data set that utilizes an integer to store multiple values; the legacy front end did a simple bitwise check (e.g. in C#: iValues & 16 == 16) to see if a particular value was set. Is it possible to do bitwise operations in XSL, and more explicitly, to do bit level comparisons via masking? The built-in "and" will always result in "true" or "false", but perhaps it's possible via the math operators available? I'm currently using .NET 2.0, which uses XSLT 1.0. 回答1: I haven't

XSLT Bitwise Logic

丶灬走出姿态 提交于 2019-12-17 12:38:54
问题 I have an existing data set that utilizes an integer to store multiple values; the legacy front end did a simple bitwise check (e.g. in C#: iValues & 16 == 16) to see if a particular value was set. Is it possible to do bitwise operations in XSL, and more explicitly, to do bit level comparisons via masking? The built-in "and" will always result in "true" or "false", but perhaps it's possible via the math operators available? I'm currently using .NET 2.0, which uses XSLT 1.0. 回答1: I haven't

Is there any way to write “mod 31” without modulus/division operators?

▼魔方 西西 提交于 2019-12-17 12:34:56
问题 Getting the modulus of a number can be easily done without the modulus operator or divisions, if your operand is a power of 2. In that case, the following formula holds: x % y = (x & (y − 1)) . This is often many performant in many architectures. Can the same be done for mod 31 ? int mod31(int a){ return a % 31; }; 回答1: Here are two ways to approach this problem. The first one using a common bit-twiddling technique, and if carefully optimized can beat hardware division. The other one