bit-manipulation

What is (x & 1) and (x >>= 1)?

我只是一个虾纸丫 提交于 2020-08-17 12:41:26
问题 I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer to a bit representation shows something like this: do { Vec.push_back( x & 1 ) } while ( x >>= 1 ); I don't want to just copy paste stuff. When I use F-10 I see what (x & 1) is doing but I don't know it is name or how it does its job(compare

Bitwise operation in Group By

ぐ巨炮叔叔 提交于 2020-08-09 16:04:30
问题 I must use bitwise operations in a group by query but I didn't found anything. Table: PermissionId, BitMask(BigInt) 1, 4 2, 7 1, 8 1, 5 I want results as: 1, 13 2, 7 How can I write this script in T-SQL as below SELECT PermissionId, BIT_OR(BitMask) FROM table GROUP BY PermissionId 回答1: Your question just became very interesting. Create this function(you may want to reconsider the name) CREATE function f_test ( @param bigint ) returns @t table (value bigint) AS BEGIN ;WITH CTE AS ( SELECT

Reading top nibble and bottom nibble in a byte

走远了吗. 提交于 2020-07-30 11:54:09
问题 What's the correct way to handle two distinct values being stored in one byte of data. I have a byte that contains two nibbles each containing their own data. I want to read the top nibble and the bottom nibble into their own variables. 11110000 = High 4 bits throttle, to be read into $throttle , and should be a value from 0 to 15. 00001111 = Low 4 bits brake, to be read into $brake , and should be a value from 0 to 15. Don't forget , drivers can apply the throttle and the brake at the same

Bitwise - How can I check if a binary number contains another?

不羁岁月 提交于 2020-07-20 07:54:12
问题 A = 110000000 - 384 Blue+Red B = 011000010 - 194 Green+Black+Red A & B = C = 010000000 - 128 Red How can I check if B contains all the bits in A and perhaps others? In the case above I would like to get "false". I'm using XCode & objective-c but that shouldn't matter as far as I know 回答1: B contains A if A & B (ie, the intersection) is equal to A: (a & b) == a Which is analogous to a ⊆ b ↔ (a ∩ b) = a from set theory. 回答2: If you mean exactly the same bits, the test is A == B . If you mean B

Bitwise - How can I check if a binary number contains another?

筅森魡賤 提交于 2020-07-20 07:53:43
问题 A = 110000000 - 384 Blue+Red B = 011000010 - 194 Green+Black+Red A & B = C = 010000000 - 128 Red How can I check if B contains all the bits in A and perhaps others? In the case above I would like to get "false". I'm using XCode & objective-c but that shouldn't matter as far as I know 回答1: B contains A if A & B (ie, the intersection) is equal to A: (a & b) == a Which is analogous to a ⊆ b ↔ (a ∩ b) = a from set theory. 回答2: If you mean exactly the same bits, the test is A == B . If you mean B

Python representation of negative integers

為{幸葍}努か 提交于 2020-07-15 01:21:30
问题 >>> x = -4 >>> print("{} {:b}".format(x, x)) -4 -100 >>> mask = 0xFFFFFFFF >>> print("{} {:b}".format(x & mask, x & mask)) 4294967292 11111111111111111111111111111100 >>> >>> x = 0b11111111111111111111111111111100 >>> print("{} {:b}".format(x, x)) 4294967292 11111111111111111111111111111100 >>> print("{} {:b}".format(~(x ^ mask), ~(x ^ mask))) -4 -100 I am having trouble figuring out how Python represents negative integers, and therefore how bit operations work. It is my understanding that

How to split a 64-bit number into eight 8-bit values?

一曲冷凌霜 提交于 2020-07-10 08:14:56
问题 Is there a simple way to split one 64-bit ( unsigned long long ) variable into eight int8_t values? For example: //1001000100011001100100010001100110010001000110011001000110011111 unsigned long long bigNumber = 10455547548911899039; int8_t parts[8] = splitULongLong(bigNumber); parts would be something along the lines of: [0] 10011111 [1] 10010001 [2] 00011001 ... [7] 10010001 回答1: { uint64_t v= _64bitVariable; uint8_t i=0,parts[8]={0}; do parts[i++]=v&0xFF; while (v>>=8); } 回答2: First you

Get an array of bits that represent an int in c#

試著忘記壹切 提交于 2020-07-09 16:55:48
问题 Is there a way to show the representation of an int in bits in c#? i.e. 1 = 00001 20 = 10100 etc. I have tried using BitConverter with no luck. This should be simple, but I can't find a solution! 回答1: Convert.ToString(value, base) Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base. Specify 2 for the base. 回答2: Here's a one-liner using linq: var myint = 20; var bytes = Enumerable.Range(0, 32).Select(b => (myint >> b) & 1); // { 0, 0, 1, 0,

Get an array of bits that represent an int in c#

自作多情 提交于 2020-07-09 16:55:12
问题 Is there a way to show the representation of an int in bits in c#? i.e. 1 = 00001 20 = 10100 etc. I have tried using BitConverter with no luck. This should be simple, but I can't find a solution! 回答1: Convert.ToString(value, base) Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base. Specify 2 for the base. 回答2: Here's a one-liner using linq: var myint = 20; var bytes = Enumerable.Range(0, 32).Select(b => (myint >> b) & 1); // { 0, 0, 1, 0,

What is 0xFF and why is it shifted 24 times?

橙三吉。 提交于 2020-07-04 07:22:34
问题 #define SwapByte4(ldata) \ (((ldata & 0x000000FF) << 24) | \ ((ldata & 0x0000FF00) << 8) | \ ((ldata & 0x00FF0000) >> 8) | \ ((ldata & 0xFF000000) >> 24)) What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24? 回答1: Here is a hex value, 0x12345678, written as binary, and annotated with some bit positions: |31 24|23 16|15 8|7 bit 0| +---------------+---------------+---------------+---------------+ |0 0 0 1 0 0 1 0|0 0 1 1 0 1 0 0|0 1 0 1 0 1