bitwise-operators

Why bitwise shift with 0 in JavaScript yields weird results in some cases

旧巷老猫 提交于 2019-12-10 15:28:17
问题 Just played around with unusual bitwise operations in JavaScript and I got some weird results in some cases: Usual cases 1 << 0 // returns 1, makes sense 100 << 0 // returns 100, makes sense 100 >> 0 // returns 100, definitely makes sense But these, when shift by 0 bits, all yield zero 9E99 << 0 // returns 0 ..... Why all bits are cleared? 9E99 >> 0 // returns 0 also ..... All bits cleared? Infinity >> 0 // returns 0 Infinity << 0 // returns 0 -Infinity << 0 // returns 0 .... Can't explain

Bitwise operation in C to compare two integers

♀尐吖头ヾ 提交于 2019-12-10 14:51:37
问题 I was recently given a quiz in one of my classes. The question is below: Write a function (called cmp ) in C that accepts two integers ( x and y ) and returns: -1 if x < y , 0 if x = y , 1 if x > y . Write cmp as concise as possible. The most concise function that I could think of was: int cmp(int x, int y) { return ((x < y) ? (-1) : ((x == y) ? (0) : (1))); } But I have a feeling there could be a bit manipulation that I could use to do this more concisely. Perhaps a combination of & and ^ ?

Bitwise enum (flags) query using MongoDB's official C# driver

孤者浪人 提交于 2019-12-10 14:41:59
问题 When I try to run a LINQ query of the form: MongoCollection<MyEntity> collection; collection.AsQueryable().Where(entity => (entity.Flags & MyFlags.AFlag) != MyFlags.None); I get an ArgumentException with the message Unsupported where clause: ((Int32)((Int32)entity.Flags & 4) != 0). Is this a known bug/feature? Is there any workaround? From the documentation it seems like MongoDB has a bitwise update, but not a bitwise query. For comparison, the same query runs smoothly above Redis using

How to turn a division into a bitwise shift when power of two?

夙愿已清 提交于 2019-12-10 14:33:17
问题 I have the following division that I need to do often: int index = pos / 64; Division can be expensive in the cpu level. I am hoping there is a way to do that with bitwise shift. I would also like to understand how you can go from division to shift, in other words, I don't want to just memorize the bitwise expression. 回答1: int index = pos >> 6 will do it, but this is unnecessary. Any reasonable compiler will do this sort of thing for you. Certainly the Sun/Oracle compiler will. The general

Bitwise operators in java only for integer and long?

♀尐吖头ヾ 提交于 2019-12-10 14:02:22
问题 I wrote following code in Eclipse: byte b = 10; /* some other operations */ b = ~b; Eclipse wanted a cast to byte in the line of the bitwise complement. It said: "Type mismatch: cannot convert from int to byte". I also tried this with other bitwise operations and on other integral types. It was with short and char the same. Only long and integer could use bitwise operations. Is there a reason for this? 回答1: Unary (such as ~ ) and binary operators in Java subject their operands to "unary

Efficiency of Bitwise XOR in c++ in comparison to more readable methods

牧云@^-^@ 提交于 2019-12-10 12:57:37
问题 I've recently been writing some code for a research project that I'm working on, where efficiency is very important. I've been considering scraping some of the regular methods I do things in and using bitwise XORs instead. What I'm wondering is if this will make if a difference (if I'm performing this operation say several million times) or if it's the same after I use 03 in g++. The two examples that come to mind: I had an instance where (I'm working with purely positive ints) I needed to

Why are bitwise operators slower than multiplication/division/modulo?

我与影子孤独终老i 提交于 2019-12-10 12:55:29
问题 It's a well known fact that multiplication, integer division, and modulo by powers of two can be rewritten more efficiently as bitwise operations: >>> x = randint(50000, 100000) >>> x << 2 == x * 4 True >>> x >> 2 == x // 4 True >>> x & 3 == x % 4 True In compiled languages such as C/C++ and Java, tests have shown that bitwise operations are generally faster than arithmetic operations. (See here and here). However, when I test these in Python, I am getting contrary results: In [1]: from

How can I access the sign bit of a number in C++?

我的未来我决定 提交于 2019-12-10 12:53:54
问题 I want to be able to access the sign bit of a number in C++. My current code looks something like this: int sign bit = number >> 31; That appears to work, giving me 0 for positive numbers and -1 for negative numbers. However, I don't see how I get -1 for negative numbers: if 12 is 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1100 then -12 is 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0011 and shifting it 31 bits would make 0000 0000 0000 0000 0000 0000 0000 0000 0000

How to combine two image byte[] arrays together quickly utilizing a mask array in C#

岁酱吖の 提交于 2019-12-10 12:14:57
问题 In a nutshell I have two images I want to overlay one over the other using a mask so that only parts of the second image show up. This is part of a real time image processing routine so I need the operation to happen as fast as possible. Specifically, I have two 32 bit image BGR byte arrays. In addition, I have a byte array that represents an image mask. I want to generate a new byte array where byte array A is overlayed on top of byte array B using the mask array to decide which byte is used

Rounded division by power of 2

独自空忆成欢 提交于 2019-12-10 01:16:08
问题 I'm implementing a quantization algorithm from a textbook. I'm at a point where things pretty much work, except I get off-by-one errors when rounding. This is what the textbook has to say about that: Rounded division by 2^p may be carried out by adding an offset and right-shifting by p bit positions Now, I get the bit about the right shift, but what offset are they talking about? Here's my sample code: def scale(x, power2=16): if x < 0: return -((-x) >> power2) else: return x >> power2 def