bit-manipulation

What's the best way to toggle the MSB?

牧云@^-^@ 提交于 2019-12-20 02:27:41
问题 So I want to toggle the most significant bit of my number. Here is an example: x = 100101 then answer should be 00101 I have a 64 bit machine and hence I am not expecting the answer to be 100000..<51 0's>..100101 One way I thought of was to count the number of bits in my number and then toggle the MSB, but not sure on how to count. 回答1: The cheat is to pawn it off to the compiler: There are instructions in most CPUs for doing work like this. The following should do what you want. i ^ (1 <<

Why doesn't the bitwise & operator short-circuit?

寵の児 提交于 2019-12-19 18:23:53
问题 We all know that the logical && operator short circuits if the left operand is false , because we know that if one operand is false , then the result is also false . Why doesn't the bitwise & operator also short-circuit? If the left operand is 0 , then we know that the result is also 0 . Every language I've tested this in (C, Javascript, C#) evaluates both operands instead of stopping after the first. Is there any reason why it would be a bad idea the let the & operator short-circuit? If not,

What does the bitwise code “$n & ($n - 1)” do?

丶灬走出姿态 提交于 2019-12-19 17:56:42
问题 What does this code mean and what are other ways accomplish the same without using bit shifting? if ($n & ($n - 1)) 回答1: That formula checks to see whether a number is a power of 2 (if your condition as written is true, then the number is not a power of two). Stated another way, your test checks to see whether there is more than one "1" bit set in the binary representation of $n . If there is zero or only one bit set, then your test will be false. It is by far the most efficient way to

java.util.BitSet — set() doesn't work as expected

送分小仙女□ 提交于 2019-12-19 17:36:11
问题 Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet? The following test fails: @Test public void testBitSet() throws Exception { BitSet b = new BitSet(); b.set(0, true); b.set(1, false); assertEquals(2, b.length()); } It's really unclear to me why I don't end up with a BitSet of length 2 and the value 10. I peeked at the source for java.util.BitSet, and on casual inspection it seems to fail to make sufficient distinction between a bit that

How to implement arithmetic right shift from logical shift?

江枫思渺然 提交于 2019-12-19 11:28:36
问题 I need to implement a 32-bit arithmetic right shift from logical shifts, and, or, xor and normal integer arithmetic operations. I read somewhere the following is supposed to work: (x>>N)|(((1<<N)-1)<<(32-N)) x is the integer that will be shifted and N is the amount of bits to shift. This works for negative (msb is 1) numbers but not for positive numbers (msb is 0). Does anyone know an efficient algorithm that always produces the right result? 回答1: You can use this (x >> N) | (-(x < 0) << (32

Verilog bit change location

浪子不回头ぞ 提交于 2019-12-19 10:44:09
问题 Assuming I have a register reg [15:0] my_reg , which contains a 16-bit signed sample: How can I find the place where the first bit change is located? Meaning, that if assuming that my_reg = 16'b0001011011010111 , how can I know that the first change from 0 to 1 is at my_reg [12] ? Same for numbers starting with 1 ,negative numbers, e.g. my_reg = 16'b1111011011010111 would be interested in the position of the first appearing 0 (which is 11 in this case). The ultimate goal (to add a little bit

How to set multiple bits in one line in C?

拜拜、爱过 提交于 2019-12-19 09:52:14
问题 I'd write two lines to set, say, some bits to something. Here, for example, I want to set upper 8 bits in uint16_t value x to y's lower 8 bits. uint16_t y = 0x0034; uint16_t x = 0xFF12; I want to have x: assert(x == 0x3412); I tend to write these two lines: x &= 0x00FF; x |= (y << 8); Is there a way of writing a single line to achieve the same effect without using macro? 回答1: Just expand out the two lines: x &= 0x00FF; // x = x & 0xFF x |= (y<<8); // x = x | (y<<8) // and combine x = (x &

PHP equivalent javascript >>> shift right with zero fill bitwise operators?

我的梦境 提交于 2019-12-19 09:18:17
问题 May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript. I just managed to discover a function as follow: function zeroFill($a, $b) { $z = hexdec(80000000); if ($z & $a) { $a = ($a>>1); $a &= (~$z); $a |= 0x40000000; $a = ($a>>($b-1)); } else { $a = ($a>>$b); } return $a; } but unfortunately, it doesn't work perfectly. EG: -1149025787 >>> 0 Javascript returns 3145941509 PHP zeroFill() return 0 回答1: /** * The >>> javascript operator in php x86

Set individual bit in C++

这一生的挚爱 提交于 2019-12-19 09:10:09
问题 I have a 5 byte data element and I need some help in figuring out how in C++ to set an individual bit of one of these byte; Please see my sample code below: char m_TxBuf[4]; I would like to set bit 2 to high of byte m_TxBuf[1] . 00000 0 00 ^ This one Any support is greatly appreciated; Thanks! 回答1: Bitwise operators in C++. "...set bit 2..." Bit endianness. I would like to set bit 2 to high of byte m_TxBuf[1]; m_TxBuf[1] |= 1 << 2 回答2: You can use bitwise-or ( | ) to set individual bits, and

C - Algorithm for Bitwise operation on Modulus for number of not a power of 2

六眼飞鱼酱① 提交于 2019-12-19 08:52:07
问题 I know that modulo of power of 2 can be calculated using bitwise operator x % 2^n == x & (2^n - 1). But I am wondering is there any generalized bitwise algorithm exists to find the modulus of any number is not a power of 2. For example, 7%5 Thank you in advance. 回答1: There are a couple, for special cases, including 5. Since 16 ≡ 1 (mod 5), a trick you could do is split your variable into 4-bit nibbles, look up the modulus of each nibble in a table, and add the values together to get the