Bit hacks - Printable Version +- Forums - Open Redstone Engineers (https://forum.openredstone.org) +-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html) +--- Forum: General computing and engineering (https://forum.openredstone.org/forum-66.html) +--- Thread: Bit hacks (/thread-3840.html) |
Bit hacks - jxu - 06-23-2014 Determining if number is power of 2 (excluding 0) Code: f = (v & (v - 1)) == 0 Parity of a byte Code: bool parity = Modulus by 1 << s: Code: m = n & ((1<< s) - 1); RE: Bit hacks - David - 06-23-2014 I've never used Modulus before but shouldn't if (x % 2 == 0) work? EDIT: Sorry, I'm tired don't mind me being stupid and all. EDIT 2: Let me try that again. if ((x & x*-1) == x) Problem is, this isn't any better than your version or anything. But I just wanted to clean my name. RE: Bit hacks - redstonewarrior - 06-25-2014 Quote:b * 0x0101010101010101ULLYou lied to us. Some fun ones from an assignment a while back: Code: // Absolute value of an integer. Uses bitmasking fun! All of these functions are written using only assignment, bitwise operators, left/right shift, and addition. There is no control logic, function calls, etc. :3 RE: Bit hacks - jxu - 06-25-2014 Log base 2 of IEEE 32 bit float Code: const int r; Integer log base 10 of an integer This one is amazing Code: unsigned int v; |