bit-manipulation

How do you implement XOR using +-*/?

亡梦爱人 提交于 2019-12-03 11:08:55
How can the XOR operation (on two 32 bit ints) be implemented using only basic arithmetic operations? Do you have to do it bitwise after dividing by each power of 2 in turn, or is there a shortcut? I don't care about execution speed so much as about the simplest, shortest code. Edit: This is not homework, but a riddle posed on a hacker.org . The point is to implement XOR on a stack-based virtual machine with very limited operations (similar to the brainfuck language and yes - no shift or mod). Using that VM is the difficult part, though of course made easier by an algorithm that is short and

bitwise & doesn't work with bytes in kotlin

喜欢而已 提交于 2019-12-03 10:51:50
I'm trying to write kotlin code like: for (byte b : hash) stringBuilder.append(String.format("%02x", b&0xff)); but I have nothing to do with the "&". I'm trying to use "b and 0xff" but it doesn't work. The bitwise "and" seems to work on Int, not byte. java.lang.String.format("%02x", (b and 0xff)) it's ok to use 1 and 0xff Kolin provides bitwise operator-like infix functions available for Int and Long only. So it's necessary to convert bytes to ints to perform bitwise ops: val b : Byte = 127 val res = (b.toInt() and 0x0f).toByte() // evaluates to 15 UPDATE: Since Kotlin 1.1 these operations are

re implement modulo using bit shifts?

夙愿已清 提交于 2019-12-03 10:48:46
I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my code, as of now one cycle of my mainloop does not run in 1/60 of a second as it should. I was wondering if it was possible to re-implement the modulo using only bit shifts like is possible with multiplication and division. So here is my code so far in c++ (if i can perform a modulo using assembly it would be even better). How can I remove the modulo

Structures with bitwise data in C++ [duplicate]

此生再无相见时 提交于 2019-12-03 10:48:26
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Converting Bit Field to int I am working on an application, part of which handles 16-bit words that contain a number of 1-bit flags. I am handling the data using a structure similar to the one shown below: struct mystruct { uint16_t Reserved1 :3; uint16_t WordErr :1; uint16_t SyncErr :1; uint16_t WordCntErr :1; uint16_t Reserved2 :10; }; i.e. the structure contains a single 16-bit variable that is handled as a

Is static_cast<T>(-1) the right way to generate all-one-bits data without numeric_limits?

家住魔仙堡 提交于 2019-12-03 10:41:01
问题 I'm writing C++ code in an environment in which I don't have access to the C++ standard library, specifically not to std::numeric_limits . Suppose I want to implement template <typename T> constexpr T all_ones( /* ... */ ) Focusing on unsigned integral types, what do I put there? Specifically, is static_cast<T>(-1) good enough? (Other types I could treat as an array of unsigned chars based on their size I guess.) 回答1: Use the bitwise NOT operator ~ on 0 . T allOnes = ~(T)0; A static_cast<T>(

n is negative, positive or zero? return 1, 2, or 4

此生再无相见时 提交于 2019-12-03 10:23:13
I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if the last result was positive, 4 otherwise. My first naive method to interpret this is: if (n < 0) cr0 = 1 else if (n > 0) cr0 = 2; else cr0 = 4; However I understand that all those branches won't be optimal, being run millions of times per second. I've seen some bit hacking on SO, but none seemed adeguate. For example I found many examples to

JavaScript Endian Encoding?

你。 提交于 2019-12-03 10:19:55
问题 A response on SO got me thinking, does JavaScript guarantee a certain endian encoding across OSs and browsers? Or put another way are bitwise shifts on integers "safe" in JavaScript? 回答1: Yes, they are safe. Although you're not getting the speed benefits you might hope for since JS bit operations are "a hack". 回答2: Shifting is safe, but your question is flawed because endianness doesn't affect bit-shift operations anyway. Shifting left is the same on big-endian and little-endian systems in

Invert 1 bit in C#

≡放荡痞女 提交于 2019-12-03 09:57:17
I have 1 bit in a byte (always in the lowest order position) that I'd like to invert. ie given 00000001 I'd like to get 00000000 and with 00000000 I'd like 00000001. I solved it like this: bit > 0 ? 0 : 1; I'm curious to see how else it could be done. How about: bit ^= 1; This simply XOR's the first bit with 1, which toggles it. If you want to flip bit #N, counting from 0 on the right towards 7 on the left (for a byte), you can use this expression: bit ^= (1 << N); This won't disturb any other bits, but if the value is only ever going to be 0 or 1 in decimal value (ie. all other bits are 0),

Fastest way to get sign in Java?

江枫思渺然 提交于 2019-12-03 09:27:13
I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a = ...; int sign = a >> 31; //0 for pos, 1 for neg sign = ~sign; //1 for pos, 0 for neg sign = sign << 1; //2 for pos, 0 for neg sign -= 1; //-1 for pos, 1 for neg -- perfect. Or more concisely: int sign = (~(a >> 31) << 1) - 1; Does this seem like a good approach? Will this work for all platforms, given endianness concerns (as MSB holds sign)? assylias Any

Fast way of finding most and least significant bit set in a 64-bit integer

一曲冷凌霜 提交于 2019-12-03 09:25:06
问题 There are a lot of questions about this on StackOverflow. A lot . However I cannot find an answer that: Works in C# Works for 64-bit integers (as opposed to 32-bit) Faster than: private static int Obvious(ulong v) { int r = 0; while ((v >>= 1) != 0) { r++; } return r; } Or Even int r = (int)(Math.Log(v,2)); I'm assuming a 64-bit Intel CPU here. One useful reference is the Bit Hacks page and another is fxtbook.pdf However, while these gives useful direction to approach the problem, they do not