bit-manipulation

How do I access the state of individual bits of a word in MIPS?

I'm writing a program and I need to determine if bits 3 and 6 are set. I know that I can rotate a word or left/right shift it. But how do I access individual bit's state? Do I use a bitwise operator like and/xor? ...

How do I set or clear the first 3 bits using bitwise operations?

Lets say I have a number like 0x448. In binary this is 0100 0100 1000. How do I set the bits 1, 2 and 3 to either all 0's or all 1's using bit-wise operations? When I say the first three, I'm counting the rightmost bit as the zero bit. So, for example Bits as 1's: b12 b0 0100 0100 1110 ^^^ Bits as 0's: b...

QT QImage pixel manipulation problems

Hi. I'm currently in the process of writing a steganography application with QT. I am trying to hide my message bits in the least significant bit of the blue colour of the pixel From debugging I can tell that this section is working as it should. However after hiding my bits in the message I then save the image and then reopen it. Thi...

uint64 UTC time

I have a UTC date time without the formatting stored in a uint64, ie: 20090520145024798 I need to get the hours, minutes, seconds and milliseconds out of this time. I can do this very easily by converting it to a string and using substring. However, this code needs to be very fast so I would like to avoid string manipulations. Is there ...

Why does BinaryReader.ReadUInt32() reverse the bit pattern?

I am trying to read a binary file with the BinaryReader class, and I need to read it in as blocks of UInt32, and then do some bit shifting etc. afterwords. But, for some reason bit order is reversed when I use the ReadUInt32 method. If I for example have a file where the first four bytes looks like this in hex, 0x12345678, they end up ...

Insert One Bit into Byte Array

Hello, I'm trying to insert a single bit into an array of bytes, which would shift all the bits in the byte array to the left. Say I have a Java byte array as follows: byte[] byteArray = new byte[2]; byteArray[0] = 0x11 byteArray[1] = 0x00 In binary, this byte array represented as: 0001 0001 0000 0000 Now I want to insert a zero ...

How to Bitwise compare a String

I am working on a function that takes in a series of permission strings, less than 255 characters and assigns them to an entity. Each string assigned is unique, but there are so many that dropping them into an array, serializing them and pushing into a database and pulling them out later and de-serializing them or recalculating from a q...

Will bit-shift by zero bits work correctly?

Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int. I'm particularly concerned about call with bitCount equal to zero - will it work correctly then? Also is ther...

toggle two bits with a single operation in C?

Lets say I have a byte with 6 unknown values: ???1?0?? and I want to swap bits 2 and 4 (without changing any of the ? values): ???0?1?? But how would I do this in one operation in C? I'm performing this operation thousands of times per second on a microcontroller so performance is the top priority. EDIT It would be fine to "toggle...

Efficient way to find "matches" in a matrix in java

I have an existing (java) application that models an order book, as it stands each order is visible to every other. There is now a requirement to put (what is effectively) a per order ACL in place. An example to illustrate, lets say I have access groups [V-Z] and orders [A-F] A B C D E F V 1 0 0 1 1 0 W 0 1 1 0 0 1 X ...

Fast average without division

I have a binary search loop which gets hit many times in the execution path. A profiler shows that the division part of the search (finding the middle index given the high and low indices of the search range) is actually the most costly part of the search, by a factor of about 4. (I think) it is not critical for efficient binary search...

A quick cheatsheet on using a bit map to store multiple values

I always get confused when I am about to use a bit map to store multiple flags. For example, if there are 10 possible properties for an object (all Yes or No), I use an unsigned int and the first 10 bits (from LSB) based on the properties. Now how to set and unset a particular bit and also how to check if a bit is set or not? If I want...

How to compute a 3D Morton number (interleave the bits of 3 ints)

I'm looking for a fast way to compute a 3D Morton number. This site has a magic-number based trick for doing it for 2D Morton numbers, but it doesn't seem obvious how to extend it to 3D: http://www-graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN So basically I have 3 10-bit numbers that I want to interleave into a single 30 b...

Java's >> versus >>> Operator?

I'm without my Java reference book and I'm having a tough time finding an answer with Google. What is the difference between the ">>" and ">>>" operators in Java? int value = 0x0100; int result = (value >> 8); System.out.println("(value >> 8) = " + result); // Prints: "(value >> 8) = 1" result = (value >>> 8); System.out.println("(v...

Bitfield manipulation in C

The classic problem of testing and setting individual bits in an integer in C is perhaps one the most common intermediate-level programming skills. You set and test with simple bitmasks such as unsigned int mask = 1<<11; if (value & mask) {....} // Test for the bit value |= mask; // set the bit value &= ~mask; // clear the bit ...

Checking if number is even by looking at the last bit - are there any other "tricks" like this one?

Hello, Recently I discovered, that if I need to see if variable is even ( or odd ), I could just see if last bit of variable equals 0. This discovery, when implemented, replaced few modulo 2 calculations and thus, whole function ran faster. Are there any other "tricks" like this one, where working with bits could replace other calculat...

I need bit manipulation guide / reference material for c#

Possible Duplicate: Most common C# bitwise operations I am looking for bit manipulation reference material for c#. And, OR, XOR, left shift , right shift. setting a bit All operations with bit. ...

In Java, is it possible to clear a bit?

In Java, is it possible to clear a bit using bitwise operations? ...

getting the bottom 16 bits of a Java int as a signed 16-bit value

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer. public class SignExtend16 { public static int get16Bits(int x) { return (x & 0xffff) - ((x & 0x8000) << 1); } public static int get16Bits0(int x) { return (int)(short)(x); } public static...

Bit Reversal

Possible Duplicate: Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C Hi All, I had just written down a bit reversal function and its shown below.I feel its lengthy.So is there any faster and short method or any other alternative which you feel is much more faster and better. Assuming integer is 4 byte...