Last active
August 29, 2015 14:24
-
-
Save yenliangl/c8158d2c355defc5177f to your computer and use it in GitHub Desktop.
zigzag encoding (32bits)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <bitset> | |
#include <cstring> | |
#include <limits> | |
int main(int argc, char** argv) | |
{ | |
const int n = atoi(argv[1]); | |
std::cout << " n = " << std::bitset<32>(n) << ", INTMAX=" << std::numeric_limits<int>::max() << ", INTMIN=" << std::numeric_limits<int>::min() << std::endl; | |
unsigned int n1=n<<1; | |
std::cout << " n << 1 = " << std::bitset<32>(n1) << ", decimal=" << n1 << std::endl; | |
unsigned int n2=n>>31; | |
std::cout << " n >> 31 = " << std::bitset<32>(n2) << ", decimal=" << n2 << std::endl; | |
unsigned int n3=n1^n2; | |
std::cout << " (n<<1)^(n>>31) = " << std::bitset<32>(n3) << ", decimal=" << n3 << std::endl; | |
return 0; | |
} |
yenliangl@caterpillar: ~$ ./a.out 2147483647
n = 01111111111111111111111111111111, INTMAX=2147483647, INTMIN=-2147483648
n << 1 = 11111111111111111111111111111110, decimal=4294967294
n >> 31 = 00000000000000000000000000000000, decimal=0
(n<<1)^(n>>31) = 11111111111111111111111111111110, decimal=4294967294
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yenliangl@caterpillar: ~$ ./a.out -2147483648
n = 10000000000000000000000000000000, INTMAX=2147483647, INTMIN=-2147483648
n << 1 = 00000000000000000000000000000000, decimal=0
n >> 31 = 11111111111111111111111111111111, decimal=4294967295
(n<<1)^(n>>31) = 11111111111111111111111111111111, decimal=4294967295