bitwise-operators

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

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:05:28
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 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-complement integer representation of the value of the left operand ; this explains why 1 << 31 is negative

Difference between bitwise inclusive or and exclusive or in java

荒凉一梦 提交于 2019-12-03 07:05:09
public class Operators { public static void main(String[] args) { int a = 12; System.out.println("Bitwise AND:"+(12&12)); System.out.println("Bitwise inclusive OR:"+(12|12)); System.out.println("Bitwise exclusive OR:"+(12^12)); } } OUTPUT: Bitwise AND:12 Bitwise inclusive OR:12 Bitwise exclusive OR:0 I understand first two, but not the third. XOR tells whether each bit is different. 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0 In other words "either but not both" 0011 XOR 0101 = 0110 BITWISE INCLUSIVE OR (|) means normal or operation , BITWISEE ExCLUSIVE OR (^) means xor operation Third one

How do I perform bit operations in glsl

不打扰是莪最后的温柔 提交于 2019-12-03 06:51:08
How do I perform bit operations in glsl? Using the regular C style bitwise operators | , & , ^ , or ! does not work. Aszarsha 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. You need to put either #version 130 or #extension GL_EXT_gpu_shader4 : enable in the top of your shader to get access to the bit operators 来源: https://stackoverflow.com/questions/1700871/how-do-i-perform-bit-operations

align macro kernel

本小妞迷上赌 提交于 2019-12-03 06:02:48
问题 I am unable to understand what this macro does. These are defined in linux-kernel but I my doubt is independent of that. I am unable to understand what does (((x)+(mask))&~(mask)) line does. #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) Any help appreciated. 回答1: Say you have a number: 0x1006 For some reasons you want to align it to a 4 bytes boundary. With a 4-byte boundary, you know aligned values are 0x1000 , 0x1004 , 0x1008 , etc.

Why does asm.js deteriorate performance?

孤街浪徒 提交于 2019-12-03 04:12:06
问题 Just to see how it performs, I wrote a very short asm.js module by hand, which simulates the 2D wave equation using 32-bit integer math and typed arrays (Int32Array). I have three versions of it, all as similar as possible: Ordinary (i.e. legible, albeit C-style) JavaScript Same as 1, with asm.js annotations added so that it passes the validator, according to Firefox and other tools Same as 2, except with no "use asm"; directive at the top I left a demo at http://jsfiddle.net/jtiscione

How to combine Intent flags in Kotlin

别来无恙 提交于 2019-12-03 04:11:59
I want to combine two intent flags as we do bellow in android Intent intent = new Intent(this, MapsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); I tried doing something like this but it didn't work for me val intent = Intent(context, MapActivity::class.java) intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) Willi Mentzel Explanation: The operation that is applied to the flags is a bitwise or. In Java you have the | operator for that. As of bitwise operations [in Kotlin], there're no special characters for them,

Bitwise Rotate Right

这一生的挚爱 提交于 2019-12-03 04:06:47
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 >= 0: return 2**n - 1 else: return 0 def ror(n, rotations=1, width=8): """Return a given number of bitwise

What does a >> mean in Go language?

為{幸葍}努か 提交于 2019-12-03 03:15:21
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 Jon Purdy 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 a power of two has the effect of “shifting” the digits left or right, respectively: // Left shift

use of the bitwise operators to pack multiple values in one int

与世无争的帅哥 提交于 2019-12-03 00:50:01
问题 Low level bit manipulation has never been my strong point. I will appreciate some help in understanding the following use case of bitwise operators.Consider... int age, gender, height, packed_info; . . . // Assign values // Pack as AAAAAAA G HHHHHHH using shifts and "or" packed_info = (age << 8) | (gender << 7) | height; // Unpack with shifts and masking using "and" height = packed_info & 0x7F; // This constant is binary ...01111111 gender = (packed_info >> 7) & 1; age = (packed_info >> 8); I

Is it possible to implement bitwise operators using integer arithmetic?

自古美人都是妖i 提交于 2019-12-03 00:29:31
问题 I am facing a rather peculiar problem. I am working on a compiler for an architecture that doesn't support bitwise operations. However, it handles signed 16-bit integer arithmetics and I was wondering if it would be possible to implement bitwise operations using only: Addition ( c = a + b ) Subtraction ( c = a - b ) Division ( c = a / b ) Multiplication ( c = a * b ) Modulus ( c = a % b ) Minimum ( c = min(a, b) ) Maximum ( c = max(a, b) ) Comparisons ( c = (a < b), c = (a == b), c = (a <= b)