bitwise

Is a logical right shift by a power of 2 faster?

Hi there, I would like to know if performing a logical right shift is faster when shifting by a power of 2. I am using C++. For example, is myUnsigned >> 4 any faster than myUnsigned >> 3 I appreciate that everyone's first response will be to tell me that one shouldn't worry about tiny little things like this, it's using correct a...

Bitfields vs. Polymorphism for game map object attributes

This optimzation question has been bugging me for the last day. In my program (a simple roguelike game), I use bitwise flags to store the attributes of map objects, such as if they are solid, or if they are rendered. However, I could accomplish the thing using polymorphism to return the appropriate value. My question is, is either way...

Convert MBF Double to IEEE

Hi, I found a topic below for convert MBF to IEEE. http://stackoverflow.com/questions/2973913/convert-mbf-single-and-double-to-ieee Anyone can explain what are the function of the code marked below? Dim sign As Byte = mbf(6) And ToByte(&H80) 'What is the reason AND (&H80)? Dim exp As Int16 = mbf(7) - 128S - 1S + 1023S 'Why is 115...

Bitwise Manipulation or Bitwise Programming

I know the concepts of bit-wise operators, bit manipulation, 2's complement etc. But when it comes to solving something using bit manipulation it does not strike me. I takes me time to wrap my head around them. I thought it would help if I looked at some questions regarding bit operators/bit manipulation but it left me even more confus...

OpenGL stencil buffer OR operation?

I'm not sure if this is possible to do, but it's worth a shot. I'm using the stencil buffer to reduce overdraw for light volumes in a deferred renderer using this algorithm (when the camera is outside the volume): Using a cheap shader, draw back faces with depth testing set to LEQUAL, marking them in the stencil buffer. Using the expen...

Checking whether a number is positive or negative using bitwise operators

I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc. Can the same be done using bitwise operators and some trick in C or in C++? ...

JavaScript Bitwise operations

Hi, I would like to store all currently-pressed keycodes in a single variable by using bitwise operations when the key is pressed and when the key is released. I'm not sure how to properly use bitwise operations, but I know this will be very simple to someone who does. Once complete, it should be simple to see which key is currently d...

How to compare two numbers to see which is greater

Comparing two numbers x,y; if x>y I return 1, else return 0. I am trying to do this using only bitwise operators such as >> << ~ ! & | ^ +. So far I got this: int greaterThan(int x,int y) { return ((~(x-y))>>31); } So if x-y is negative, the most significant bit would be a 1. So I negate it to get a 0 and shift it so that it returns...

How to implement Bitcount using only Bitwise operators?

The task is to implement a bit count logic using only bitwise operators. I got it working fine, but am wondering if someone can suggest a more elegant approach. Only Bitwise ops are allowed. No "if", "for" etc int x = 4; printf("%d\n", x & 0x1); printf("%d\n", (x >> 1) & 0x1); printf("%d\n", (x >> 2) & 0x1); printf("%d\n", (x >> 3) &...

C# bitwise shift on ushort (UInt16)

I need to perform a bitwise left shift on a 16-bit integer (ushort / UInt16), but the bitwise operators in C# seem to apply to int (32-bit) only. How can I use << on an ushort, or at least get to the same result with a simple workaround? ...

Extract bit sequences of arbitrary length from byte[] array efficiently

I'm looking for the most efficient way of extracting (unsigned) bit sequences of arbitrary length (0 <= length <= 16) at arbitrary position. The skeleton class show how my current implementation essentially handles the problem: public abstract class BitArray { byte[] bytes = new byte[2048]; int bitGet; public BitArray() { } public vo...

Bitwise flags and Switch statement?

I have the following code (example), and I'm really not comfortable with so many 'if' checks: public enum Flags { p1 = 0x01, // 0001 p2 = 0x02, // 0010 p3 = 0x04, // 0100 p4 = 0x08 // 1000 }; public static void MyMethod (Flags flag) { if ((flag & Flags.p1) == Flags.p1) DoSomething(); if ((...

Does anyone know an efficient way to rotate a 32 * 32 bit matrix by 90 degrees in C#?

Possible Duplicate: How do you rotate a two dimensional array? I have 32 Int32's. Picture this as a block of 32 * 32 bits. I need to rotate this block of bits 90 degrees clockwise. What's the most efficient way to do this in C#? ...

How can I use linq to check if an flags/bitwise enumeration contains a type?

Hi folks, the following lambda statemement returns null, when i was hoping it would return a string value. var countryCode = AddressComponents .Where(x => x.AddressType == AddressType.Country) .Select(x => x.ShortName) .SingleOrDefault(); now the AddressType property of the current instance i'm interrigating contains the ...

How a processor can calculate if A < B

This is probably a dumb question came to my mind, but I think I don't have answer to this - How processor would find if A < B? In fact there are other things as well like A>B or A==B. But if we solve A I understand that different processors may probably have different implementations but I wanted to have some high level idea of doing ...

Bitshifting Java, please explain

Hi! I'm trying to understand how bit shift works. Can someone please explain the meaning of this line: while ((n&1)==0) n >>= 1; where n is an integer and give me an example of a n when the shift is executed. Thanks! ...

Represent BitWise AND,OR,SHIFT Operations in PseuodoCode

How Can I represent bitwise AND, OR Operations and Shift Operations using PsuedoCode? Please Hep me ...

Check if a number x is positive (x>0) by ONLY using bitwise operators in C.

isPositive - return 1 if x > 0, return 0 otherwise Example: isPositive(-1) = 0. Legal ops: ! ~ & ^ | + << >>(arithmetic shift) Max ops: 8 Note: No conditional statements are allowed. size of int is a word(4 bytes). It is a signed representation using two's compliment. int isPositive(int x) { return ???; } ...

bit swapping with char type in C

the data type is char, and the pattern is follow: source byte: [0][1][2][3][4][5][6][7] destination: [6][7][4][5][2][3][0][1] for example, if I pass a char, 29 to this function, it will do the swapping and return a char type value, which is 116. How can I do the swapping? thank you. ======================== Just wondering if I can...

bit shifting in C

int x = 2; x = rotateInt('L', x, 1); // should return 4 x = rotateInt('R', x, 3); // should return 64 Here is the code, can someone check it and let me know what the error is? Compilation is successful, but it says Segmentation Fault when I execute it. int rotateInt(char direction, unsigned int x, int y) { int i; for(i = 0; i ...