bit-manipulation

Expand Right Bitwise Algorithm

我只是一个虾纸丫 提交于 2019-12-10 21:51:10
问题 Originally this post requested an inverse sheep-and-goats operation, but I realized that it was more than I really needed, so I edited the title, because I only need an expand-right algorithm, which is simpler. The example that I described below is still relevant. Original Post: I'm trying to figure out how to do either an inverse sheep-and-goats operation or, even better, an expand-right-flip. According to Hacker's Delight, a sheeps-and-goats operation can be represented by: SAG(x, m) =

What is the correct way to check if bit field is turn on in php

末鹿安然 提交于 2019-12-10 20:41:46
问题 What is the correct way to check if bit field is turn on - (in php) ? I want to check a bit field that come from db(mysql) if is turn on or not. is this is the correct way ? if($bit & 1) Are there other ways ? I see somebody code that using ord() function , it is correct ? like if(ord($bit) == 1) 回答1: Use if( $bit & (1 << $n) ) { // do something } Where $n is the n -th bit to get minus one (for instance, $n=0 to get the least significant bit) 回答2: It's a bit late but might be usefull for

Combining two bytes in Java

旧时模样 提交于 2019-12-10 18:45:08
问题 Having two bytes, how to make a new byte by taking the first 3 bits from the first byte and the last 5 from the second ? For instance, how would that for 11100000 and 00011111 ==> 11111111 ? I am using Java. 回答1: byte b1, b2; take first 3 bits: b1 & 0xE0 take last 5 bits: b2 & 0x1F concatenate: b1 | b2 回答2: You can use the BitSet class. There's an example in here. 回答3: Using the masks 0xE0 (11100000) and 0x1F (00011111), you can mask out the bits you don't want and bitwise or them together:

Why is `int >> 32` not always zero? [duplicate]

纵然是瞬间 提交于 2019-12-10 18:42:44
问题 This question already has answers here : Weird behavior of right shift operator (1 >> 32) (7 answers) Closed 4 years ago . Can someone explain me why right 32 bitwise shift of some 4 byte integer number may return not zero in C/C++ ? Why does it depend on -O option of the compiler? For example this code gives 45 with -O0 and 0 with -O3 options in gcc 4.8.3: unsigned int x = 45; // 4 bytes x = x >> 32; printf("%u\n", x); Why it is like this? 回答1: Because it is undefined behavior: [expr.shift]

Convert rows of hexadecimal values to binary, vertically by column

只愿长相守 提交于 2019-12-10 18:06:25
问题 I am working with data coming from a serial device which outputs its data in a very interesting format. The device has a 256x256 array of pixels, whereas each pixel has a 14-bit value, read-out with a shift register. To show the format, I will illustrate it as it would look if each pixel had a 6-bit value: 'Pixel #' 0-8 9-16 17-24 25-32 33-40 41-48 48-56 57-64 ... 256 -------------------------------------------------------------- 0 255 255 255 255 255 255 255 255 ... 1 127 255 255 255 255 255

Swapping Values with XOR [duplicate]

∥☆過路亽.° 提交于 2019-12-10 17:42:16
问题 This question already has answers here : Sequence Point - Xor Swap on Array get wrong result (1 answer) How does XOR variable swapping work? (9 answers) Closed 5 years ago . What is the difference between these two macros? #define swap(a, b) (((a) ^ (b)) && ((a) ^= (b) ^= (a) ^= (b))) Or #define swap(a, b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b))) I saw the second macro here but couldn't understand why it wasn't written like the first one? Is there a special reason that I missed? 回答1:

Bitwise right shift operator in Java

假如想象 提交于 2019-12-10 17:35:25
问题 In Java, -4 >> 2 gives -1 but -5 >> 2 gives -2. Can anybody explain why? Here is a sample code: byte r=-5; r>>=2; System.out.println(r); Also in this scenario >> and >>> operators give the same answer. Can anyone explain that as well? 回答1: You can take a look at the bits. Using two's complement notation, the bits for -4 and -5 are, showing only the last 8 bits for brevity: -4: 1111 1100 -5: 1111 1011 Bit shifting to the right 2 positions, with sign extension: -4 >> 2: 1111 1111 (-1) -5 >> 2:

C fastest way to compare two bitmaps

て烟熏妆下的殇ゞ 提交于 2019-12-10 17:29:51
问题 There are two arrays of bitmaps in the form of char arrays with millions of records. What could be fastest way to compare them using C. I can imagine to use bitwise operator xor 1 byte at a time in a for loop. Important point about bitmaps: 1% to 10% of times algorithm is run, bitmaps can differ. Most of the time they will be same. When hey can differ, they can as much as 100%. There is high probability of change of bits in continuous streak. Both bitmaps are of same length. Aim: Check do

How do I access the state of individual bits of a word in MIPS?

≯℡__Kan透↙ 提交于 2019-12-10 17:23:18
问题 I'm writing a program and I need to determine if bits 3 and 6 are set. I know that I can rotate a word or left/right shift it. But how do I access individual bit's state? Do I use a bitwise operator like and/xor? 回答1: You would do a bitwise and with 0x08 and 0x40 (presuming bit 0 is the lowest order bit). You would use the andi instruction to do this. If $t0 is the value you want to test: andi $t1, $t0, 0x08 andi $t2, $t0, 0x40 $t1 will be non-zero if bit 3 is set, $t2 will be non-zero if bit

How to interleave 2 booleans using bitwise operators?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:01:21
问题 Suppose I have two 4-bit values, ABCD and abcd . How to interleave it, so it becomes AaBbCcDd , using bitwise operators? Example in pseudo-C: nibble a = 0b1001; nibble b = 0b1100; char c = foo(a,b); print_bits(c); // output: 0b11010010 Note: 4 bits is just for illustration, I want to do this with two 32bit ints. 回答1: This is called the perfect shuffle operation, and it's discussed at length in the Bible Of Bit Bashing, Hacker's Delight by Henry Warren, section 7-2 "Shuffling Bits." Assuming x