bit-manipulation

Simulate a 128-bit unsigned integer in SQL and C# using a 64-bit signed value?

瘦欲@ 提交于 2019-12-08 06:42:47
问题 Take this scenario: You have a few flag enumerations in C# tied to (and in fact generated from) Enum-ish tables in SQL Server. Say you are a distributor, and you allow your resellers to specify what US states they ship to. Being a brilliant and elegant software engineer, you implemented these as a bitwise-combinable flag value to save storage: create table USState ( StateID bigint, StateAbbr char(2), StateName varchar(50)) /* insert all US States + DC into USState, StateIDs must be in powers

Converting to Binary using bitwise and bitshift

北城以北 提交于 2019-12-08 06:09:05
问题 I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code. void PrintInBinary( unsigned int decNum ) { int i = 0; unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1); for( i = 0; i < sizeof(int)*8; i++ ) { printf( "%u", decNum & (highestOne >> i) ); } printf("\n"); } int main() { unsigned int a = 128; PrintInBinary( a ); system("PAUSE"); return 0; } The following is the output:

Switching bits in each nibble of an int

扶醉桌前 提交于 2019-12-08 05:26:48
问题 How can I switch the 0th and 3rd bits of each nibble in an integer using only bit operations (no control structures)? What kind of masks do I need to create in order to solve this problem? Any help would be appreciated. For example, 8(1000) become 1(0001). /* * SwitchBits(0) = 0 * SwitchBits(8) = 1 * SwitchBits(0x812) = 0x182 * SwitchBits(0x12345678) = 0x82a4c6e1 * Legal Operations: ! ~ & ^ | + << >> */ int SwitchBits(int n) { } 回答1: Code: #include <stdio.h> #include <inttypes.h> static

A “dynamic bitfield” in C

隐身守侯 提交于 2019-12-08 05:23:01
问题 In this question, assume all integers are unsigned for simplicity. Suppose I would like to write 2 functions, pack and unpack, which let you pack integers of smaller width into, say, a 64-bit integer. However, the location and width of the integers is given at runtime, so I can't use C bitfields. Quickest is to explain with an example. For simplicity, I'll illustrate with 8-bit integers: * * bit # 8 7 6 5 4 3 2 1 myint 0 1 1 0 0 0 1 1 Suppose I want to "unpack" at location 5, an integer of

How to find TMax without using shifts

坚强是说给别人听的谎言 提交于 2019-12-08 04:43:02
问题 Using ONLY ! ~ & ^ | + How can I find out if a 32 bit number is TMax? TMax is the maximum, two's complement number. My thoughts so far have been: int isTMax(int x) { int y = 0; x = ~x; y = x + x; return !y; } That is just one of the many things I have unsuccessfully have tried but I just cant think of a property of TMax that would give me TMax back. Like adding tmax to itself would be unique compared to all the other integers. Here is the actual problem: /* * isTMax - return 1 if x is the

Binary searching via bitmasking?

自作多情 提交于 2019-12-08 03:17:27
问题 I have used this algorithm many times to binary search over Ints or Longs . Basically, I start from Long.MinValue and Long.MaxValue and decide to set the bit at i th position depending on the value of the function I am maximizing (or minimizing). In practice, this turns out to be faster (exactly 63*2 bitwise operations) and easier to code and avoids the many gotchas of traditional binary search implementations. Here is my algorithm in Scala: /** * @return Some(x) such that x is the largest

Tilde C unsigned vs signed integer

徘徊边缘 提交于 2019-12-08 01:54:33
问题 For example: unsigned int i = ~0; Result : Max number I can assign to i and signed int y = ~0; Result : -1 Why do I get -1 ? Shouldn't I get the maximum number that I can assign to y ? 回答1: Both 4294967295 (a.k.a. UINT_MAX ) and -1 have the same binary representation of 0xFFFFFFFF or 32 bits all set to 1 . This is because signed numbers are represented using two's complement. A negative number has its MSB (most significant bit) set to 1 and its value determined by flipping the rest of the

division and multiplication by power of 2

不问归期 提交于 2019-12-08 01:18:11
问题 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. 回答1: 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

HasFlag not recognizing role assignment

允我心安 提交于 2019-12-08 01:06:31
问题 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;

Looping through Bits C

只愿长相守 提交于 2019-12-07 22:44:25
问题 I'm trying to loop through the bits of an unsigned char, but I'm not sure where to start, eventually, I'll perform other bitwise operating on the bits, such as ~ and xor..etc. 回答1: Looping over bits can be done in several ways: You can do a destructive loop, when you shift the value, and test the initial or the final bit, depending on the order in which you would like to enumerate bits, or You can do a non-destructive loop, when you use bitwise AND to test the number with a single-bit mask ,