[algogeeks] Re: reverse all the bits in a unsigned integer
On Mar 21, 7:40 am, hijkl [EMAIL PROTECTED] wrote: . How to reverse all the bits in a unsigned integer? an signed integer (you have to keep the sign of the integer)? For an unsigned int, the following algorithm can be used (I believe you can find in in the Hacker's Delight book by H.Warren) The idea is to to swap bits with increasing distance each time: uint bit_reverse (uint x) { x = ((x 0x) 1) | ((x 0x) 1); x = ((x 0x) 2) | ((x 0x) 2); x = ((x 0x0f0f0f0f) 4) | ((x 0xf0f0f0f0) 4); x = ((x 0x00ff00ff) 8) | ((x 0xff00ff00) 8); x = ((x 0x) 16) | ((x 0x) 16); return x; } The function doesn't have if's and should work pretty fast. For 64- bit numbers the solution is similar (masks are of double width and yet another operation, shifting by 32 bits before return. Regards, Igor --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] A hanoi based problem
hello! It's a problem with all specifications of hanoi( 3 towers and and ordered rings) except that rings are not all in the same tower. They are distributed in 3 towers ( in each tower they are ordered ) and we want to bring them all in tower C ( assuming towers A,B,C). We want the solution with minimum time. I thank if you can answer it! (perhaps with a pseudocode) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: count the 1 bits in integer
how abt my code #includestdio.h int count1bitsn(int n) { int count=0; while(n) { count+=n1; n=n1; } } int main() { int n; printf(Enter the value of n\n); scanf(%d,n); printf(Answer=%d\n,count1bitsofn(n)); } On Mar 22, 12:39 am, Karthik Singaram L [EMAIL PROTECTED] wrote: The standard linkhttp://graphics.stanford.edu/~seander/bithacks.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: count the 1 bits in integer
how abt my code #includestdio.h int count1bitsn(int n) { int count=0; while(n) { count+=n1; n=n1; } return count; } int main() { int n; printf(Enter the value of n\n); scanf(%d,n); printf(Answer=%d\n,count1bitsofn(n)); } On Mar 22, 12:39 am, Karthik Singaram L [EMAIL PROTECTED] wrote: The standard linkhttp://graphics.stanford.edu/~seander/bithacks.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: 2D arrays
char a [n][n]; int i, j, m; m = n/2; if ( n % 2 == 0) m--; for(i = 0; i = m; i++) { for(j = 0; j = i; j++) { a[i][j] = '*'; a[n - i - 1][j] = '*'; } for(j = i + 1; j n; j++) { a[i][j] = '-'; a[n - i - 1][j] = '-'; } } 2007/3/18, BiGYaN [EMAIL PROTECTED]: Please don't post homework questions here. This problem is too simple for a group calling themselves Algorithm Geeks. I hope most of you'll agree to this and put a stop to this practice. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: traverse the tree layer by layer.
u can use a queen in bfs traverse void visit(Node* r) { } void BF_Traverse(Node* r) { queenNode* q; q.push(r); while(!q.empty()) { Node* t = *q.front(); q.pop(); visit(t); if(t-left !=0) q.push(t-left); if(t-right!=0) q.push(t-right); } } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---