bitwise

Prevent misuse of logical operator instead of bitwise operators

In C++ it's possible to use a logical operator where a biwise operator was intended: int unmasked = getUnmasked(); //some wide value int masked = unmasked & 0xFF; // izolate lowest 8 bits the second statement could be easily mistyped: int masked = unmasked && 0xFF; //&& used instead of & This will cause incorrect behaviour - masked...

bitwise operator variations in C++

I read in a book that C++ provides additional operators to the usual &,|, and ! which are "and","or" and "not" respectively, plus they come with automatic short circuiting properties where applicable. I would like to use these operators in my code but for some reason the compiler interprets them as identifiers whenever i use them and thr...

Understanding bitwise operations in javascript

I am currently storing data inside an XML doc as binary, 20 digits long, each representing a boolean value. <matrix> <resource type="single"> <map>10001010100011110000</map> <name>Resource Title</name> <url>http://www.yoursite.com&lt;/url&gt; </resource> </matrix> I am parsing this with jQuery and am cur...

Split a non-power-of-two based int

I know that you can split a power-of-two number in half like so: halfintR = some32bitint & 0xFFFF halfintL = some32bitint >> 16 can you do the same thing for an integer which is bounded by a non-power of two space? (say that you want your range to be limited to the set of integers that will fit into 4 digit base 52 space unsigned) ...

Calculating Hamming weight efficiently in matlab

Given a MATLAB uint32 to be interpreted as a bit string, what is an efficient and concise way of counting how many nonzero bits are in the string? I have a working, naive approach which loops over the bits, but that's too slow for my needs. (A C++ implementation using std::bitset count() runs almost instantly). I've found a pretty n...

bitwise operators and "Endianness"

Does Endianness matter at all with the bitwise operations. Either logical or shifting? I'm working on homework with regard to bitwise operators and I can not make heads or tails on it and I think I'm getting quite hung up on the endianess. That is, I'm using a little endian machine (like most are) but does this need to be considered or...

What is “2's Complement”?

I'm in a computer systems course and have been struggling, in part, with Two's Complement. I want to understand it but everything I read hasn't brought the picture together for me. I've read the wikipeida article and various other articles including my text book. Hence, I wanted to start this community wiki post to define what Two's Com...

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

How does this bitwise operation check for a power of 2?

I'm looking at some code which should be trivial -- but my math is failing me miserably here. Here's a condition that checks if a number if a power of 2 using the following: if((num != 1) && (num & (num - 1))) { /* make num pow of 2 */ } My question is, how does using a bitwise AND between num and num - 1 determine if a number is a p...

AND with full bits?

I have been reading over some code lately and came across some lines such as: somevar &= 0xFFFFFFFF; What is the point of anding something that has all bits turned on; doesn't it just equal somevar in the end? ...

Even and odd number

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo? >>> def isodd(num): return num & 1 and True or False >>> isodd(10) False >>> isodd(9) True ...

Opposite of Bitwise OR operation.

I compute c = a 'OR' b // bitwise OR operation here Now given only values of c and b how can I compute the original value of a? ...

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

XSLT Bitwise Logic

I have an existing data set that utilizes an integer to store multiple values; the legacy front end did a simple bitwise check (e.g. in C#: iValues & 16 == 16) to see if a particular value was set. Is it possible to do bitwise operations in XSL, and more explicitly, to do bit level comparisons via masking? The built-in "and" will always ...

Understanding javascript bitwise NOT

Thanks to everyone in advance - alert((~1).toString(2)); outputs: -10 but in PHP/Java it outputs 11111111111111111111111111111110 Am I missing something, why does Javascript add a "-" to the output? Thx, Sam ...

How to add two numbers without using ++ or + or another arithmetic operator.

How to add two numbers without using ++ or + or any other arithmetic operator. It was a question asked a long time ago in some campus-interview. Anyways today someone asked a question regarding some bit-manipulations, and in answers a beautiful quide *stanford Bit Twiddling * was referred. I spend some time studying it, and thought that ...

Fast Bitwise Question in C++

Hello all :) I'm looking for a fast way to setup a method of returning a bitmask based on a number. Basically, 4 one bits need to be emitted pre number input. Here's a good idea of what I mean: foo(1); // returns 0x000F foo(2); // returns 0x00FF foo(3); // returns 0x0FFF foo(4); // returns 0xFFFF I could just use a big switch statemen...

Fastest/easiest way to average ARGB color ints?

I have five colors stored in the format #AARRGGBB as unsigned ints, and I need to take the average of all five. Obviously I can't simply divide each int by five and just add them, and the only way I thought of so far is to bitmask them, do each channel separately, and then OR them together again. Is there a clever or concise way of av...

Bitwise operations

Hello A) (Int32)X | ((Int32)Y << 16); B) (Int32)X + (Int32)Y * (Int32)Int16.MaxValue; Shouldn't both be equivalent? I know from testing that the first works as expected, but for some reason the second doesn't. Both X and Y are shorts (Int16), and the return type is an integer (Int32). Shouldn't Y << 16 <=> Y * Int16.MaxValue? ...

How to compute one's complement using Ruby's bitwise operators ?

What I want: assert_equal 6, ones_complement(9) # 1001 => 0110 assert_equal 0, ones_complement(15) # 1111 => 0000 assert_equal 2, ones_complement(1) # 01 => 10 the size of the input isn't fixed as in 4 bits or 8 bits. rather its a binary stream. What I see: v = "1001".to_i(2) => 9 There's a bit flipping opera...