bit-manipulation

why is 1>>32 == 1?

不问归期 提交于 2019-12-17 05:04:28
问题 I'm wondering if perhaps this is a JVM bug? java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode) class Tmp { public static void main(String[] args) { System.out.println("1>>1 = "+(1>>1)); System.out.println("1>>2 = "+(1>>2)); System.out.println("1>>31 = "+(1>>31)); System.out.println("1>>32 = "+(1>>32)); System.out.println("1>>33 = "+(1>>33)); } } produces this when I run it: 1>>1 = 0 1>>2 = 0 1>>31 =

why is 1>>32 == 1?

巧了我就是萌 提交于 2019-12-17 05:04:02
问题 I'm wondering if perhaps this is a JVM bug? java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode) class Tmp { public static void main(String[] args) { System.out.println("1>>1 = "+(1>>1)); System.out.println("1>>2 = "+(1>>2)); System.out.println("1>>31 = "+(1>>31)); System.out.println("1>>32 = "+(1>>32)); System.out.println("1>>33 = "+(1>>33)); } } produces this when I run it: 1>>1 = 0 1>>2 = 0 1>>31 =

Java “Bit Shifting” Tutorial? [closed]

五迷三道 提交于 2019-12-17 04:25:54
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I would be thankful for a good tutorial, that explain for java newbies how in java all the "bit shifting" work. I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with byteshifting/bitmanipulation in java. This is just an example

Workaround for basic syntax not being parsed

北战南征 提交于 2019-12-17 03:20:27
问题 I want to have a class property that allow for an expression to take place on the right side of the equals sign. All versions of PHP choke on the following code, but it is written in this way to allow for easier extendibility in the future. /* Example SDK Class */ class SDK { /* Runtime Option Flags */ // Strings # 0: Makes no change to the strings. var $STRING_NONE = (1 << 0); # 1: Removes color codes from the string. var $STRING_STRIP_COLOR = (1 << 1); # 2: Removes language codes from the

BitSet to and from integer/long

百般思念 提交于 2019-12-17 03:18:49
问题 If I have an integer that I'd like to perform bit manipulation on, how can I load it into a java.util.BitSet ? How can I convert it back to an int or long? I'm not so concerned about the size of the BitSet -- it will always be 32 or 64 bits long. I'd just like to use the set() , clear() , nextSetBit() , and nextClearBit() methods rather than bitwise operators, but I can't find an easy way to initialize a bit set with a numeric type. 回答1: The following code creates a bit set from a long value

C/C++ check if one bit is set in, i.e. int variable

别来无恙 提交于 2019-12-17 02:41:33
问题 int temp = 0x5E; // in binary 0b1011110. Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking. Just want to know if there is some built in function for this, or am I forced to write one myself. 回答1: In C, if you want to hide bit manipulation, you can write a macro: #define CHECK_BIT(var,pos) ((var) & (1<<(pos))) and use it this way to check the n th bit from the right end: CHECK_BIT(temp, n - 1) In C++, you can use std::bitset. 回答2: Check if bit N (starting

Fast computing of log2 for 64-bit integers

牧云@^-^@ 提交于 2019-12-17 02:36:04
问题 A great programming resource, Bit Twiddling Hacks, proposes (here) the following method to compute log2 of a 32-bit integer: #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n static const char LogTable256[256] = { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7) }; unsigned int v; // 32-bit word to find the log of unsigned r; // r will be lg(v) register unsigned int t, tt; //

Division without using '/'

可紊 提交于 2019-12-17 02:35:09
问题 Can anyone tell me an efficient approach to perform the division operation without using '/'. I can calculate the integer value in log(n) steps using a method similar to binary search. 115/3 57 * 3 > 115 28 * 3 < 115 47 * 3 > 115 . . . 38 * 3 is quotient value ..... But is there any other more efficient method? 回答1: The typical way is to shift and subtract. This is basically pretty similar to long division as we learned it in school. The big difference is that in decimal division you need to

Proper way for casting uint16 to int16 in Go

◇◆丶佛笑我妖孽 提交于 2019-12-14 04:19:38
问题 Bitwise manipulation and Go newbie here :D I am reading some data from sensor with Go and I get it as 2 bytes - let's say 0xFFFE . It is easy too cast it to uint16 since in Go we can just do uint16(0xFFFE) but what I need is to convert it to integer, because the sensor returns in fact values in range from -32768 to 32767. Now I thought "Maybe Go will be this nice and if I do int16(0xFFFE) it will understand what I want?" , but no. I ended up using following solution (I translated some python

Converting a set of booleans to a number

帅比萌擦擦* 提交于 2019-12-14 04:18:09
问题 This is the code I am going to use to take a set of three booleans and convert it into an int for a switch statement: int bits = 0; bool a = true, b = false, c = true; // 101 = 5 bits = bits | a << 2; bits = bits | b << 1; bits = bits | c; cout << bits; I have eight cases based on the combined state of these three booleans. Am I doing this right? Right, not in the sense of the syntax although if there are any problems there please advise. More right in the sense of "Is this the best way to