bitwise-operators

Check division by 3 with binary operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 11:35:22
问题 I've read this interesting answer about " Checking if a number is divisible by 3 " Although the answer is in Java , it seems to work with other languages also. Obviously we can do : boolean canBeDevidedBy3 = (i % 3) == 0; But the interesting part was this other calculation : boolean canBeDevidedBy3 = ((int) (i * 0x55555556L >> 30) & 3) == 0; For simplicity : 0x55555556L = "1010101010101010101010101010110" Nb There's also another method to check it : One can determine if an integer is

Bitwise Operator on positive and negative numbers

时光毁灭记忆、已成空白 提交于 2019-12-04 11:07:56
问题 -5 / 2 = -2 -5 >> 1 = -3 I learn from my teacher that >>1 divides the number by 2. It works on positive number but it does not work on negative numbers. Can someone explain to me?? Thanks 回答1: As BЈовић & mystical states, using bit shift operators on negative numbers is implementation defined. The reason for this is C doesn't distinguish between logical and arithmetic bit shifting. (Arithmetic pads with the most significant bit, logical pads with 0's) for positive numbers this doesn't matter,

Determine whether number is odd or even without using conditional code

佐手、 提交于 2019-12-04 11:03:21
How to find whether a number is odd or even, without using if condition or ternary operators in Java? This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator. Pshemo There are few ways to not use if and get behavior that will be same as if if was used, like ternary operator condition ? valueIfTrue : valueIfFalse or switch/case . But to be tricky you can also use arrays and try to figure some transformation of our value to proper array index. In this case your code could look like int number = 13; String[] trick = { "even", "odd" }; System

Java results differ for (int)Math.pow(2,x) and 1<<x

╄→гoц情女王★ 提交于 2019-12-04 10:55:02
问题 Why do the following two operations yield different results in Java for x = 31 or 32 but the same results for x=3 ? int x=3; int b = (int) Math.pow(2,x); int c = 1<<x; Results: x=32: b=2147483647; c=1; x=31: b=2147483647; c=-2147483648; x=3: b=8 ; c=8 回答1: There are multiple issues at play: An int can only store values between -2147483648 and 2147483647 . 1 << x only uses the lowest five bits of x. Thus, 1 << 32 is by definition the same as 1 << 0 . Shift operations are performed on the two's

MySQL doesn't use indexes when query over BIT field using bitwise functions

自闭症网瘾萝莉.ら 提交于 2019-12-04 10:08:25
I have a field of type BIT in my MySQL table. I want to store statuses of the record using bit value, for example: 1 = status1 2 = status2 4 = status3 8 = status4 Each record can have many statuses at once. For status1 and status3 the value will be 1 + 4 = 5. I can query table for all records with status3 using: SELECT * FROM `table` WHERE `statuses` & 4 I have index on statuses , but EXPLAIN tells that no index is used. Can I use index in such situation? P.S. Using separate many-to-many linking table is more normalized solution, but I'd like to have more 'flat' structure for this. It would be

Bitwise Rotate Right

假如想象 提交于 2019-12-04 09:35:03
问题 I am trying to convert this C function into Python; typedef unsigned long var; /* Bit rotate rightwards */ var ror(var v,unsigned int bits) { return (v>>bits)|(v<<(8*sizeof(var)-bits)); } I have tried Googling for some solutions, but I can't seem to get any of them to give the same results as the one here. This is one solution I have found from another program; def mask1(n): """Return a bitmask of length n (suitable for masking against an int to coerce the size to a given length) """ if n >=

What does a >> mean in Go language?

末鹿安然 提交于 2019-12-04 09:14:13
问题 I’m looking for information on Google’s Go language. In “A Tour of Go” they have this code: const ( Big = 1<<100 Small = Big>>99 ) But what do << and >> mean? You can see all of the code at http://tour.golang.org/#14 回答1: They are bitwise shift operators. x << y means x × 2 y , while x >> y means x × 2 −y or, equivalently, x ÷ 2 y . These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by

The best way to shift a __m128i?

柔情痞子 提交于 2019-12-04 08:39:38
I need to shift a __m128i variable, (say v), by m bits, in such a way that bits move through all of the variable (So, the resulting variable represents v*2^m). What is the best way to do this?! Note that _mm_slli_epi64 shifts v0 and v1 seperately: r0 := v0 << count r1 := v1 << count so the last bits of v0 missed, but I want to move those bits to r1. Edit: I looking for a code, faster than this (m<64): r0 = v0 << m; r1 = v0 >> (64-m); r1 ^= v1 << m; r2 = v1 >> (64-m); For compile-time constant shift counts, you can get fairly good results. Otherwise not really. This is just an SSE

Inverting a binary value of a number

天大地大妈咪最大 提交于 2019-12-04 07:21:24
I would like first to convert a number to binary, then invert it bitwise. Like this: Number is 793 = 1100011001 then convert the binary value into: 0011100110 In JavaScript I can do the following: var x = 793; document.write(x.toString(2)); // gives 0011100110 This will give me the binary value of the number. But how do I invert the binary bitwise? I tried the ~ operator, but not working probably... The output is: -1100011010 MooGoo's answer is correct. Here is some information about what is happening.... Lets assume this is a 64 bit integer. 793 = 1100011001 ~793 = -794 =

What's the point of logical operators vs. bitwise operators

一个人想着一个人 提交于 2019-12-04 06:56:55
问题 Given this statement is a logical operation ((a > 5) && (b > 4)) And this statement is bitwise-operation ((a > 5) & (b > 4)) Above two statements is not equivalent. Because (a > 5) is an element of {0,1} So, why do we need logical operators & bitwise-operation ? Edit : thanks for all of the feedback. Regarding the short circuit behaviour of logical operators, I actually do not want this behaviour - I am writing code for a GPU where branches degrade performance: short circuit results in two