string ASMDecodeParser::convertBranchAddToHexAddress(string str) //This is to convert the branch target address encoding to its hex addresss { //flag used to make sure no pre 0's in the string. bool flag = false; //init decimal int dec = 0; //used for the four bits in the string. string fourBit = ""; //Using a stringstream object stringstream tmp; //adds two 0's in front of the encoded string and two 0's at end of the encoded string. tmp << "00" << str << "00"; //convert the sstring object to string then assign it into a string called binary. string binary = tmp.str(); //Creates another stringstream object for the hex and adds 0x stringstream ss; ss << "0x"; //goes by groups of 4 and converts each to its hex form. for(int i = 0; i <= binary.length()-4; i+=4) { //Gets the four bits in the string. fourBit= string(binary.begin()+i,binary.end()+(i-16)); //converts the fourBit string into its decimal form. dec = binaryToDecimal(fourBit); //If dec does not equal 0 flag is set true. //Used to eliminate extra 0's in front. if(dec != 0 && flag != true) flag = true; //used to eliminate pre 0's in address //adds the hex value into the stream. if(flag) ss << hex << dec; //otherwise continue iteration. else continue; } //converst the stringstream object to a string and assigns to the result then returns. string result = ss.str(); return result; }
void dfs(vector<int> &result, vector<int> &gray, int i) { if (i == gray.size()) { int dec = binaryToDecimal(gray); result.push_back(dec / 2 ^ dec); return; } gray[i] = 0; dfs(result, gray, i + 1); gray[i] = 1; dfs(result, gray, i + 1); }
void W_DM(char value) { int index = binaryToDecimal(dm.address, 1); if(index < 0 || index > DM_SIZE) return; //Desconsidera quando o valor na entrada do endereço é inválido //mandado as vezes quando não se deseja fazer uma operação nem de leitura nem escrita if(value == '1') { //Escrita //Copia para a posição (dm.address) do vetor 'dm.data', o valor da entrada 'dm.input' strcpy(dm.data[index], dm.input); strcpy(dm.output, ZERO); //zera a saída } else if(value == '0') { //Leitura //Copia para a saída 'dm.output' o valor presente no vetor 'dm.data', na posição (dm.address) strcpy(dm.output, dm.data[index]); } }
int main() { char binaryString[sizeofBinaryString]; int decimal; printf("enter binary number\n"); fgets(binaryString,sizeofBinaryString,stdin); binaryString[strlen(binaryString)-1]=0; if(!isBinaryString(binaryString)) { printf("please enter a number binary this just containts 0 and 1\ntry again\n"); return -1; } decimal=binaryToDecimal(binaryString); printf("%s (binary)=%d (decimal)\n",binaryString,decimal); return 0; }
/** * @brief Converter::fromBinaryString2DoubleString * @param pBinaryString * @return * Función para convertir desde string binario a string Double */ std::string Converter::fromBinaryString2DoubleString( std::string pBinaryString ) { std::string decimal; std::string entero; // conversion parte entera for(int i = (pBinaryString.length() - 8); i < pBinaryString.length() ; i++){ decimal += pBinaryString.at(i); } //cout << decimal << endl; //cout << binaryToDecimal( decimal ) << endl; for(int i = 0; i < (pBinaryString.length() - 8) ; i++){// conversion parte entera entero += pBinaryString.at(i); } //cout << entero << endl; //cout << binaryToDecimal( entero ) << endl; int decimalIntNumber = fromString2Int( binaryToDecimal( decimal ) ); int enteroIntNumber = fromString2Int( binaryToDecimal( entero ) ); double final = enteroIntNumber * ( pow( 10, (-decimalIntNumber) ) ); //cout << final << endl; //cout << fromDouble2String( final ) << endl; return fromDouble2String( final ); }
string ASMDecodeParser::convertJumpAddToHexAddress(string str) //Takes a jump address and converts it into its Hex Address. { //flag bool flag = false; //init dec value. int dec = 0; //used for holding the four bits in coverting to hex. string fourBit = ""; //adds two 0's to end of address string. string binary = str + "00"; //using stringstream object stringstream ss; ss << "0x"; //Goes through the 28bits in groups of 4 and converts to hex. for(int i = 0; i <= binary.length()-4; i+=4) { //Grabs a group of 4bits. fourBit= string(binary.begin()+i,binary.end()+(i-24)); //converts the four bits to binary. dec = binaryToDecimal(fourBit); //if the dec doesnt equal 0 make flag true. //Used to eliminate extra 0's in front. if(dec != 0 && flag != true) { flag = true; } //if flag is true. if(flag) //add hex into the stream. ss << hex << dec; //otherwise continue iteration. else continue; } //convert the stringstream into a string then assign it into the result and return. string result = ss.str(); return result; }
/** * @brief Converter::binaryToString * @param pBinaryString * Conversión de binario a String */ std::string Converter::binaryToString(const std::string &pBinaryString) { std::string temp, result; const char SIZEOFBINARY = 8; for (unsigned short counter = pBinaryString.length() / SIZEOFBINARY, pos = 0; counter > 0; counter--, pos += SIZEOFBINARY) { temp = pBinaryString.substr(pos, SIZEOFBINARY); std::string str(binaryToDecimal(temp)); QString asciiCharacter(str.c_str()); char binaryToStringChar = asciiCharacter.toInt(); result.append(numericCharToString(binaryToStringChar)); } //cout << "Conversión de " << pBinaryString << " a " << result << endl; return result; }
float divisaoBinaria(unsigned char *aop1, unsigned char *aop2) { return divisao(binaryToDecimal(aop1), binaryToDecimal(aop2)); }
unsigned char multiplicacaoBinaria(unsigned char *aop1, unsigned char *aop2) { return multiplicacao(binaryToDecimal(aop1), binaryToDecimal(aop2)); }
unsigned char subtracaoBinaria(unsigned char *aop1, unsigned char *aop2) { return subtracao(binaryToDecimal(aop1), binaryToDecimal(aop2)); }
int somaBinaria(unsigned char *aop1, unsigned char *aop2) { printf("\nBinatyToDecimal: %d", binaryToDecimal(aop1) ); printf("\nBinatyToDecimal: %d", binaryToDecimal(aop2) ); return soma(binaryToDecimal(aop1), binaryToDecimal(aop2)); }
void ASMDecodeParser::setInstructValues(string str, Instruction &i) //gets appropriate values for the instruction then calls instruction class set values. { //Inits all the values. Opcode myOpcode; Register myRS = -1; Register myRT = -1; Register myRD = -1; int myImmediate = -1; string myImmediateAddress = ""; //Gets the opcode value using the encoded string instruction. Opcode o = opcodes.getOpcode2(str); //Gets the instant type of the instruction. InstType type = opcodes.getInstType(o); //RTYPE instruction. if(type == RTYPE) { //Grabs rsField of encoded instruction. string rs = string(str.begin()+6, str.end()-21); //Grabs rtField of encoded instruction. string rt = string(str.begin()+11, str.end()-16); //Grabs rdField of encoded instruction. string rd = string(str.begin()+16, str.end()-11); //Grabs shamtField of encoded instruction. string shamt = string(str.begin()+21, str.end()-6); //Sets the opcode. myOpcode = o; //Sets the rs. myRS = binaryToDecimal(rs); //Sets the rt. myRT = binaryToDecimal(rt); //sets the rd. myRD = binaryToDecimal(rd); //sets the imm. myImmediate = twoCompBinaryToDecimal(shamt); //Calls the setValue method and inits all of the data fields in the instruction. i.setValues(myOpcode,myRS,myRT,myRD,myImmediate, myImmediateAddress); } //ITYPE instruction. else if(type == ITYPE) { //If the instruction has a label. if(opcodes.isIMMLabel(o)) { //Grab the immediate field of the instruction. string immediateField = string(str.begin()+16,str.end()); //Converts the encoded address into its hexadecimal form. string hex = convertBranchAddToHexAddress(immediateField); //sets immediateAdress myImmediateAddress = hex; } //Regular immediate. else { //Grabs immediateField of encoded string. string immediateField = string(str.begin()+16,str.end()); //Using two's complemant for negative or positive immediate values and sets it to imm field. myImmediate = twoCompBinaryToDecimal(immediateField); } //Grabs rsField of encoded string. string rs = string(str.begin()+6, str.end()-21); //Grabs rtField of encoded string. string rt = string(str.begin()+11,str.end()-16); //sets the opcode. myOpcode = o; //sets the rs. myRS = binaryToDecimal(rs); //sets the rt. myRT = binaryToDecimal(rt); //Calls the setValue method and inits all of the data fields in the instruction. i.setValues(myOpcode,myRS,myRT,myRD,myImmediate,myImmediateAddress); } //JTYPE instruction. else if(type == JTYPE) { //Sets the opcode. myOpcode = o; //Grabs the addressField of the encoded string. string addressField = string(str.begin()+6,str.end()); //Calls the jump address conversion method to convert the jump address to hex address. string hex = convertJumpAddToHexAddress(addressField); //sets the imm address. myImmediateAddress = hex; //Calls the setValue method and inits all of the data fields in the instruction. i.setValues(myOpcode,myRS,myRT,myRD,myImmediate,myImmediateAddress); } //UNDEFINED else { //Sets the opcode. myOpcode = o; //Calls the setValue method and inits all of the data fields in the instruction. i.setValues(myOpcode,myRS,myRT,myRD,myImmediate,myImmediateAddress); } }
//Update the observation we return to the agent. //Make binary array that represents a 16-bit value. void PacMan::updateObservation() { //bool binary[16]; binaryObservation[0] = false; binaryObservation[1] = false; binaryObservation[2] = false; binaryObservation[3] = false; binaryObservation[4] = false; binaryObservation[5] = false; binaryObservation[6] = false; binaryObservation[7] = false; binaryObservation[8] = false; binaryObservation[9] = false; binaryObservation[10] = false; binaryObservation[11] = false; binaryObservation[12] = false; binaryObservation[13] = false; binaryObservation[14] = false; binaryObservation[15] = false; //Get the surrounding spaces around pacman if (pacmanY - 1 < 0 || map[pacmanY - 1][pacmanX] == '*') { binaryObservation[0] = true; } if (pacmanX + 1 > 16 || map[pacmanY][pacmanX + 1] == '*') { binaryObservation[1] = true; } if (pacmanY + 1 > 18 || map[pacmanY + 1][pacmanX] == '*') { binaryObservation[2] = true; } if (pacmanX - 1 < 0 || map[pacmanY][pacmanX] == '*') { binaryObservation[3] = true; } //See if pacman can find a ghost or a pellet north int upY = pacmanY - 1; while (upY > 0 && map[upY][pacmanX] != '*') { if (map[upY][pacmanX] == 'A' || map[upY][pacmanX] == 'B' || map[upY][pacmanX] == 'C' || map[upY][pacmanX] == 'D') { binaryObservation[4] = true; } if (map[upY][pacmanX] == '.') { binaryObservation[11] = true; } upY--; } //See if pacman can find a ghost or a pellet east. int rightX = pacmanX + 1; while (rightX < 17 && map[pacmanY][rightX] != '*') { if (map[pacmanY][rightX] == 'A' || map[pacmanY][rightX] == 'B' || map[pacmanY][rightX] == 'C' || map[pacmanY][rightX] == 'D') { binaryObservation[5] = true; } if (map[pacmanY][rightX] == '.') { binaryObservation[12] = true; } rightX++; } //See if pacman can find a ghost or a pellet south. int downY = pacmanY + 1; while (downY < 19 && map[downY][pacmanX] != '*') { if (map[downY][pacmanX] == 'A' || map[downY][pacmanX] == 'B' || map[downY][pacmanX] == 'C' || map[downY][pacmanX] == 'D') { binaryObservation[6] = true; } if (map[downY][pacmanX] == '.') { binaryObservation[13] = true; } downY++; } //See if pacman can find a ghost or a pellet east. int leftX = pacmanX - 1; while (leftX > 0 && map[pacmanY][leftX] != '*') { if (map[pacmanY][leftX] == 'A' || map[pacmanY][leftX] == 'B' || map[pacmanY][leftX] == 'C' || map[pacmanY][leftX] == 'D') { binaryObservation[7] = true; } if (map[pacmanY][leftX] == '.') { binaryObservation[14] = true; } leftX--; } //Check if there's a pellet nearby for (int y = 0; y < 19; y++) { for (int x = 0; x < 17; x++) { int distance = manhattanDistance(pacmanX, pacmanY, x, y); if (!binaryObservation[8] && distance <= 2 && map[y][x] == '.') { binaryObservation[8] = true; } if (!binaryObservation[9] && distance <= 3 && map[y][x] == '.') { binaryObservation[9] = true; } if (!binaryObservation[10] && distance <= 4 && map[y][x] == '.') { binaryObservation[10] = true; } } } //See if pacman is under the effects of a power pellet binaryObservation[15] = poweredUp; m_observation = binaryToDecimal(binaryObservation); }
void huffman(FILE** file, FILE** ptFileOutput, char* fileInputName) { char c; int i = 0; int positionChar = 0; int tailleTab = 0; int tailleCode = 0; int currentChar = 0; float longueurTotaleCode = 0; char* tabChar; char* fileOutputName; int* intTab; char* charTab; char* bufferCode; char charTemp[7]; elementListe* elemL = NULL; elementListe** ptListe = &elemL; /* temps de traitement */ clock_t start_time, end_time; start_time = clock(); intTab = calloc(1, sizeof(int)); charTab = calloc(1, sizeof(char)); if ((intTab == NULL) || (charTab == NULL)) { printf("Erreur d'allocation intTab ou charTab.\n"); exit(-1); } /* parcours du fichier source et remplissage des tableaux de frequence*/ while ((c = fgetc(*file)) != EOF) { /*printf("%c", c);*/ if (isInTab(c, charTab) == -1) { intTab = realloc(intTab, sizeof(int) * tailleTab + 1); charTab = realloc(charTab, sizeof(char) * tailleTab + 1); if ((intTab == NULL) || (charTab == NULL)) { printf("Erreur REALLOC intTab ou charTab.\n"); exit(-1); } intTab[tailleTab] = 1; charTab[tailleTab] = c; tailleTab++; } else { positionChar = isInTab(c, charTab); intTab[positionChar]++; } } /* Affichage tableau */ /*for (i = 0; i < tailleTab; i++) { printf("1-%c %d\n", charTab[i], intTab[i]); }*/ tri(charTab, intTab, tailleTab); /* Affichage tableaux triés */ /*for (i = 0; i < tailleTab; i++) { printf("2-%c %d\n", charTab[i], intTab[i]); }*/ for (i = 0; i < tailleTab; i++) { createChainedList(ptListe, charTab[i], intTab[i]); } /* Affichage liste chainee triée*/ /*for (a = *ptListe;a->suivant!=NULL;a = a->suivant) { printf("3-%c %d\n", a->caractere, a->frequence); }*/ printf("\n"); while ((*ptListe)->suivant != NULL) { insertNewNodeInChainedList(ptListe); } /* La liste ne contient plus qu'un seul element qui contiend l'arbre entier */ tabChar = calloc(1, sizeof(char*)); if (tabChar == NULL) { printf("Erreur d'allocation tabChar.\n"); exit(-1); } /* Parcours d'arbre*/ prefixeHuffmanTree(elemL->noeudIntermediaire, tabChar, 0); /* affichage caractères codés */ for (i = 0; i < 256; i++) { if (code[i]) { printf("'%c': %s\n", i, code[i]); longueurTotaleCode += strlen(code[i]); } } printf("Longueur moyenne d'un symbole : %.2f bits\n", longueurTotaleCode / tailleTab); printf("Compression en cours, veuillez patienter . . .\n"); /*Ecriture de la taille de la table des fréquences */ fileOutputName = calloc((8 + strlen(fileInputName)), sizeof(char)); if (fileOutputName == NULL) { printf("Erreur d'allocation fileOutputName.\n"); exit(-1); } fileOutputName = createBinaryFile(fileInputName, ptFileOutput, ".huffman"); openFile(fileOutputName, ptFileOutput, "wb"); /*ECRITURE DANS LE FICHIER CIBLE*/ /*------------------------------------------------------------*/ /* on écrit la taille du dictionnaire */ fwrite(&tailleTab, sizeof(int), 1, *ptFileOutput); /* Ecriture du dictionnaire de donnees */ for (i = 0; i < tailleTab; i++) { fwrite(&charTab[i], sizeof(char), 1, *ptFileOutput); tailleFileOutput++; fwrite(&intTab[i], sizeof(int), 1, *ptFileOutput); tailleFileOutput += 4; } /*------------------------------------------------------------*/ bufferCode = calloc(1, sizeof(char)); if (bufferCode == NULL) { printf("Erreur d'allocation bufferCode.\n"); exit(-1); } /*pointeur sur le fichier initial pour un nouveau parcours (creation du code)*/ fseek(*file, 0, SEEK_SET); while ((c = fgetc(*file)) != EOF) { tailleFileInput++; for (i = 0; i < 256; i++) { if (c == (char) i) { tailleCode = strlen(code[i]) + strlen(bufferCode) + 1; bufferCode = realloc(bufferCode, tailleCode * sizeof(char)); if (bufferCode == NULL) { printf("Erreur reallocation bufferCode.\n"); } strcat(bufferCode, code[i]); } } } printf("\n"); /* Affichage du buffer */ /*printf("%s\n", bufferCode);*/ tailleCode = strlen(bufferCode); int nboctet = tailleCode / 7; tailleFileOutput += nboctet; for (i = 0; i < nboctet; i++) { strncpy(charTemp, bufferCode, 7); currentChar = binaryToDecimal(charTemp); fwrite(¤tChar, 1, 1, *ptFileOutput); bufferCode += 7; } i = nboctet * 7; /* si il reste des octets */ if (i != tailleCode) { tailleFileOutput++; strcpy(charTemp, bufferCode); if (strlen(charTemp) != 7) { for (i = 0; i < 7; i++) { if (charTemp[i] == '\0') { charTemp[i + 1] = '\0'; charTemp[i] = '0'; } } currentChar = binaryToDecimal(charTemp); fwrite(¤tChar, 1, 1, *ptFileOutput); } } closeFile(ptFileOutput); printf("Taux de compression : %.2llu %% \n", 100 -((tailleFileOutput) * 100 / tailleFileInput)); freeHuffmanTree(elemL->noeudIntermediaire); free(elemL); free(intTab); intTab = NULL; free(charTab); charTab = NULL; free(fileOutputName); fileOutputName = NULL; free(tabChar); tabChar = NULL; end_time = clock(); printf("Compression effectuee en %lu s.", (long) ((end_time - start_time) / CLOCKS_PER_SEC)); }