bitwise-operators

Would you use num%2 or num&1 to check if a number is even?

Well, there are at least two low-level ways of determining whether a given number is even or not: 1. if (num%2 == 0) { /* even */ } 2. if ((num&1) == 0) { /* even */ } I consider the second option to be far more elegant and meaningful, and that's the one I usually use. But it is not only a matter of taste; The actual performance ma...

Good tutorials on Bitwise operations in Java

Can anyone please point me to good online tutorials on Bitwise Operations (preferably in Java)? Before anyone write LMGTFY, I've already Googled it but couldn't find one with good examples. Java's own tutorial is also not that extensive and doesn't clearly explain what is going under the hood. I'm a visual learner; so, if there are any r...

Bitwise-or seems to have an implicit cast to long. Can this be avoided?

I'm a Java programmer trying to migrate to C#, and this gotcha has me slightly stumped: int a = 1; a = 0x08000000 | a; a = 0x80000000 | a; The first line compiles just fine. The second does not. It seems to recognise that there is a constant with a sign bit, and for some reason it decides to cast the result to a long, resulting in th...

Real world use cases of bitwise operators

What are some real world use cases of the following bitwise operators? AND XOR NOT OR ...

C++ bitwise operators for std::string

Hi, my question is about how to use bitwise operators on C++ std::string... through overloading or as function does not matter Example for an working XOR/^ function for std::string: std::string XOR(std::string value, std::string key) { std::string retval(value); long unsigned int klen = key.length(); long unsigned int vlen = val...

Bit masking in Postgres

I have this query SELECT * FROM "functions" WHERE (models_mask & 1 > 0) and the I get the following error: PGError: ERROR: operator does not exist: character varying & integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. The models_mask is an integer in the databas...

Getting 32 bit words out of 64-bit values in C/C++ and not worrying about endianness...

It's my understanding that in C/C++ bitwise operators are supposed to be endian independent and behave the way you expect. I want to make sure that I'm truly getting the most significant and least significant words out of a 64-bit value and not worry about endianness of the machine. Here's an example: uint64_t temp; uint32_t msw, lsw;...

Binary representation of a number in C

I came across this code for the binary representation of a number. I want to know the need for using !! in the code. int main() { int n,i; unsigned flag = 1<<(sizeof(int) * 8 - 1); printf("Input the number\n"); scanf("%d",&n); for(i=0;i<sizeof(int)*8;i++) { printf("%d",!!(n & flag) ); ...

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

Efficiency of Bitwise XOR in c++ in comparison to more readable methods

I've recently been writing some code for a research project that I'm working on, where efficiency is very important. I've been considering scraping some of the regular methods I do things in and using bitwise XORs instead. What I'm wondering is if this will make if a difference (if I'm performing this operation say several million times)...

How to use an int as an array of ints/bools?

I noticed while making a program that a lot of my int type variables never went above ten. I figure that because an int is 2 bytes at the shortest (1 if you count char), so I should be able to store 4 unsigned ints with a max value of 15 in a short int, and I know I can access each one individually using >> and <<: short unsigned int S...

C#: Getting lowest valued key in a bitwise Enum

I have enum like this [Flags] public enum Key { None = 0, A = 1, B = 2, C = 4 } I have the following Key k1 = Key.A | Key.B | Key.C; I want to get the key in k1 that has the lowest value. How can I do that? Example: Key k1 = Key.A | Key.B | Key.C; // I want a Key k2 = Key.B | Key.C; // I want b Key k3 = Key.A | Key.C; //...

Algorithm for bitwise fiddling

If I have a 32-bit binary number and I want to replace the lower 16-bit of the binary number with a 16-bit number that I have and keep the upper 16-bit of that number to produce a new binary number.. how can I do this using simple bitwise operator? For example the 32-bit binary number is: 1010 0000 1011 1111 0100 1000 1010 1001 and ...

C/C++: Bitwise operators on dynamically allocated memory

In C/C++, is there an easy way to apply bitwise operators (specifically left/right shifts) to dynamically allocated memory? For example, let's say I did this: unsigned char * bytes=new unsigned char[3]; bytes[0]=1; bytes[1]=1; bytes[2]=1; I would like a way to do this: bytes>>=2; (then the 'bytes' would have the following values):...

I (think) I want to use a BItWise Operator to check useraccountcontrol property!

Hello, Here's some code: DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + lstUsers.SelectedItem.Text + "))"; SearchResult result = searcher.FindOne(); Within result.Properties["useraccountcontrol"] will be an item which will give me a value depen...

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

C#: Shift left assignment operator behavior

I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes left == right and no shift occurs current <<= (32 + left - right); //this works current <<= (32 - right); current <<= left; It appears for any value >= 32, only the value % 32 is shifted. Is there some "optimization" occurring in the ...

How to reverse bitwise AND (&) in C?

How to reverse bitwise AND (&) in C? For example I have an operation in C like this: ((unsigned int)ptr & 0xff000000)) The result is 0xbf000000. What I need at this moment is how to reverse the above, i.e. determine ptr by using the result from the operation and of course 0xff000000. Is there any simple way to implement this in C? ...

48-bit bitwise operations in Javascript?

I've been given the task of porting Java's Java.util.Random() to JavaScript, and I've run across a huge performance hit/inaccuracy using bitwise operators in Javascript on sufficiently large numbers. Some cursory research states that "bitwise operators in JavaScript are inherently slow," because internally it appears that JavaScript will...

how to do bitwise exclusive or of two strings in python?

i would like to do bitwise exclusive or of words in python but xor of strings are not allowed in python . so how to do it ? ...