bit-manipulation

JavaScript bitwise undefined pitfalls?

回眸只為那壹抹淺笑 提交于 2019-12-05 11:23:34
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. The internal function [ToInt32] is called on all operands for all bitwise operators. Note that ToInt32

Why doesn't left bit shifting by 64 overflow in golang?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 10:54:57
I was taking a look at A Tour of Go and I was confused by something in their basic-types.go example: MaxInt uint64 = 1<<64 - 1 Shouldn't shifting a 1 64 positions to the left in an unsigned 64 bit integer cause overflow (a.k.a. shifting a bit past the MSB)? However, the compiler doesn't complain until the line is changed to: MaxInt uint64 = 1<<65 - 1 ./basic-types.go:5: constant 36893488147419103231 overflows uint64 If I write some code to iterate over left shifts of varying lengths, including shifting by 65 as in the above example that causes the compiler to barf, I see two things: It behaves

Why aren't bitwise operators as smart as logical “and\\or” operators

谁说我不能喝 提交于 2019-12-05 09:37:26
I just noticed that bitwise operations aren't as "smart" as logical "and\or" operations and I wonder why? Here's an example: // For the record private bool getTrue(){return true;} private bool getFalse(){return false;} // Since a is true it wont enter getFalse. bool a = getTrue() || getFalse(); // Since a is false it wont enter getTrue. bool b = getFalse() && getTrue(); // Since b is false it wont enter getTrue. b = b && getTrue(); However the bitwise operators "|=" and "&=" aren't as smart: bool a = getTrue(); a |= getFalse(); // a has no chance to get false but it still enters the function.

bit twiddling in java - decomposing long into long[] of bitmasks

女生的网名这么多〃 提交于 2019-12-05 08:25:25
I am decomposing a single long into a long[] of single bit longs by public static int decompose(long[] buffer, long base) { int count = Long.bitCount(base); for (int i=0; i<count; i++) { base ^= (buffer[i] = Long.lowestOneBit(base)); } return count; } but I feel like there might be a faster way to do this, since it seems like there are some repeated steps. For example, counting the bits should already come pretty close to getting all the info needed to populate the result. Any suggestions? I'm familiar with the premature optimization mantra, hence why I'm not pushing my solution any further on

ASCII compressor works for short test file, not on long

*爱你&永不变心* 提交于 2019-12-05 08:17:26
The current project in Systems Programming is to come up with an ASCII compressor that removes the top zero bit and writes the contents to the file. In order to facilitate decompression, the original file size is written to file, then the compressed char bytes. There are two files to run tests on- one that is 63 bytes long, and the other is 5344213 bytes. My code below works as expected for the first test file, as it writes 56 bytes of compressed text plus 4 bytes of file header. However, when I try it on the long test file, the compressed version is 3 bytes shorter than the original, when it

Are bitwise operations still practical?

人走茶凉 提交于 2019-12-05 07:16:03
Wikipedia, the one true source of knowledge, states: On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication). Is there a practical reason to learn bitwise operation hacks or it is now just something you learn for theory and curiosity? Bitwise operations are worth studying because they have many applications. It is

php array bitwise

醉酒当歌 提交于 2019-12-05 07:02:58
if I have an array of flags and I want to combine them with a bitwise conjunction ie: $foo = array(flag1, flag2); into $bar = flag1 | flag2; Does PHP have any good functions that will do this nicely for me already? The array_reduce will reduce an array to a single value for you: $res = array_reduce($array, function($a, $b) { return $a | $b; }, 0); Reduce is also sometimes called fold (fold left or fold right) in other languages. You could do it like so $bar = $foo[0] | $foo[1] If the size of your array is unknown you could use array_reduce like this // in php > 5.3 $values = array_reduce(

Bitwise operations in Postgres

拜拜、爱过 提交于 2019-12-05 07:01:00
I have the following tables: types | id | name ------+----+---------- 1 | A 2 | B 4 | C 8 | D 16| E 32| F and vendors | id | name | type --------+----+----------+----- 1 | Alex | 2 //type B only 2 | Bob | 5 //A,C 3 | Cheryl | 32 //F 4 | David | 43 //F,D,A,B 5 | Ed | 15 //A,B,C,D 6 | Felix | 8 //D 7 | Gopal | 4 //C 8 | Herry | 9 //A,D 9 | Iris | 7 //A,B,C 10| Jack | 23 //A,B,C,E I would like to query now: select id, name from vendors where type & 16 >0 //should return Jack as he is type E select id, name from vendors where type & 7 >0 //should return Ed, Iris, Jack select id, name from vendors

Storing binary string in MySQL

筅森魡賤 提交于 2019-12-05 06:45:00
I've developed a small binary flag system for our admin centre. It allows us to set items to have multiple options assigned to them, without having to store have a table with several fields. Once the options are converted into binary with bitwise operators, we'd end up with an option like 10000 or 10010 which is all good. Doing it this way allows us to keep adding options, but without having to re-write which value is which, 10010 & (1 << 4) and I know that we have something turned on. The problem however is storing this data in our MySQL table. I've tried several field types, but none of them

Concatenate two 32 bit int to get a 64 bit long in Python

耗尽温柔 提交于 2019-12-05 06:25:55
I want to generate 64 bits long int to serve as unique ID's for documents. One idea is to combine the user's ID, which is a 32 bit int, with the Unix timestamp, which is another 32 bits int, to form an unique 64 bits long integer. A scaled-down example would be: Combine two 4-bit numbers 0010 and 0101 to form the 8-bit number 00100101 . Does this scheme make sense? If it does, how do I do the "concatenation" of numbers in Python? sykora Left shift the first number by the number of bits in the second number, then add (or bitwise OR - replace + with | in the following examples) the second number