bit-manipulation

Is there another way to test Enum bit fields?

When using Enums with bit fields: enum ReallyBigEnum { FirstChoice = 0x01, AnotherOption = 0x02 } ReallyBigEnum flag = ReallyBigEnum.FirstChoice | ReallyBigEnum.AnotherOption; the code used to test the bits is: if( (flag & ReallyBigEnum.AnotherOption) == ReallyBigEnum.AnotherOption ) { ... } which seems verbose and erro...

How can I check if a signed integer is positive?

Using bitwise operators and I suppose addition and subtraction, how can I check if a signed integer is positive (specifically, not negative and not zero)? I'm sure the answer to this is very simple, but it's just not coming to me. ...

Swapping pair of bits in a Byte

I have an arbitrary 8-bit binary number e.g., 11101101 I have to swap all the pair of bits like: Before swapping: 11-10-11-01 After swapping: 11-01-11-10 I was asked this in an interview ! ...

How can I take two bytes and combine them into a single UInt16 in C#?

I want to have a method with the body: public UInt16 ReadMemory16(Byte[] memory, UInt16 address) { // read two bytes at the predefined address } So, I want to get the value at memory[address] AND the next byte and combine them into a single UInt16. For the order of the bytes, the machine I'm implementing is little endian if tha...

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

How to determine the number of similar bits?

I need to compare two numbers and look for similarities in more significant bits. I'm trying to determine the number of least significant bits that differ. 10111000 10111011 184 and 187 require an offset of two, because only two least significant bits differ. 10111011 11111011 187 and 251 require an offset of seven, because the sev...

How to output the Binary value of a variable in C++

I've got a homework assignment in my C++ programming class to write a function that outputs the binary value of a variable's value. So for example, if I set a value of "a" to a char I should get the binary value of "a" output. My C++ professor isn't the greatest in the whole world and I'm having trouble getting my code to work using th...

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

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

How can I use bit shifting to replace integer division

I understand how to do it for powers of 2 so that's not my question. For example, if i want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that? So instead of (x * 20 / 19), i could do (x * 100 >> 11). Now this isnt right but it's close and i arrived at it using trial and error. how would ...

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

Bits into char array

Hello, I need to transfrom bits into char array or string, help to find best way to store bits and what shoud I do if for example I have 18 bits I will make 2 char and 2 bits? ...

Bit banging in ruby

I want to create a bit, that will contain security permissions for a given user. In c#, I would do this by creating an enumeration, and then I would do some bit banging on the binary value, by anding '&&' to see if it results in a TRUE value. How can I do this best in Ruby? ...

Bit Level Manipulation & Arithmetic Functions (In C)

Hey everybody. I am learning bit wise operation and arithmetic operations in C but I am a bit confused about some of it. I am working through some of the review questions in my book but the book only has answers for the odd number questions so I would like to pose some questions for you guys so I can tell if I am on the right track. Than...

Bit manipulations/operations calculator !

Hi ! I am getting into bit manipulation in C/C++ Is there a good calculator or program(executable or online), that makes it relatively convenient to study & test bit procedures ? I can do same work in Visual Studio or Eclipse, but relatively small program is easier and more convenient. Thanks ! ...

Check if a number is non zero using bitwise operators in C.

Check whether a number x is nonzero using the legal operators except !. Examples: isNonZero(3) = 1, isNonZero(0) = 0 Legal ops: ~ & ^ | + << >> Note : Only bitwise operators should be used. if, else, for, etc. cannot be used. Edit1 : No. of operators should not exceed 10. Edit2 : Consider size of int to be 4 bytes. int isNonZero(...

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 ???; } ...

Integer Byte Swapping in C++

I'm working on a homework assignment for my C++ class. The question I am working on reads as follows: Write a function that takes an unsigned short int (2 bytes) and swaps the bytes. For example, if the x = 258 ( 00000001 00000010 ) after the swap, x will be 513 ( 00000010 00000001 ). Here is my code so far: #include <iostream> u...

Why is 3<<1 == 6 in python?

Possible Duplicate: Absolute Beginner's Guide to Bit Shifting? anyone can explain me that operator << or >> ...

how can I remove a flag in C?

There is a variable that holds some flags and I want to remove one of them. But I don't know how to remove it. Here is how I set the flag. my.emask |= ENABLE_SHOOT; ...