Monday, March 14, 2011

Decimal to Binary manipulations

For my first object oriented programming module, (it's been awhile since I did this in Comp Science, Memories!)
We were told to write an algorithm that would change a decimal number to binary numbers.

This was further extended to printing only the non trivial (sum of all bits not equal to one) and constant auto-correlating bits for number 0 to 2^14.

The code for decimal to bin
#include <string>
#include<fstream>
using namespace std;

//takes an int and returns the binary representation as a string
string decToBin(unsigned int intToConvert, unsigned int size){

register unsigned int tempInt = intToConvert;
string binRep="";
unsigned int i = 0;
do{
i++;
binRep = char((tempInt & 1u)+48u) + binRep;
tempInt >>= 1;
}while(i<size);

return binRep;
}


void assignment1Question1(){
const unsigned int size = 15u;
unsigned int maxBinarySize = 1<<size;
ofstream out("binary15.txt");
for (unsigned int i=0; i<maxBinarySize; i++){
out<<decToBin(i,size)<<endl;
}
}

int main(){
assignment1Question1();
return 0;
}



The code for const auto correlation and non trival
#include <string>
#include<fstream>
using namespace std;

//takes an int and returns the binary representation as a string
string decToBin(unsigned int intToConvert, unsigned int size){

register unsigned int tempInt = intToConvert;
string binRep="";
unsigned int i = 0;
do{
i++;
binRep = char((tempInt & 1u)+48u) + binRep;
tempInt >>= 1;
}while(i<size);

return binRep;
}

//MIT HAKMEM count
int sumOfBits(unsigned int test){

register unsigned int tmp;
tmp = test - ((test >> 1) & 033333333333)
- ((test >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}

//checks if an integer is of constant auto correlation
bool isConstAutoCor(unsigned int test, unsigned int size){
unsigned int shifted = test;
unsigned int autoCorNum = 0;
bool firstPass = 1;
//shift the bits, size-1 times to check for autocorrelation
for(unsigned int i=1; i < size; i++){
//shift the bits to the right
shifted = (shifted >> 1) | ( (1u & shifted) << (size-1));
//AND the shifted value with the test value to find the autoCorrelation number
unsigned int bitCount = sumOfBits(test & shifted);
//on first pass, set the autocorrelation number
if(firstPass==1){
firstPass=0;
autoCorNum = bitCount;
}
//on subsequent passes, check if the new autoCorrelation number is the same as the AC numbers found so far
else if(bitCount != autoCorNum){
return 0;
}
}
//only true if all autocorrelation numbers are equal
return 1;
}

//checks if an int is trivial
bool isTrivial(unsigned int test, unsigned int size){
unsigned int sum = sumOfBits(test);
if((sum <= 1) || (sum >= size-1)){
return 1;
}
return 0;
}

void assignment1Question2(){
const unsigned int size = 15u;
unsigned int maxBinarySize = 1<<size;
ofstream out("binary15a.txt");
for (unsigned int i=0; i<maxBinarySize; i++){
//we are looking for non trivial AND constant auto correlated binaries
if(!isTrivial(i, size)&&isConstAutoCor(i,size)){
out<<decToBin(i,size)<<endl;
}
}
}

int main(){
assignment1Question2();
}

No comments:

Post a Comment