void DES::createKeys() { //cout << "In createKeys" << endl; if(m_key.size() < 10) { cout << "createKeys: key too small. Exiting." << endl; exit(0); } //cout << "Key:\t" << this->m_key << endl; string temp = ""; //apply P10 for(int k = 0; k < 10; k++) { temp += this->m_key[this->tableP10[k] - 1]; } //cout << "P10:\t" << temp << endl; //apply LS-1 function temp = leftShift(temp); //cout << "shift:\t" << temp << endl; if(temp.size() < 8) { cout << "createKeys: temp too small. Exiting." << endl; exit(0); } this->m_k1 = ""; //apply P8, finding K1 for(int k = 0; k < 8; k++) { this->m_k1 += temp[this->tableP8[k] - 1]; } //cout << "P8(k1):\t" << this->m_k1 << endl; //cout << "preshift:\t" << temp << endl; //apply two more left shifts temp = leftShift(temp); temp = leftShift(temp); //cout << "2shifts:\t" << temp << endl; this->m_k2 = ""; //apply P8, finding K2 for(int k = 0; k < 8; k++) { this->m_k2 += temp[this->tableP8[k] - 1]; } //cout << "P8(k2):\t" << this->m_k2 << endl; //cout << "Out createKeys" << endl; }
void createSubkeys(bool binaryKey[8*8], bool subkeys[16][48]){ int pc_1[56] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; bool keyPermutation[56]; // according to pc_1 create from 64-bit key 56-bit keyPermutation for(int i = 0; i < 56; i++){ keyPermutation[i] = binaryKey[pc_1[i]-1]; } // C and D will be saved here: [C/D] [index] [28 bools] bool CD[2][16+1][56/2]; // divide keyPermutation into halves to C0 a D0 - each consists of 28 bits divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]); // from C0, D0 and shifts make C1, D1 -> C16, D16 int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; for(int i = 1; i < 17; i++){ leftShift(CD[0][i-1], shifts[i-1], CD[0][i]); leftShift(CD[1][i-1], shifts[i-1], CD[1][i]); } // each subKey out of 16 is made from one out of 16 CD with the use of pc_2 int pc_2[48] = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; for(int i = 0; i < 16; i++){ for(int j = 0; j < 48; j++){ // find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1 int where = pc_2[j] - 1; bool CorD = 0; if(where >= 56/2){ CorD = 1; where-=56/2; // subtract 28, to start indexing from 0 again in case of D } subkeys[i][j] = CD[CorD][i+1][where]; } } }
void multiply_bignum(bignum *a, bignum *b, bignum *c) { bignum aCopy = *a; bignum tmp; int i, j; int digit; // TODO: compare a and b for the optimized performance. if (compareBignum(a, b) < 0) { multiply_bignum(b, a, c); return; } initializeBignum(c); rightShift(&aCopy, b->numFractions); // Multiple by the fractional numbers. for (i = 0; i < b->numFractions; i++) { digit = b->integers[i]; // Add {digit} times. for (j = 1; j <= digit; j++) { addBignum(c, &aCopy, &tmp); *c = tmp; } leftShift(&aCopy, 1); } // Multiple by the integer numbers. for (i = 0; i <= b->numIntegers; i++) { digit = b->integers[i]; for(j = 1; j <= digit; j++) { addBignum(c, &aCopy, &tmp); *c = tmp; } leftShift(&aCopy, 1); } c->sign = (a->sign == b->sign) ? PLUS : MINUS; optimizeBignum(c); }
int main() { int len = length("Paritosh"); printf("%d\n", len); len = lengthPtr("Paritosh"); printf("%d\n", len); char s[123] = "Paritosh"; cat(s, "Kulkarni"); printf("%s\n", s); char s1[123] = "Paritosh"; ncpy(s1, "Kulkarni",3); printf("%s\n", s1); int ret = cmp(s,s1); printf("%d\n", ret); char* p = "Paritosh"; //rev1(p); //rightShift(p, 3); leftShift(p, 3); printf("%s", p); return 0; }
//left shift base Decimal BigInt& BigInt::selfLeftShift(size_t n) { BigInt bi = leftShift(n); *this = bi; return *this; }
/** enqueue a set of variants */ void VariantAlleleNormalizer::add(Variants const & vs) { bool all_homref = true; for(Call const & c : vs.calls) { if(!(c.isHomref() || c.isNocall())) { all_homref = false; break; } } for(auto const & x : vs.ambiguous_alleles) { if(!x.empty()) { all_homref = false; break; } } if (all_homref && !_impl->homref) { #ifdef DEBUG_VARIANTNORMALIZER std::cerr << "Skipping homref variant: " << vs << "\n"; #endif return; } // don't touch import fails if (vs.getInfoFlag("IMPORT_FAIL")) { _impl->buffered_variants.push(vs); return; } Variants nv(vs); size_t tmp = 0; _impl->current_maxpos.resize(std::max(_impl->current_maxpos.size(), nv.calls.size()), tmp); #ifdef DEBUG_VARIANTNORMALIZER std::cerr << "before: " << nv << "\n"; #endif if(_impl->ref_fasta) { int64_t new_start = -1; int64_t new_end = -1; int64_t leftshift_limit = -1; if(_impl->limit >= 0) { leftshift_limit = nv.pos - _impl->limit; } for (size_t rvc = 0; rvc < nv.variation.size(); ++rvc) { int64_t this_leftshift_limit = leftshift_limit; if(nv.chr == _impl->maxpos_chr) { for(size_t j = 0; j < nv.calls.size(); ++j) { for(size_t c = 0; c < nv.calls[j].ngt; ++c) { if(nv.calls[j].gt[c] - 1 == (int)rvc) { this_leftshift_limit = std::max(this_leftshift_limit, _impl->current_maxpos[j]); } } } } #ifdef DEBUG_VARIANTNORMALIZER std::cerr << "leftshift limit for " << nv.variation[rvc] << " is " << this_leftshift_limit << "\n"; #endif leftShift(*(_impl->ref_fasta), nv.chr.c_str(), nv.variation[rvc], this_leftshift_limit); trimLeft(*(_impl->ref_fasta), nv.chr.c_str(), nv.variation[rvc], _impl->refpadding); trimRight(*(_impl->ref_fasta), nv.chr.c_str(), nv.variation[rvc], _impl->refpadding); if(new_start < 0 || new_start > nv.variation[rvc].start) { new_start = nv.variation[rvc].start; } if(new_end < 0 || new_end > nv.variation[rvc].end) { new_end = nv.variation[rvc].end; } } if (new_start > 0 && new_end > 0) { nv.pos = new_start; nv.len = new_end - new_start + 1; // handle insertions if (nv.len == 0) { --nv.pos; nv.len = 1; } } } #ifdef DEBUG_VARIANTNORMALIZER std::cerr << "after: " << nv << "\n"; #endif if(_impl->maxpos_chr != nv.chr) { for(size_t j = 0; j < nv.calls.size(); ++j) { if(!nv.calls[j].isNocall() && !nv.calls[j].isHomref()) { _impl->current_maxpos[j] = nv.pos + nv.len - 1; } } } else { for(size_t j = 0; j < nv.calls.size(); ++j) { if(!nv.calls[j].isNocall() && !nv.calls[j].isHomref()) { _impl->current_maxpos[j] = std::max(nv.pos + nv.len - 1, _impl->current_maxpos[j]); } } } _impl->maxpos_chr = nv.chr; #ifdef DEBUG_VARIANTNORMALIZER std::cerr << "new max-shifting pos on " << _impl->maxpos_chr << " : "; for(size_t s = 0; s < _impl->current_maxpos.size(); ++s) { std::cerr << " s" << s << ": " << _impl->current_maxpos[s] << " "; } std::cerr << "\n"; #endif _impl->buffered_variants.push(nv); }
void input (void) { int c; u64 x; u64 new_block = 0; NewNumber = TRUE; Radix = HEX; output(); for (;;) { if (new_block != BlockNum) { if (readDisk(new_block) != 0) { new_block = BlockNum; } } output(); c = getch(); switch (c) { /* * Don't use these: CNTRL(C), CNTRL(Z), CNTRL(S), * CNTLR(Q), CNTRL(Y) */ case '?': help(); break; case '_': lastx(); neg(); break; case '+': lastx(); add(); break; case '-': lastx(); sub(); break; case '*': lastx(); mul(); break; case '/': lastx(); divide(); break; case '%': lastx(); mod(); break; case '~': lastx(); not(); break; case '&': lastx(); and(); break; case '|': lastx(); or(); break; case '^': lastx(); xor(); break; case '<': lastx(); leftShift(); break; case '>': lastx(); rightShift(); break; case '.': lastx(); swap(); break; case '!': lastx(); store(); break; case '=': lastx(); retrieve(); break; case '#': lastx(); qrand(); break; case KEY_RESIZE: /* Screen resize event - don't do anything */ break; case KEY_LEFT: case 'h': Current->left(); /* left one column */ break; case KEY_DOWN: case 'j': Current->down(); /* down one row */ break; case KEY_UP: case 'k': Current->up(); /* up one row */ break; case KEY_RIGHT: case 'l': Current->right(); /* right one column */ break; case CNTRL('A'):clear(); /* switch to next format */ next_format(); break; case CNTRL('W'):clear(); /* switch to previous format */ prev_format(); break; case CNTRL('B'):/* Backward one block */ if (BlockNum > 0) { new_block = BlockNum - 1; } break; case EOF: /* Ignore EOF - send on resize */ break; case 'q': case CNTRL('D'):finish(0); /* Exit */ break; case CNTRL('F'):/* Forward a block */ new_block = BlockNum + 1; break; case 'g': /* Go to specified Block */ lastx(); x = pop(); NewNumber = FALSE; new_block = x; break; case KEY_BACKSPACE: case KEY_DC: case CNTRL('H'):/* Delete last digit entered */ push(pop() / RadixTable[Radix]); break; case CNTRL('I'):/* Next field */ Current->next(); break; case KEY_CLEAR: case CNTRL('L'):/* Refresh the display */ wrefresh(curscr); break; case KEY_ENTER: case CNTRL('M'):/* Finish entry of number or duplicate top */ if (!NewNumber) { duptop(); } NewNumber = FALSE; break; case CNTRL('P'):/* Push copy of current field */ Current->copy(); NewNumber = FALSE; break; case CNTRL('R'):/* Rotate from bottom of stack to top */ rotate(); break; case 'r': /* Change radix */ ++Radix; if (Radix > UNSIGNED) Radix = HEX; break; case CNTRL('T'):/* Reinitialize stack */ initStack(); NewNumber = TRUE; break; case CNTRL('U'):/* Push last x */ push(Lastx); NewNumber = FALSE; break; case CNTRL('X'):/* Delete top of stack */ (void)pop(); push(0); NewNumber = TRUE; case 'Z': /* Previous field */ Current->prev(); break; default: /* Is it a digit? */ if ((Radix == HEX) && isxdigit(c)) { if (isdigit(c)) { digit(c, '0'); } else if (isupper(c)) { digit(c, 'A' - 10); } else { digit(c, 'a' - 10); } } else if ((Radix == UNSIGNED) && isdigit(c)) { digit(c, '0'); } else if ((Radix == SIGNED) && isdigit(c)) { digit(c, '0'); } else if (Radix == CHARACTER) { digit(c, 0); } else { flash(); sprintf(Error, "bad char=%d", c); } break; } } }
int divide_bignum(bignum *a, bignum *b, bignum *c) { int aSign, bSign; /* temporary signs */ int i; /* counters */ int numShiftRequired; int answerIndex; bignum aCopy, bCopy, tempNum; int compareResult; int count = 0; int isExiting = 0; int isIntegersDone = 0; if (isBignumZero(b)) { // Divided by zero. return 0; } initializeBignum(c); if (isBignumZero(a)) { return 1; } c->sign = (a->sign == b->sign) ? PLUS : MINUS; aSign = a->sign; bSign = b->sign; a->sign = b->sign = PLUS; aCopy = *a; bCopy = *b; if (b->numIntegers == 1 && b->integers[0] == 0) { for (i = 0; i < MAX_NUM_DIGITS; i++) { if (b->fractions[i] != 0) { break; } } numShiftRequired = (a->numIntegers - b->numIntegers) + i + 1; } else { numShiftRequired = (a->numIntegers > b->numIntegers) ? a->numIntegers - b->numIntegers : 0; } c->numIntegers = numShiftRequired + 1; leftShift(&bCopy, numShiftRequired); answerIndex = numShiftRequired; // Fill up the integers. while (answerIndex >= 0) { while (1) { if (isBignumZero(&aCopy)) { isExiting = 1; break; } compareResult = compareBignum(&aCopy, &bCopy); if (compareResult < 0) { if (--answerIndex < 0) { isIntegersDone = 1; break; } leftShift(&aCopy, 1); continue; } subtractBignum(&aCopy, &bCopy, &tempNum); aCopy = tempNum; c->integers[answerIndex]++; } if (isIntegersDone || isExiting) { break; } } if (!isExiting) { answerIndex = 0; leftShift(&aCopy, 1); c->numFractions++; // Fill up the fractions. while (answerIndex < MAX_NUM_DIGITS - 1) { while (1) { if (isBignumZero(&aCopy)) { isExiting = 1; break; } compareResult = compareBignum(&aCopy, &bCopy); if (compareResult < 0) { if (++answerIndex >= MAX_NUM_DIGITS - 1) { isExiting = 1; break; } leftShift(&aCopy, 1); c->numFractions++; continue; } subtractBignum(&aCopy, &bCopy, &tempNum); aCopy = tempNum; c->fractions[answerIndex]++; } if (isExiting) { break; } } } optimizeBignum(c); a->sign = aSign; b->sign = bSign; return 1; }