bitwise-operators

When should I use a bitwise operator?

无人久伴 提交于 2020-01-19 04:51:04
问题 I read the following Stack Overflow questions, and I understand the differences between bitwise and logical. Difference between & and && in PHP Reference - What does this symbol mean in PHP? However, none of them explains when I should use bitwise or logical. When should I use bitwise operators rather than logical ones and vice versa? In which situation do I need to compare bit by bit? I am not asking about the differences, but I am asking the situation when you need to use bitwise operators.

is it possible to bit mask a char array

怎甘沉沦 提交于 2020-01-17 08:32:27
问题 Let's say I have the following char array char array[32]; I want to use only the 10 most significant bits of this array as a hash value. Is it possible to use bitwise operation on this char array? If so, how should i do it? 回答1: Assuming your implementation has 8-bit char , and that you have a 256-bit number stored in big endian in this array, here how to access the 10 msb of the 256-bit number. uint16_t a; a = (array[0] << 2 | (array[1] & 0xC0) >> 6) & 0x3FF; 回答2: I'm pretty sure you want

JavaScript — pass a boolean (or bitwise) operator as an argument?

谁说我不能喝 提交于 2020-01-16 14:33:29
问题 In C# there are various ways to do this C# Pass bitwise operator as parameter specifically the "Bitwise.Operator.OR" object, but can something like this be done in JavaScript? For example: function check(num1, num2, op) { return num1 op num2; //just an example of what the output should be like } check(1,2, >); //obviously this is a syntax error, but is there some kind of other object or bitwise operator of some kind I can plug into the place of ">" and change the source function somehow? 回答1:

C# Pass bitwise operator as parameter

≡放荡痞女 提交于 2020-01-14 03:15:08
问题 How can I pass a bitwise operator as a parameter to my method? I've read some articles that describes how to pass for example equality operator as a parameter however they implement it in some way and after this pass it with a delegate. In my case I'm not sure how to implement the bitwise operator. 回答1: You can use Func<> int MyFunc(int input1, int input2, Func<int, int, int> bitOp) { return bitOp(input1, input2); } Use like this Console.WriteLine(MyFunc(1, 2, (a, b) => a | b)); Outputs "3"

Bitwise shift in mysql

末鹿安然 提交于 2020-01-13 15:00:32
问题 How can I make a bitwise shift in MySQL? Is there any specific instruction or operator? If not, how to simulate it optimally? 回答1: Have a look at the bitwise operators in MySQL first: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html Then you have left shift: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html#operator_left-shift And right shift: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html#operator_right-shift 回答2: To use a bitwise shift in MySQL, you can use << or

(Not 1) evaluates to -2 for some reason

半腔热情 提交于 2020-01-13 13:31:34
问题 Why does (Not 1) evaluate as -2? I would expect it to evaluate as 0. 回答1: VBA/VBScript does not have real logical operators (AND, OR, NOT). The logical operators you see are actually bitwise operators, and that's all you get. VBA plays some games with the True and False values so this works most of the time, but occasionally you'll find a "gotcha". In this case, instead of If Not InStr() Then you have to write If InStr() <= 0 Then . Instead of If InStr() Then you have to write If InStr() > 0

How do I perform bit operations in glsl

北战南征 提交于 2020-01-12 05:28:05
问题 How do I perform bit operations in glsl? Using the regular C style bitwise operators | , & , ^ , or ! does not work. 回答1: They have been introduced with GLSL 1.30 (OGL 3.0). Depending on what you want to do, you could eventually emulate them with floating point operations, x & (2^n)-1 = frac(x/(2^n))*(2^n) for example, but you'll have to take care of floating point errors. 回答2: You need to put either #version 130 or #extension GL_EXT_gpu_shader4 : enable in the top of your shader to get

CUDA: Why are bitwise operators sometimes faster than logical operators?

谁都会走 提交于 2020-01-11 04:35:31
问题 When I am down to squeezing the last bit of performance out of a kernel, I usually find that replacing the logical operators ( && and || ) with bitwise operators ( & and | ) makes the kernel a little bit faster. This was observed by looking at the kernel time summary in CUDA Visual Profiler. So, why are bitwise operators faster than logical operators in CUDA? I must admit that they are not always faster, but a lot of times they are. I wonder what magic can give this speedup. Disclaimer: I am

Can bitwise operators have undefined behavior?

廉价感情. 提交于 2020-01-10 02:30:06
问题 Bitwise operators ( ~ , & , | and ^ ) operate on the bitwise representation of their promoted operands. Can such operations cause undefined behavior? For example, the ~ operator is defined this way in the C Standard: 6.5.3.3 Unary arithmetic operators The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the

Pipe character in Python

别说谁变了你拦得住时间么 提交于 2020-01-08 13:20:34
问题 I see a "pipe" character ( | ) used in a function call: res = c1.create(go, come, swim, "", startTime, endTime, "OK", ax|bx) What is the meaning of the pipe in ax|bx ? 回答1: It is a bitwise OR of integers. For example, if one or both of ax or bx are 1 , this evaluates to 1 , otherwise to 0 . It also works on other integers, for example 15 | 128 = 143 , i.e. 00001111 | 10000000 = 10001111 in binary. 回答2: This is also the union set operator set([1,2]) | set([2,3]) This will result in set([1, 2,