bit-manipulation

Lua: Working with Bit32 Library to Change States of I/O's

有些话、适合烂在心里 提交于 2020-01-03 02:51:11
问题 I am trying to understand exactly how programming in Lua can change the state of I/O's with a Modbus I/O module. I have read the modbus protocol and understand the registers, coils, and how a read/write string should look. But right now, I am trying to grasp how I can manipulate the read/write bit(s) and how functions can perform these actions. I know I may be very vague right now, but hopefully the following functions, along with some questions throughout them, will help me better convey

Tesseral arithmetic/quadtree

99封情书 提交于 2020-01-02 17:41:31
问题 I did a project a while back on path finding with quadtrees and I would like to improve on its performance. It seems that using tesseral arithmetic to determine node adjacency (as per this page, courtesy of the Geography department of the University of British Columbia) would be much faster than the brute force method I'm using at the moment (I'm checking for shared edges, which works fine for a static quadtree but would be too much overhead if the map were changing). I more or less

Package for fast determination of similarity between two bit sequences

穿精又带淫゛_ 提交于 2020-01-02 15:03:04
问题 I need to compare a query bit sequence with a database of up to a million bit sequences. All bit sequences are 100 bits long. I need the lookup to be as fast as possible. Are there any packages out there for fast determination of the similarity between two bit sequences? --Edit-- The bit sequences are position sensitive. I have seen a possible algorithm on Bit Twiddling Hacks but if there is a ready made package that would be better. 回答1: If the database is rather static, you may want to

Bitwise operation on a floating point usefulness

我与影子孤独终老i 提交于 2020-01-02 06:33:12
问题 I noticed a SSE instruction existed for floating point AND which got me wondering. You can do the same thing with scalars in fp/integer union. The idea struck me that, if you bitwise OR the components of an array of floats, you can determine if any of them are negative quickly by looking at the sign bit of the result. What other uses exist for bitwise operations on floating point values? 回答1: A lot. For example when you only need to do bitwise operations on a floating-point only instruction

'memcpy'-like function that supports offsets by individual bits?

独自空忆成欢 提交于 2020-01-02 05:28:13
问题 I was thinking about solving this, but it's looking to be quite a task. If I take this one by myself, I'll likely write it several different ways and pick the best, so I thought I'd ask this question to see if there's a good library that solves this already or if anyone has thoughts/advice. void OffsetMemCpy(u8* pDest, u8* pSrc, u8 srcBitOffset, size size) { // Or something along these lines. srcBitOffset is 0-7, so the pSrc buffer // needs to be up to one byte longer than it would need to be

Iterating bits of a char

爷,独闯天下 提交于 2020-01-02 03:25:46
问题 Assuming I have char "C" whose ascii code is 0110 0111 . How can I iterate over its bits? I would like to build a vector from these 1's and 0's.... 回答1: You can easily iterate over them using bitwise operators: char c = 'C'; for (int i = 0; i < 8; ++i) { // extract the i-th bit int b = ((c & 1<<i) >> i); // b will be 1 if i-th bit is set, 0 otherwise // do whatever you want with b } you can optimize it (as suggested in comments): int b = ((c >> i) & 1); 回答2: A character has an integer value.

Is there a bit-wise trick for checking the divisibility of a number by 2 or 3?

六眼飞鱼酱① 提交于 2020-01-02 02:31:20
问题 I am looking for a bit-wise test equivalent to (num%2) == 0 || (num%3) == 0 . I can replace num%2 with num&1 , but I'm still stuck with num%3 and with the logical-or. This expression is also equivalent to (num%2)*(num%3) == 0 , but I'm not sure how that helps. 回答1: Yes, though it's not very pretty, you can do something analogous to the old "sum all the decimal digits until you have only one left" trick to test if a number is divisible by 9, except in binary and with divisibility by 3. You can

Bitwise Operations on short

让人想犯罪 __ 提交于 2020-01-02 02:00:33
问题 I am using a technology called DDS and in the IDL, it does not support int . So, I figured I would just use short . I don't need that many bits. However, when I do this: short bit = 0; System.out.println(bit); bit = bit | 0x00000001; System.out.println(bit); bit = bit & ~0x00000001; bit = bit | 0x00000002; System.out.println(bit); It says "Type mismatch: Cannot convert from int to short". When I change short to long , it works fine. Is it possible to perform bitwise operations like this on a

Bitwise AND in Sql Server

戏子无情 提交于 2020-01-02 01:57:14
问题 I have a very typical situation. We have a table called Users which has a column called Branches (varchar 1000). The organization can have 1000 branches. So if a user has access to branch 1, 5, and 10, the branches string would look like: 1000100001000000000...... (i.e. 1 for a position a User has branch access to based on the branch's number). Please do not advise better data storage options, this is coming to me from a legacy application that is deployed across continents. Now given this

NASM: Count how many bits in a 32 Bit number are set to 1

浪尽此生 提交于 2020-01-01 12:36:08
问题 I have a 32 Bit number and want to count know how many bits are 1. I'm thinking of this pseudocode: mov eax, [number] while(eax != 0) { div eax, 2 if(edx == 1) { ecx++; } shr eax, 1 } Is there a more efficient way? I'm using NASM on a x86 processor. (I'm just beginning with assembler, so please do not tell me to use code from extern libraries, because I do not even know how to include them ;) ) (I just found How to count the number of set bits in a 32-bit integer? which also contains my