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...
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...
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</url>
</resource>
</matrix>
I am parsing this with jQuery and am cur...
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)
...
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...
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...
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...
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'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...
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?
...
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
...
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?
...
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...
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 ...
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 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 ...
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...
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...
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?
...
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...