Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Created May 26, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/75b9eef29d07446db0cbd77032db64c5 to your computer and use it in GitHub Desktop.
Save heatblazer/75b9eef29d07446db0cbd77032db64c5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
static int npow2(int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
static unsigned int flipb4(unsigned int x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}
static unsigned int lrot(unsigned int x, int n)
{
return (x << n) | (x >> -n);
}
static unsigned int rrot(unsigned int x, int n)
{
return (x << -n) | (x >> n);
}
union bitset
{
unsigned int val;
struct
{
unsigned char b0: 1;
unsigned char b1: 1;
unsigned char b2: 1;
unsigned char b3: 1;
unsigned char b4: 1;
unsigned char b5: 1;
unsigned char b6: 1;
unsigned char b7: 1;
} m_field[sizeof(int)];
unsigned char data[sizeof(int)];
};
template <typename T, typename N> struct is_same
{
static const bool value = false;
};
template <class A> struct is_same<A, A>
{
static const bool value = true;
};
template <class A, class B>
static inline bool IsSame()
{
return is_same<A, B>::value;
}
template <class A, class B>
static inline bool IsSame(const A& a, const B& b)
{
return is_same<A, B>::value;
}
class A
{
};
class B
{
};
int main()
{
A a;
B b;
printf("%d \r\n", IsSame<A, A>());
printf("%d \r\n", IsSame(a, a));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment