bitwise-operators

Are & and <and> equivalent in python? [duplicate]

假如想象 提交于 2021-02-19 05:42:08
问题 This question already has answers here : 'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays? (8 answers) Closed 5 years ago . Is there any difference in the logic or performance of using the word and vs. the & symbol in Python? 回答1: and is a Boolean operator. It treats both arguments as Boolean values, returning the first if it's falsy, otherwise the second. Note that if the first is falsy, then the second argument isn't even computed at all, which is

Number of trailing zeroes

送分小仙女□ 提交于 2021-02-10 14:43:47
问题 I've written a function trailing_zeroes(int n) that returns the number of the trailing zeroes in the binary representation of a number. Example : 4 in binary is 100 , so the function in this case returns 2 . unsigned trailing_zeroes(int n) { unsigned bits; bits = 0; while (n >= 0 && !(n & 01)) { ++bits; if (n != 0) n >>= 1; else break; } return bits; } The reason of the if statement is because in case n equals to 0, there will be a loop. I think it's pretty ugly this code written like this;

Number of trailing zeroes

假如想象 提交于 2021-02-10 14:42:13
问题 I've written a function trailing_zeroes(int n) that returns the number of the trailing zeroes in the binary representation of a number. Example : 4 in binary is 100 , so the function in this case returns 2 . unsigned trailing_zeroes(int n) { unsigned bits; bits = 0; while (n >= 0 && !(n & 01)) { ++bits; if (n != 0) n >>= 1; else break; } return bits; } The reason of the if statement is because in case n equals to 0, there will be a loop. I think it's pretty ugly this code written like this;

bitiwise exclusive or ^ with stdbool.h bool types in C and assignment

时光总嘲笑我的痴心妄想 提交于 2021-02-10 06:42:08
问题 I have a toy program in which I use the <stdbool.h> library to use the type bool . I have a few arrays of bool 's, and the following commands work in a loop. I have read online that I should not use bit-wise logic with bools. They appear to work here. Is this just luck of the draw with my compiler, or did I just misunderstand warnings about bitwise operators? #include <stdio.h> #include <stdbool.h> #define N 5 int main(void) { bool a[N] = { true, true, false, false, false }; bool b[N] = {

Pair with Maximum AND value

回眸只為那壹抹淺笑 提交于 2021-02-07 10:32:24
问题 Given a very large array of integers in which element can go upto 10^9 how do I find a pair with maximum AND value. My current approach is I calculate all possible pairs and traverse through it and find maximum, however it is very slow. Any other approach? 回答1: As long as you can find at least two numbers with the same most significant bit set, the solution will involve two of them. Next, discard all other elements and remove everything left of this MSB for the numbers not discarded and solve

What does x += x & -x mean?

谁都会走 提交于 2021-02-07 03:28:38
问题 I found that many people use x += x & -x , x -= x & -x to solve the interval tree problem. Can you explain what that equation means? void update(int m, int x) { m++; while (m < N) { t[m] = t[m] + x; m += m & -m; } } int query(int m) { int result= 0; m++; while (m > 0) { result = result + t[m]; m -= m & -m; } return result; } 回答1: Note: This answer (like the method itself) assumes signed integers are represented in two's complement form. The expression x & -x is a quick - but admittedly arcane

How do Bitwise Operators work?

只愿长相守 提交于 2021-02-05 11:49:27
问题 So I was going through some problems at codewars.com and I came across a problem about basic encryption (https://www.codewars.com/kata/basic-encryption/javascript). The goal is to get a string value and shift it x values to the right on the ASCII chart. here was my initial solution: function encrypt(text, rule) { let res = '' for(let i = 0; i<text.length; i++) { res += (String.fromCharCode(text.charCodeAt(i)+rule)) } return res }; However, it didn't pass all of the tests, so I looked at the

How can I store 4 8 bit coordinates into one integer (C#)?

牧云@^-^@ 提交于 2021-02-05 09:40:57
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To

How can I store 4 8 bit coordinates into one integer (C#)?

老子叫甜甜 提交于 2021-02-05 09:40:52
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To

How can I store 4 8 bit coordinates into one integer (C#)?

折月煮酒 提交于 2021-02-05 09:40:41
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To