Last active
August 29, 2015 14:16
-
-
Save panchr/79fe0e914ae9be7e0f9d to your computer and use it in GitHub Desktop.
Bitstring operations in Python and C
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
// Rushy Panchal | |
// bitstring.c | |
// Provides bitstring operations in C | |
#include <math.h> | |
unsigned int countBits(unsigned int n); | |
unsigned int not(unsigned int n); | |
unsigned int rcirc(unsigned int x, unsigned int n); | |
unsigned int lcirc(unsigned int x, unsigned int n); | |
int main(void) { | |
// Main process | |
return 0; | |
} | |
unsigned int countBits(unsigned int n) { | |
// Counts the number of bits in n | |
unsigned int count; | |
while (n > 0) { | |
count++; | |
n = n >> 1; | |
} | |
return count; | |
} | |
unsigned int not(unsigned int n) { | |
// Calculate the logical NOT of n | |
unsigned int x; | |
x = ~n & (unsigned int) (pow(2, countBits(n)) - 1); | |
return x; | |
} | |
unsigned int rcirc(unsigned int x, unsigned int n) { | |
// Calculate the RCIRC of a bitstring | |
unsigned int numBits = countBits(x); | |
n = n % numBits; | |
if (n == numBits) { | |
return x; | |
} | |
unsigned int toShift = numBits - n; | |
unsigned int right = x >> n; | |
unsigned int left = (x << toShift) ^ (right << numBits); | |
return left | right; | |
} | |
unsigned int lcirc(unsigned int x, unsigned int n) { | |
// Calculate the LCIRC of a bitstring | |
unsigned int numBits = countBits(x); | |
n = n % numBits; | |
if (n == numBits) { | |
return x; | |
} | |
unsigned int toShift = numBits - n; | |
unsigned int right = x >> toShift; | |
unsigned int left = (x << n) ^ (right << numBits); | |
return left | right; | |
} |
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
class Integer(int): | |
'''A wrapper around native ints that allows for various logical operators''' | |
def __invert__(self): | |
'''Invert the current value with the logical NOT operator''' | |
value = self.real | |
return ~value & (2**value.bit_length() - 1) | |
def rcirc(self, n): | |
'''RCIRC the bitstring n times''' | |
value = self.real | |
numBits = value.bit_length() | |
n = n % numBits # make sure n is within the range of (0, numBits) inclusive | |
if (n == numBits): # RCIRC(x, n) for n = (number of bits in x) is equal to x | |
return value | |
toShift = numBits - n | |
right = value >> n # remove the last n bits | |
left = (value << toShift) ^ (right << numBits) # get the first "toShift" bits | |
return left | right # combine the left and right halves | |
def lcirc(self, n): | |
'''LCIRC the bitstring n times''' | |
value = self.real | |
numBits = value.bit_length() | |
n = n % numBits # make sure n is within the range of (0, numBits) inclusive | |
if (n == numBits): # LCIRC(x, n) for n = (number of bits in x) is equal to x | |
return value | |
toShift = numBits - n | |
right = value >> toShift # remove the last "toShift" bits | |
left = (value << n) ^ (right << numBits) # get the first n bits | |
return left | right # combine the left and right halves |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment