bit-manipulation

Generating a wanted number by bitwise OR

做~自己de王妃 提交于 2019-12-07 09:38:17
问题 Given N integer intervals [lo_i,hi_i]. From each interval chose a number such that bitwise OR of them become given number X.(It doesn't matter if the result has more 1 bits than X; i.e. if the generated number is Y, (X&Y)==X should hold) 回答1: I guess this problem is NP complete, though I haven't found an NP hard problem easily reducible to this. But for those sets that contain 2^(mostSignificantDigit) - 1, I would do as a heuristic: Firstly, try the number 1...1 (mostSignificantDigit-1 ones),

The structure of Deflate compressed block

不问归期 提交于 2019-12-07 09:35:26
问题 I have troubles with understanding Deflate algorithm (RFC 1951). TL; DR How to parse Deflate compressed block 4be4 0200 ? I created a file with a letter and newline a\n in it, and run gzip a.txt . Resultant file a.txt.gz : 1f8b 0808 fe8b eb55 0003 612e 7478 7400 4be4 0200 07a1 eadd 0200 0000 I understand that first line is header with additional information, and last line is CRC32 plus size of input (RFC 1951). These two gives no trouble to me. But how do I interpret the compressed block

Convert 4 bytes to an unsigned 32-bit integer and storing it in a long

孤人 提交于 2019-12-07 08:51:07
问题 I'm trying to read a binary file in Java. I need methods to read unsigned 8-bit values, unsigned 16-bit value and unsigned 32-bit values. What would be the best (fastest, nicest looking code) to do this? I've done this in c++ and did something like this: uint8_t *buffer; uint32_t value = buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24; But in Java this causes a problem if for example buffer[1] contains a value which has it sign bit set as the result of a left-shift is an int (?

Could someone explain to me what the following Java code is doing?

。_饼干妹妹 提交于 2019-12-07 08:28:40
问题 byte s[] = getByteArray() for(.....) Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6); I understand that you are trying to convert the byte into hex string. What I don't understand is how that is done. For instance if s[i] was 00000001 (decimal 1) than could you please explain: Why 0x000000ff & 00000001 ? Why not directly use 00000001? Why result from #1 | 0xffffff00? Finally why substring(6) is applied? Thanks. 回答1: It's basically because bytes are signed in Java. If you

Bitwise shift in C [duplicate]

こ雲淡風輕ζ 提交于 2019-12-07 08:12:39
问题 This question already has answers here : Weird behavior of right shift operator (1 >> 32) (7 answers) Closed 2 years ago . I got some C code that confuses me: int a = 1; int b = 32; printf("%d\n %d\n", a<<b, 1<<32); The output is 1 0 The code was run on Ubuntu 16.04 (Xenial Xerus), and I compiled it using gcc -m32 a.c with GCC version 5.4.0. I've read some posts that have explained why a<<b outputs 1, but I don't understand why 1<<32 results to 0. I mean, what is the difference between a<<b

Bitwise AND on signed chars

大城市里の小女人 提交于 2019-12-07 07:55:08
问题 I have a file that I've read into an array of data type signed char . I cannot change this fact. I would now like to do this: !((c[i] & 0xc0) & 0x80) where c[i] is one of the signed characters. Now, I know from section 6.5.10 of the C99 standard that "Each of the operands [of the bitwise AND] shall have integral type." And Section 6.5 of the C99 specification tells me: Some operators (the unary operator ~ , and the binary operators << , >> , & , ^ , and | , collectively described as bitwise

Generic bit string comparison against zero in Postgres

瘦欲@ 提交于 2019-12-07 06:49:18
问题 Is there a way to do a non-zero bit string test without hard-coding the bit string width of 0? For example, suppose I have two tables, Users and Features, each with masks, I want to test this: SELECT u.name FROM Users u, Features f WHERE u.mask & f.mask; matching implicit non-zero results. However, SQL requires an explicit boolean result for WHERE as opposed to an implicit cast, such as this: SELECT u.name FROM Users u, Features f WHERE (u.mask & f.mask) != 0::BIT(2048); I don't want to

JavaScript bitwise undefined pitfalls?

痴心易碎 提交于 2019-12-07 06:07:33
问题 What is the logic of bitwise operators on undefined??? var x; console.log(x); // undefined console.log(x^7); // 7 console.log(7^x); // 7 console.log(x|7); // 7 console.log(7|x); // 7 console.log(7&x); // 0 console.log(x&7); // 0 console.log(~x); // -1 console.log(x*2); // NaN console.log(x/2); // NaN console.log(x+2); // NaN console.log(x-2); // NaN I can see some sense in NaN. Because undefined -2 is really 'not a number'. But I do not follow any logic on bitwise operators and undefined. 回答1

Do bitwise operations distribute over addition?

半世苍凉 提交于 2019-12-07 06:02:58
问题 I'm looking at an algorithm I'm trying to optimize, and it's basically a lot of bit twiddling, followed by some additions in a tight feedback. If I could use carry-save addition for the adders, it would really help me speed things up, but I'm not sure if I can distribute the operations over the addition. Specifically if I represent: a = sa+ca (state + carry) b = sb+cb can I represent (a >>> r) in terms of s and c? How about a | b and a & b? 回答1: Think about it... sa = 1 ca = 1 sb = 1 cb = 1 a

Declaring masks for bitwise operations

北城余情 提交于 2019-12-07 05:45:49
问题 I'm new to low level operations like this, I'm hoping someone can point out the obvious mistake I must be making here. //Input value - 00111100 //I want to get the value of the bits at indexes 1-3 i.e 0111. byte mask = (byte)0x00001111; // This gives 17 not the 15 I'd expect byte shifted = (byte)(headerByte >> 3); //shifted is 7 as expected byte frameSizeValue = (byte)(shifted & mask); //Gives 1 not 7 It looks like the problem lies with the way the mask is defined, but I can't see how to fix