Beruflich Dokumente
Kultur Dokumente
Home
Bitwise Programming
Open Questions
Boolean Logic
Tips and Hacks
Hacking (computing)
Comment Share 9
Notifications
Downvote
Learning to Program
Nalan
Add Question
Computer Science
Nalan KR
Computer Programming
Edit Topics
A SK T O A NSWER
Views
17,804
while(b^=a^=b^=a%=b);
return a;
Aliases
Followers
291
5 }
Written 16 Jun.
Upvote 60
Laukik Nirgudkar
43 upvotes by Vikrant Singh, Tapasweni Pathak, Himani Garg, (more)
Downvote Comment 1
Share
Example:
So if you & v-1 with v you get zero if there is only one standing bit (i.e. powers
of two). This does not work if v is zero, hence left side of && first checks if v is
zero.
How (2) works:
(v & -v) gets least significant standing bit of v.
Examples:
If v = 6
&
If v = 4
&
If there is only one standing bit (v & -v) is equal to v. As a side note, (v & v) trick to get least significant standing bit is also used in Binary Indexed Tree,
and it is a cool bit trick on its own.
Note: As Brad Bazemore mentions in the comment, if v is negative all that
this trick does is two's complement
Note 2: As Anders Kaseorg pointed out in the comment I initially
misinterpreted Brad Bazemore's comment and edited my answer stating that
replacing v with -v will solve the problem with v with negative value, but there
is no such thing as power of 2 that is negative, so I removed that part of my
edit. In summary, make sure that an integer is non-negative before using this
trick to find out whether it is a power of 2 or not
Updated 22 Dec, 2012.
Upvote 97
2 int sign;
3
4 // CHAR_BIT is the number of bits per byte (normally 8).
5 sign = -(v < 0); // if v < 0 then -1, else 0.
1 int x, y;
2
3 bool f = ((x ^ y) < 0); // true iff x and y have opposite signs
2 unsigned int r;
3 int r;
4
5 r = y ^ ((x ^ y) & -(x < y)); // min(x, y)
6
7 r = x ^ ((x ^ y) & -(x < y)); // max(x, y)
5
6 }
Computing parity.
1 unsigned int v;
3
4 while (v)
5 {
6
parity = !parity;
v = v & (v - 1);
8 }
Reverse bits.
... (more)
Upvote 31
Downvote Comments 2
Share
if (number&1)==1
then number is odd
else
number is even
This is logic is very famous and question based on this is frequently asked in
programming contest
P.S. as Raghavan Santhanam pointed out this logic wont work if the number is
float type
Written 25 Apr, 2013.
Upvote 11
Downvote Comment 1
Share
long i;
float x2, y;
5
6
x2 = number * 0.5F;
= number;
= * ( long * ) &y;
= 0x5f3759df - ( i >> 1 );
10
= * ( float * ) &i;
11
= y * ( threehalfs - ( x2 * y * y ) );
// 1st iteration
12 //
= y * ( threehalfs - ( x2 * y * y ) );
13
return y;
14 }
Downvote Comments 2
Share 1
int remaining=0;
remaining^=array[i];
cout <<
remaining
<<
endl;
Shivaraj Shetty
7 upvotes by Vishnu Jayvel, Abhishek Tapadar, Surya Kant Verma, (more)
I love following snippet to find the number which doesn't have a pair in a
array where all elements are repeated twice while one of them is only once.
Bit manipulation (xor) allows to find this at a complexity of O(n).
odd_one = 0;
for ( i = 0; i < n ; i++)
odd_one ^= array[i];
Written 7 Jun.
Upvote
Downvote Share
// do stuff with s
s |= lz;
s |= (lz / lo / 2) - 1;
10 }
... and much more. The explanation for the above code snippets are also
available in that article.
Updated 25 Oct, 2012.
Upvote 11
Not my own technique, but I always liked the in-place swap trick:
1 a ^= b;
2 b ^= a;
3 a ^= b;
This works because a XOR a is always 0, and a XOR b is the same as b XOR a.
So what happens is you get,
a := a XOR b
b := b XOR a, but a is now a XOR b, so b := a XOR b XOR b, which works out
to being a
Now the last step, remember that a is currently a XOR b. Further XORing that
with b, which is now a, will cancel out the original a and leave you with just b.
Voila! Swap with no additional memory needed
Edit: fixed to the correct source, since my original answer was actually me
being lazy and typing the shorthand. Thanks to Xuan Luo
Written 24 Jan, 2013.
Upvote 19
Nalan KR
Edit Bio Make Anonymous
Written 3 Aug.
Read In Feed
Read In Feed