bitmask

C++11 standard conformant bitmasks using enum class

橙三吉。 提交于 2019-12-02 17:40:13
Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <. Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the following does not compile with GCC (besides being extremely dangerous as it allows an erroneous

Number of pairs with constant difference and bitwise AND zero

走远了吗. 提交于 2019-12-02 08:34:27
How to find the number of pairs whose difference is a given constant and their bitwise AND is zero? Basically, all (x,y) such that x-y = k; where k is a given constant and x&y = 0; An interesting problem. Let k n-1 ...k 1 k 0 be the the binary representation of k . Let l be the index of the smallest i such that k i =1 We can remark that a potential pair of solutions x and y must have all their bits i , i<l at zero. Otherwise the only way to have a difference x-y with its i th bit unset would be to have x i =y i =1 and x&y will not have its i th bit unset. Now we arrive at the first bit at one

Tagged Pointers for lockFree list in C

旧巷老猫 提交于 2019-12-02 08:10:59
问题 I am trying to use tagged pointers for handling the lock free operations on a list, in order to block the compare-and-swap (CAS) from going through if some other thread operated on the list during this transaction. My node struct and CAS look like this: struct node { unsigned long key; unsigned long val; struct node * next; }; static inline bool CAS(std::atomic<node*> node, struct node* oldNode, struct node* newNode) { node.compare_exchange_strong(oldNode, newNode, std::memory_order_seq_cst);

Tagged Pointers for lockFree list in C

时光怂恿深爱的人放手 提交于 2019-12-02 03:27:29
I am trying to use tagged pointers for handling the lock free operations on a list, in order to block the compare-and-swap (CAS) from going through if some other thread operated on the list during this transaction. My node struct and CAS look like this: struct node { unsigned long key; unsigned long val; struct node * next; }; static inline bool CAS(std::atomic<node*> node, struct node* oldNode, struct node* newNode) { node.compare_exchange_strong(oldNode, newNode, std::memory_order_seq_cst); } I found some methods for setting and checking these pointers but it is unclear to me how they work,

Get specific bit from uint32

[亡魂溺海] 提交于 2019-12-01 17:08:53
问题 I have a UInt32 variable like 3238844000. Now I want to get the first two bits of this number and the 6 bits after the first two bits. Both bits should be int. Decimal: 3238844000 Binary: 11000001000011001101011001100000 ^^ and Decimal: 3238844000 Binary: 11000001000011001101011001100000 ^^^^^^ 回答1: Update 2: The simplest (and also the fastest ) way for this case turns out to be by purely using the bitwise-shift operators int val = (int)(input >> 30); // performs the same int val2 = (int)(

Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how?

北战南征 提交于 2019-11-30 23:53:29
问题 In C I would do this int number = 3510; char upper = number >> 8; char lower = number && 8; SendByte(upper); SendByte(lower); Where upper and lower would both = 54 In C# I am doing this: int number = Convert.ToInt16("3510"); byte upper = byte(number >> 8); byte lower = byte(number & 8); char upperc = Convert.ToChar(upper); char lowerc = Convert.ToChar(lower); data = "GETDM" + upperc + lowerc; comport.Write(data); However in the debugger number = 3510, upper = 13 and lower = 0 this makes no

Is there a practical limit to the size of bit masks?

人盡茶涼 提交于 2019-11-30 22:10:07
There's a common way to store multiple values in one variable, by using a bitmask. For example, if a user has read, write and execute privileges on an item, that can be converted to a single number by saying read = 4 (2^2), write = 2 (2^1), execute = 1 (2^0) and then add them together to get 7. I use this technique in several web applications, where I'd usually store the variable into a field and give it a type of MEDIUMINT or whatever, depending on the number of different values. What I'm interested in, is whether or not there is a practical limit to the number of values you can store like

Storing multiple values via bitmask in c#

别等时光非礼了梦想. 提交于 2019-11-30 16:07:40
I'm trying to store four independent 5-bit values (0-31) inside a 32-bit int via bit mask but am having trouble getting the values correct to set and get the individual values from the masked int used for storage. Can anyone help me with this? Edit: Sorry for the external link - here's some JavaScript demonstrating what I'm trying to achieve (but in bitmasks instead of decimal algebra): var s = 0; var v = [31, 6, 23, 31]; //save values s = v[0] + (v[1] * 32) + (v[2] * 1024) + (v[3] * 32768); console.log(s); //retrieve values v[3] = parseInt(s / 32768); v[2] = parseInt((s - (v[3] * 32768)) /

How do I perform a circular rotation of a byte?

一笑奈何 提交于 2019-11-30 15:00:44
I'm trying to implement a function that performs a circular rotation of a byte to the left and to the right. I wrote the same code for both operations. For example, if you are rotating left 1010 becomes 0101 . Is this right? unsigned char rotl(unsigned char c) { int w; unsigned char s = c; for (w = 7; w >= 0; w--) { int b = (int)getBit(c, w);// if (b == 0) { s = clearBit(s, 7 - w); } else if (b == 1) { s = setBit(s, 7 - w); } } return s; } unsigned char getBit(unsigned char c, int n) { return c = (c & (1 << n)) >> n; } unsigned char setBit(unsigned char c, int n) { return c = c | (1 << n); }

How do I check, if bitmask contains bit?

泄露秘密 提交于 2019-11-30 11:16:09
I don't quite understand this whole bitmask concept. Let's say I have a mask: var bitMask = 8 | 524288; I undestand that this is how I would combine 8 and 524288 , and get 524296 . BUT, how do I go the other way? How do I check my bitmask, to see if it contains 8 and/or 524288 ? To make it a bit more complex, let's say the bitmask I have is 18358536 and I need to check if 8 and 524288 are in that bitmask. How on earth would I do that? well if (8 & bitmask == 8 ) { } will check if the bitmask contains 8. more complex int mask = 8 | 12345; if (mask & bitmask == mask) { //true if, and only if,