bitwise

Help translating Reflector deconstruction into compilable code

So I am Reflector-ing some framework 2.0 code and end up with the following deconstruction fixed (void* voidRef3 = ((void*) &_someMember)) { ... } This won't compile due to 'The right hand side of a fixed statement assignment may not be a cast expression' I understand that Reflector can only approximate and generally I can see a clea...

Any significant performance improvement by using bitwise operators instead of plain int sums in C#?

Hello, I started working with C# a few weeks ago and I'm now in a situation where I need to build up a "bit set" flag to handle different cases in an algorithm. I have thus two options: enum RelativePositioning { LEFT = 0, RIGHT = 1, BOTTOM = 2, TOP = 3, FRONT = 4, BACK = 5 } ...

C# .NET: if ((e.State & ListViewItemStates.Selected) != 0) <- What does it mean ??

In standard MSN code, there's a line on a ListView - Ownerdraw - DrawItem : if ((e.State & ListViewItemStates.Selected) != 0) { //Draw the selected background } Apparently it does a bitwise comparison for the state ?? Why bitwise ? The following doesn't work: if (e.State == ListViewItemStates.Selected) { //Doesn't work ?? } ...

Trying to figure out how to check a checksum

I'm trying to figure out how to check a checksum. My message looks like this: 38 0A 01 12 78 96 FE 00 F0 FB D0 FE F6 F6 being the checksum. I convert the preceding 12 sets in to binary and then add them together. Then attempt a bitwise operation to apply the 2s complement. I get a value of -1562, but I can't convert it back to hex to ...

Using a bitwise AND on more than two bits.

Hi, I am pretty new to bitwise operators. Let's say I have 3 variables a, b and c, with these values in binary: a = 0001 b = 0011 c = 1011 Now, I want to perform a bitwise AND like this: a AND b AND c -------- d = 0001 d &= a &= b &= c doesn't work (as I expected), but how can I do this? Thanks ...

Get length of bits used in int

If you have the binary number 10110 how can I get it to return 11111? e.g a new binary number that sets all bits to 1 after the first 1, there are some likewise examples listed below: 101 should return 111 (3 bit length) 011 should return 11 (2 bit length) 11100 should be return 11111 (5 bit length) 101010101 should return 111111111 (9 ...

User role permissions for different modules using bitwise operators

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. H...

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

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 ...

Count bits used in int

If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below: 101 should return 3 000000011 should return 2 11100 should return 5 101010101 should return 9 How can this be obtained the easiest way in Java? I have come up with the follow...

Bitwise OR of constants

While reading some documentation here, I came across this: unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *comps = [gregorian components:unitFlags fromDate:date]; I have no idea how this works. I read up on the bitwise operators in C, but I do not understand how you can fit three (...

Bitwise operations in BC?

$ bc BC> ibase=2 BC> 110&101 // wanna get 100 (standar_in) 8: syntax error Wikipedia informs that the ops are "|, & and ^". It may be that they work only in certain BC-types or I misread something. ...

C Population Count of unsigned 64-bit integer with a maximum value of 15

I use a population count (hamming weight) function intensively in a windows c application and have to optimize it as much as possible in order to boost performance. More than half the cases where I use the function I only need to know the value to a maximum of 15. The software will run on a wide range of processors, both old and new. I a...

How can I get the bitmask value back out of an enum with the flag attribute in C#?

In our database we have a bitmask that represents what types of actions a user can make. In our C# client when we retrieve this integer value from the database we construct an enum/flag. It looks somewhat like the following: [Flags] public enum SellPermissions { Undefined = 0, Buy = 1, Sell = 2, SellOpen = 4, SellC...

Usage of Bitwise OR in the following DOS command

...

Macros to set and clear bits

Im trying to write a few simple macros to simplify the task of setting and clearing bits which should be a simple task however I cant seem to get them to work correctly. #define SET_BIT(p,n) ((p) |= (1 << (n))) #define CLR_BIT(p,n) ((p) &= (~(1) << (n))) ...

What is the trick in pAddress & ~(PAGE_SIZE - 1) to get the page's base address

Following function is used to get the page's base address of an address which is inside this page: void* GetPageAddress(void* pAddress) { return (void*)((ULONG_PTR)pAddress & ~(PAGE_SIZE - 1)); } But I couldn't quite get it, what is the trick it plays here? Conclusion: Personally, I think Amardeep's explanation plus Alex B's exam...

Using bit manipulation to tell if an unsigned integer can be expressed in the form 2^n-1

To test if an unsigned integer is of the form 2^n-1 we use: x&(x+1) What is that supposed to equal? That is, x&(x+1) == ? ...

Does Xcode's objective-c compiler optimize for bit shifts?

Does the objective-c compiler in Xcode know better, or is it faster if I use bit shift for multiplications and divisions by powers of 2? NSInteger parentIndex = index >> 1; // integer division by 2 ...

Java: How do I create a Method to extract an Integer's split bits from a byte array using a mask

While working on decoding some video streaming standards I have noticed a lot of instances where the bits of an Integer value are provided in anything from 2-6 bytes but separated by reserved bits, as follows: // Specification (16 bits) // ----------------------- // Reserved 1 bit // Value A [6-7] 2 bit // Reserved ...

Is there anything that prevents this form of use of XOR test?

Is there anything that prevents this form of use of XOR test? bool result = false; bool b1 = false; bool b2 = false; ... if ( b1 ^ b2 ) { result = true; } ...