bit-manipulation

How to define 2-bit numbers in C, if possible?

情到浓时终转凉″ 提交于 2019-12-06 08:36:51
问题 For my university process I'm simulating a process called random sequential adsorption. One of the things I have to do involves randomly depositing squares (which cannot overlap) onto a lattice until there is no more room left, repeating the process several times in order to find the average 'jamming' coverage %. Basically I'm performing operations on a large array of integers, of which 3 possible values exist: 0, 1 and 2. The sites marked with '0' are empty, the sites marked with '1' are

User role permissions for different modules using bitwise operators

送分小仙女□ 提交于 2019-12-06 08:27:51
问题 So I have an application that has several modules (think of modules as different pages), each module has a set of permissions; view, add, edit, delete I want each user role to have privileges for each module, for example Role A Permissions Module 1 -> view Module 2 -> add, edit Module 3 -> view, add, edit, delete etc. How can I design the database to support that and how would I go about implementing it using bitwise operators (or would there be a more efficient way for this particular case?)

division and multiplication by power of 2

99封情书 提交于 2019-12-06 08:04:46
I read in a paper that division and multiplication of a number by a power of 2 is a trivial process. I have searched a lot of internet for the explanation but doesn't get it. Can any one explain in easy words what does this actually meant to say. Vlad It is trivial from the bit operations perspective. Multiplying by 2 is equivalent to a shift left by 1 bit, division is a right shift. similarly it is the same trivial to multiply and divide by any power of 2. int a = 123; // or in binary format: a = 0b01111011; assert (a * 2) == (a << 1); // 2 = 2^1, (a << 1) = 11110110 assert (a / 2) == (a >> 1

Bitwise operations in Python

给你一囗甜甜゛ 提交于 2019-12-06 07:15:42
问题 I'm looking for recommendations on how to do bitwise math in python. The main problem I have is that python's bitwise operators have infinite precision, which means that -1 is really "111.......111". That's not what I want. I want to emulate real hardware which will have some fixed precision, say 32 bits. Here are some gotchas: 1) -n should return a 32 bit 2's complement number ( this is easily achieved by taking the lower 32 bits of the infinite precision -n ) 2) n >> 3, should be an

Finding all combinations of longs with certain bits set

无人久伴 提交于 2019-12-06 06:24:35
问题 This is such an obscure problem that I suspect I'll have to do it at a different level in my code...but hopefully the hive mind that is Stack Overflow can help... I have a long, which if expressed as a binary string will have exactly five bits set. For example, long l = 341; // as a bit string, "101010101" I'm seeking an array containing all ten possible longs which exactly three of those bits set. To continue the example, long[] results = { 101010000, 101000100, 101000001, 100010100,

Returning i-th combination of a bit array

喜欢而已 提交于 2019-12-06 06:11:19
问题 Given a bit array of fixed length and the number of 0s and 1s it contains, how can I arrange all possible combinations such that returning the i-th combinations takes the least possible time? It is not important the order in which they are returned. Here is an example: array length = 6 number of 0s = 4 number of 1s = 2 possible combinations (6! / 4! / 2!) 000011 000101 000110 001001 001010 001100 010001 010010 010100 011000 100001 100010 100100 101000 110000 problem 1st combination = 000011

HasFlag not recognizing role assignment

心已入冬 提交于 2019-12-06 05:10:57
I'm using an Enum decorated with [Flags] to control autoization within my MVC2 app. Below is my code examples: [Flags] public enum SecurityRoles { None = 0, Executive = 1, BackOffice = 2, AccountManager = 4, Consultant = 8, Administrator = 16 } [TestMethod] public void MultipleSelectionsTest() { var requiredRoles = SecurityRoles.Executive | SecurityRoles.BackOffice; var user1Roles = SecurityRoles.Executive | SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant; var user1HasAccess = user1Roles.HasFlag(requiredRoles); var user2Roles = SecurityRoles.Administrator |

Base-encoding efficiency using bases that are not powers of 2

℡╲_俬逩灬. 提交于 2019-12-06 04:25:41
Base64 encodes three 8-bit characters onto onto four (base-64) 6-bit "characters". Base64 is efficient because it uses a base (64) and exponent (4) that happens to perfectly match a base-10 exponent of 2 (24): 3x8=4x6=24 and 2 24 =64 4 =16777216. It appears that there are no base/exponent combinations that result in values that exactly match base-10 exponents of 2 (specifically 2 n for any 0< n <256), except for base32, base64 and base128 (along with base4, base8, base16, base256, base512, etc which are more difficult to make practical use of). See the last code block for the full list of

What does <<= operator mean in Java?

痞子三分冷 提交于 2019-12-06 04:01:55
问题 Can you please explain this code snippet from HashMap constructor specifically the line capacity <<= 1: // Find a power of 2 >= initialCapacity 198 int capacity = 1; 199 while (capacity < initialCapacity) 200 capacity <<= 1; 回答1: It is equivalent to capacity = capacity << 1; . That operation shifts capacity's bits one position to the left, which is equivalent to multiplying by 2. The specific code you posted finds the smallest power of 2 which is larger than initialCapacity . So if

How to do zero-fill right shift in Dart?

谁说我不能喝 提交于 2019-12-06 03:50:33
How do I do zero-fill right shift in Google Dart? Something like: foo >>> 2 for instance. Zero-fill right shift requires a specific integer size. Since integers in Dart are of arbitrary precision the '>>>' operator doesn't make sense there. The easiest way to emulate a zero-fill right shift is to bit-and the number first. Example: (foo & 0xFFFF) >> 2 // 16 bit zero-fill shift (foo & 0xFFFFFFFF) >> 2 // 32 bit shift. You could define a utility function to use: int zeroFillRightShift(int n, int amount) { return (n & 0xffffffff) >> amount; } That assumes you have 32-bit unsigned integers and that