bitwise

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest way to find X given Y = 2**X in Python?" I am currently trying to optimize a routine (Rab...

Package for fast determination of similarity between two bit sequences

I need to compare a query bit sequence with a database of up to a million bit sequences. All bit sequences are 100 bits long. I need the lookup to be as fast as possible. Are there any packages out there for fast determination of the similarity between two bit sequences? --Edit-- The bit sequences are position sensitive. I have seen a p...

How to find N's complement of a number?

Hi all, I want to find 9's complement of number but failed. I tried it with the methods of 1's and 2's complements but no effect. What is common method to find out the N's complement of a number? ...

PHP Left Shift giving two answers on two different machines

Hello, I'm very confused about behaviour of PHP's left shift function. I'm using it on two different machines (dev and hosting), and they are giving me different answers. I've tracked it down to this calculation: (-3941404251) << 5; On one machine I'm getting the answer -1570884448; on the other, I get 0. On both systems, PHP_INT_MAX ...

addition without arithmetic and bitwise operators

how to add 2 numbers without using + and bitwise operators? ...

XOR reversibility operation question

Hi, I have heard somewhere that using XOR is not reversible (they spoke about encryption) but I do not understand how it was meant? AFAIK even with OR operation you cannot find out which of the two bits was 1. Please, could anyone who knows how it was meant explain it to me? Thank you ...

Wrapping 4 integers in a 64 bit long - java bitwise

Alright, so I have 4 integers I want to wrap in a long. The 4 integers all contains 3 values, positioned in the first 2 bytes: +--------+--------+ |xxpppppp|hdcsrrrr| +--------+--------+ {pppppp} represents one value, {hdcs} represents the second and {rrrr} the last. I want to pack 4 of these integers, in a long. I've tried the f...

SQL Server - Finding the Highest Priority item using Bitwise operators

The Problem: I want to return all of the rows from the Primary Data table together with the Highest Priority Exception based on the currently assigned Priority in an Exception table. I have created a simplified example of my data set-up below (with creation scripts) so hopefully you can help with what should be a fairly quick T-SQL pr...

how to set a large constant with bitwise operation on ULong?

This work nicely Public Const test As ULong = 1 << 30 This doesn't work nicely Public Const test As ULong = 1 << 31 It create this error: Constant expression not representable in type 'ULong' Anyone know how to make it work? This does work: Public Const test As Long = 1 << 31 But I have to use ULong ...

Getting the number of trailing 1 bits

Are there any efficient bitwise operations I can do to get the number of set bits that an integer ends with? For example 1110 = 10112 would be two trailing 1 bits. 810 = 10002 would be 0 trailing 1 bits. Is there a better algorithm for this than a linear search? I'm implementing a randomized skip list and using random numbers to determi...

C#: How to bit shift hexadecimal digits

Okay, I am working on a card playing program, and I am storing card values as hexadecimal digits. Here is the array: public int[] originalCards = new int[54] { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0...

C# :: Checking if a bit is set or not

Shhow me a very simple one line example in C# to check if a given bit is set in given byte function signature should be like bool IsBitSet(Byte b,byte nPos) { return .....; } ...

how to calculate bitwise OR using AND, XOR and shift?

Hi The question seems pretty well formulated I have a virtual machine which implements only AND, XOR, SHL and SHR, yet I have to do a "OR 0x01" operation. ...

C++ 64bit issue

I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new unsigned char [dem_content_buff_size]; int tmp_data; unsigned long long tmp_64_data; if (!(strcmp(dems[i].GetValType(), "s32"))) { dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tm...

PHP bitwise left shifting 32 spaces problem and bad results with large numbers arithmetic operations

Hello, I have the following problems: First: I am trying to do a 32-spaces bitwise left shift on a large number, and for some reason the number is always returned as-is. For example: echo(516103988<<32); // echoes 516103988 Because shifting the bits to the left one space is the equivalent of multiplying by 2, i tried multiplying the...

How to define 2-bit numbers in C, if possible?

For my university process I'm simulating a process called random sequential adsorption. One of the things I have to do involves randomly depositing squares (which cannot overlap) onto a lattice until there is no more room left, repeating the process several times in order to find the average 'jamming' coverage %. Basically I'm performi...

Bit Reversal using bitwise

Hi all I am trying to do bit reversal in a byte. I use the code below static int BitReversal(int n) { int u0 = 0x55555555; // 01010101010101010101010101010101 int u1 = 0x33333333; // 00110011001100110011001100110011 int u2 = 0x0F0F0F0F; // 00001111000011110000111100001111 int u3 = 0x00FF00FF; // 00000000111111110000000...

How is joystick axis information formatted from a USB Joystick?

I actually just have a rather small question, but I have had the HARDEST time finding information about it. For the application I am programming for, there will be a 3-axis joystick being connected via USB to a Windows XP computer, and it is being handled by directx. That information will then be sent elsewhere to an embedded controlle...

How to define and work with an array of bits in C?

I want to create a very large array on which I write '0's and '1's. I'm trying to simulate a physical process called random sequential adsorption, where units of length 2, dimers, are deposited onto an n-dimensional lattice at a random location, without overlapping each other. The process stops when there is no more room left on the latt...

What are the best methods to "Clear the 6th bit" of an integer?

What are the best methods to "Clear the 6th bit" of an integer? And, is your solution platform independent? (32 or 64 bit integer, etc). If not, can you give a solution that is platform independent? Update: we don't know whether that bit is set or unset when it was given... also, any language is ok... i know of a solution that is p...