bit-manipulation

Most efficient way to find the index of the only '1' bit in a char variable (in C)

亡梦爱人 提交于 2019-12-07 05:13:13
问题 This is an interview question: You are given a char variable named ch , when you know that it represents a number that in its binary form, only one of its eight bits will be equal to '1'. I.E. , the only possible values for ch are: 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 . Given the variable ch , I need to write the most efficient code to get the index of that '1' bit. For example: if ch == 0x1 -> result is 0. if ch == 0x4 -> result is 2. The obvious way is to use switch-case, but I need

Why is -1 right shift 1 = -1 in Java?

你说的曾经没有我的故事 提交于 2019-12-07 04:42:38
问题 I came across the question "Why is -1 zero fill right shift 1=2147483647 for integers in Java?" I understood the concept of zero fill right shift perfectly well from the above question's answer. But when I tried to find -1>>1, I am getting a totally complex answer which I felt difficult to understand. -1 in binary form is as follows: 11111111111111111111111111111111 After flipping the bits, I got: 00000000000000000000000000000000 Upon adding 1 to it, I got: 00000000000000000000000000000001

Fastest way to read Left-Most bit from unsigned int (C++)?

大憨熊 提交于 2019-12-07 04:10:01
问题 What is the fastest way to read the Left-Most bit from unsigned int ? 回答1: i >> (sizeof(unsigned int) * CHAR_BIT - 1) The sizeof , multiplication, and subtraction will be computed at compile-time by any reasonable compiler, so this should become a single right-shift instruction, which is about as fast as you will get. 回答2: Might be faster than shifting at run-time, if AND is faster than shifting: i & (1 << (sizeof(unsigned int) * CHAR_BIT - 1)) 回答3: On a 32-bit system using any compiler that

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

谁说胖子不能爱 提交于 2019-12-07 04:06:02
问题 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,

ASCII compressor works for short test file, not on long

不羁的心 提交于 2019-12-07 03:40:08
问题 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,

Bitwise operations in Postgres

萝らか妹 提交于 2019-12-07 03:08:09
问题 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

How does the `test` instruction work?

人走茶凉 提交于 2019-12-07 03:01:20
问题 If we have: test dword ptr [eax], 2000h je label1: Is there any value other than 0 in dword ptr [eax] that would make the jump take place? 回答1: Instruction test works like and instruction, the only difference is that result is not stored back in to the destination operand. So the answer is yes. All binary numbers which not have set the 13th bit on memory address [eax], or all numbers present like b'xxxxxxxx xxxxxxxx xx0xxxxx xxxxxxxx', where x is 0 or 1, there is exactly 2^31 numbers. 来源:

Storing binary string in MySQL

时间秒杀一切 提交于 2019-12-07 02:56:25
问题 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

Is it possible to do bitwise operations on a string in Python?

旧时模样 提交于 2019-12-07 02:51:45
问题 This fails, not surprisingly: >>> 'abc' << 8 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for <<: 'str' and 'int' >>> With ascii abc being equal to 011000010110001001100011 or 6382179 , is there a way to shift it some arbitrary amount so 'abc' << 8 would be 01100001011000100110001100000000 ? What about other bitwise operations? 'abc' & 63 = 100011 etc? 回答1: What you probably want is the bitstring module (see http://code.google

C++: Emulated Fixed Point Division/Multiplication

白昼怎懂夜的黑 提交于 2019-12-07 02:46:24
I'm writing a Fixedpoint class, but have ran into bit of a snag... The multiplication, division portions, I am not sure how to emulate. I took a very rough stab at the division operator but I am sure it's wrong. Here's what it looks like so far: class Fixed { Fixed(short int _value, short int _part) : value(long(_value + (_part >> 8))), part(long(_part & 0x0000FFFF)) {}; ... inline Fixed operator -() const // example of some of the bitwise it's doing { return Fixed(-value - 1, (~part)&0x0000FFFF); }; ... inline Fixed operator / (const Fixed & arg) const // example of how I'm probably doing it