void findPrimes(bignum topCandidate) { BITARRAY *ba = createBitArray(topCandidate); assert(ba != NULL); /* SET ALL BUT 0 AND 1 TO PRIME STATUS */ setAll(ba); clearBit(ba, 0); clearBit(ba, 1); /* MARK ALL THE NON-PRIMES */ bignum thisFactor = 2; bignum lastSquare = 0; bignum thisSquare = 0; while (thisFactor * thisFactor <= topCandidate) { /* MARK THE MULTIPLES OF THIS FACTOR */ bignum mark = thisFactor + thisFactor; while (mark <= topCandidate) { clearBit(ba, mark); mark += thisFactor; } /* PRINT THE PROVEN PRIMES SO FAR */ thisSquare = thisFactor * thisFactor; for (; lastSquare < thisSquare; lastSquare++) { if (getBit(ba, lastSquare)) printPrime(lastSquare); } /* SET thisFactor TO NEXT PRIME */ thisFactor++; while (getBit(ba, thisFactor) == 0) thisFactor++; assert(thisFactor <= topCandidate); } /* PRINT THE REMAINING PRIMES */ for (; lastSquare <= topCandidate; lastSquare++) { if (getBit(ba, lastSquare)) printPrime(lastSquare); } freeBitArray(ba); }
void printBoard() { system("cls"); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (map[y][x] == GOAL) { if (getBit(nowCase.m_State, x, y) == 1) { printf("▩"); } else { printf("▣"); } } else if (map[y][x] == BLACK) { printf("■"); } else if (getBit(nowCase.m_State, x, y) == 1) { printf("□"); } else { printf("▒"); } } printf("\n"); } }
int checkStartAck(unsigned char byte) { if(((int)(byte & 0xF3) == 0xA3)&& getBit(byte, 2) == com.host_ack) { com.client_ack = getBit(byte, 3); switchAck(); return 1; } return 0; }
int ccbiReader::readIntWithSign(bool sign) { // Read encoded int int numBits = 0; while (!getBit()) { numBits++; } long long current = 0; for (int a=numBits-1; a >= 0; a--) { if (getBit()) { current |= (long long)(1 << a); } } current |= (long long)(1 << numBits); int num; if (sign) { int s = (int)(current%2); if (s) num = (int)(current/2); else num = (int)(-current/2); } else { num = (int)(current-1); } alignBits(); return num; }
static void scanSource(Fsck *chk, char *name, Source *r) { u32int a, nb, o; Block *b; Entry e; if(!chk->useventi && globalToLocal(r->score)==NilBlock) return; if(!sourceGetEntry(r, &e)){ error(chk, "could not get entry for %s", name); return; } a = globalToLocal(e.score); if(!chk->useventi && a==NilBlock) return; if(getBit(chk->smap, a)) return; setBit(chk->smap, a); nb = (sourceGetSize(r) + r->dsize-1) / r->dsize; for(o = 0; o < nb; o++){ b = sourceBlock(r, o, OReadOnly); if(b == nil){ error(chk, "could not read block in data file %s", name); continue; } if(b->addr != NilBlock && getBit(chk->errmap, b->addr)){ warn(chk, "previously reported error in block %ux is in file %s", b->addr, name); } blockPut(b); } }
static enum COLOUR getColourEnum(struct gameboy * gameboy, uint8_t colourNum, uint16_t address) { enum COLOUR result = WHITE; uint8_t palette = readByte(gameboy, address); int hi = 0; int lo = 0; //which bits of the colour palette does the colour id map to? switch(colourNum) { case 0: hi = 1; lo = 0; break; case 1: hi = 3; lo = 2; break; case 2: hi = 5; lo = 4; break; case 3: hi = 7; lo = 6; break; } //use the palette to get the colour int colour = 0; colour = getBit(palette, hi) << 1; colour |= getBit(palette, lo); //convert the game colour to emulator colour switch(colour) { case 0: result = WHITE; break; case 1: result = LIGHT_GREY; break; case 2: result = DARK_GREY; break; case 3: result = BLACK; break; } return result; }
void swapBits(int *number, int fpos, int spos) { int fbit = getBit(*number, fpos), sbit = getBit(*number, spos); if(fbit != sbit) { if(fbit) { changeBitToOne(number, spos); } else { changeBitToNull(number, spos); } if(sbit) { changeBitToOne(number, fpos); } else { changeBitToNull(number, fpos); } } }
void Drives::PortWrite(word addr, byte val) { UNREFERENCED_PARAMETER(addr); ctrl_drive = getBits(val, cDriveSelectOfs, cDriveSelectBits); ctrl_side = getBit(val, cSideSelectOfs); ctrl_ddense = getBit(val, cDensitySelectOfs) != 0; }
int getNext(int n) { int i=0, c=0; while(!getBit(n, i)) i++; while(getBit(n, i)) i++, c++; setBit(n, i); n = (n & ~((1<<i) - 1)) | ((1<<(c-1))-1); return n; }
int totalNeighborsDiag(unsigned char *bits, int w, int h, int x, int y) { int total= 0; if(getBit(bits,w,h,x-1,y-1)) ++total; // Top Left if(getBit(bits,w,h,x+1,y-1)) ++total; // Top Right if(getBit(bits,w,h,x-1,y+1)) ++total; // Bottom Left if(getBit(bits,w,h,x+1,y+1)) ++total; // Bottom Right return total; }
bool dwIsClockProblem(dwDevice_t* dev) { bool clkllErr, rfllErr; clkllErr = getBit(dev->sysstatus, LEN_SYS_STATUS, CLKPLL_LL_BIT); rfllErr = getBit(dev->sysstatus, LEN_SYS_STATUS, RFPLL_LL_BIT); if(clkllErr || rfllErr) { return true; } return false; }
/** * This computes the number of common most-significant bits in the mantissas * of two double-precision numbers. * It does not count the hidden bit, which is always 1. * It does not determine whether the numbers have the same exponent - if they do * not, the value computed by this function is meaningless. * @param db * @return the number of common most-significant mantissa bits */ int CommonBits::numCommonMostSigMantissaBits(int64 num1, int64 num2) { int count = 0; for (int i = 52; i >= 0; i--){ if (getBit(num1, i) != getBit(num2, i)) return count; count++; } return 52; }
/* * check if the acknowledge byte is valid */ int checkAck(unsigned char* bytes) { if(((bytes[0] & 0xF0) == 0xF0) & (getBit(bytes[0], 2) == com.host_ack)) { com.client_ack = getBit(bytes[0], 3); switchAck(); //very important to switch ack for next ack com.failReceive = 0; return 1; } com.failReceive++; return 0; }
/** * @brief Returns count of black pixels adjacent to the pixel at (x,y) in the four rectangular * positions e.g. above, below, left, right. * * bits - input bitplane, 1 bit per pixel. 1=black, 0=white. * w,h - dimensions of bitplane. */ int totalNeighborsRect(unsigned char *bits, int w, int h, int x, int y) { int total= 0; if(getBit(bits,w,h,x ,y-1)) ++total; // Top if(getBit(bits,w,h,x ,y+1)) ++total; // Bottom if(getBit(bits,w,h,x-1,y )) ++total; // Left if(getBit(bits,w,h,x+1,y )) ++total; // Right return total; }
bool dwIsReceiveFailed(dwDevice_t *dev) { bool ldeErr, rxCRCErr, rxHeaderErr, rxDecodeErr; ldeErr = getBit(dev->sysstatus, LEN_SYS_STATUS, LDEERR_BIT); rxCRCErr = getBit(dev->sysstatus, LEN_SYS_STATUS, RXFCE_BIT); rxHeaderErr = getBit(dev->sysstatus, LEN_SYS_STATUS, RXPHE_BIT); rxDecodeErr = getBit(dev->sysstatus, LEN_SYS_STATUS, RXRFSL_BIT); if(ldeErr || rxCRCErr || rxHeaderErr || rxDecodeErr) { return true; } return false; }
int parseBroadcasterLoop(unsigned char *data,BroadcasterLoop *l) { int boff = 0; memset(l,0,sizeof(BroadcasterLoop)); l->broadcaster_id = getBit(data,&boff,8); getBit(data,&boff,4); l->broadcaster_descriptors_length = getBit(data,&boff,12); return 3; }
uint8_t Life::evolve() { uint8_t changecount = 0; for (char y=0; y<NUM_RINGS; y++) { for (char x=0; x<LEDS_PER_RING; x++) { // Count the neighbors int n = countNeighborsWithWraparound(x,y); // implement the rules if (getBit(universe, y, x)) { // Any live cell with < 2 live neighbors dies. // Any live cell with 2-3 neighbors is good. // Any live cell with > 3 neighbors dies. if (n == 2 || n == 3) { setBit(newUniverse, y, x); } else { clearBit(newUniverse, y, x); } } // Any dead cell with == 3 neighbors becomes live. else if (n == 3) { setBit(newUniverse, y, x); } else { // (else it stays dead) clearBit(newUniverse, y, x); } } } // Put the new universe in place, counting the number // of changes for (char y=0; y<NUM_RINGS; y++) { for (char x=0; x<LEDS_PER_RING; x++) { uint8_t oldState = getBit(universe, y, x); if (getBit(newUniverse, y, x)) { if (!oldState) changecount++; setBit(universe, y, x); } else { if (oldState) changecount++; clearBit(universe, y, x); } } } return changecount; }
inline void reverseBitBlock(iterator A, uint64_t const offset, uint64_t const len) { uint64_t o0 = offset; uint64_t o1 = offset+len-1; uint64_t o2 = o0 + len/2; for ( ; o0 != o2; o0++, o1-- ) { bool const b0 = getBit(A,o0); bool const b1 = getBit(A,o1); putBit(A,o0,b1); putBit(A,o1,b0); } }
void test_bitTasks() { assert(getBit(4,1) == 0); assert(getBit(4,2) == 1); assert(setBit(4,1) == 6); assert(setBit(4,2) == 4); assert(clearBit(4,1) == 4); assert(clearBit(4,2) == 0); assert(updateBit(4,1,0) == 4); assert(updateBit(4,2,1) == 4); }
bool isBinPalindrome(unsigned int num) { int first_one; for (int i = 19; 1; --i) { if (getBit(num, i)) { first_one = i; break; } } for (int i = first_one, j = 0; i > j; --i, ++j) { if (getBit(num, i) ^ getBit(num, j)) { return false; } } return true; }
/* simple printing for debugging purposes */ void printBitRow(BitMatrix *m,int y){ int x; for(x=0;x<m->width;x++){ printf("%01d ",getBit(m,x,y)); } printf("\n"); }
// Checks to see if a button has been pressed since the last time this function // was called, and returns that button's ID if one has. If no button has been // pressed, returns BUTTON_NONE. int input_getButtonPress(void) { int i, curr, diff; // Record the previous state of ~FIO0PIN between invocations. static int prev = 0; // List of button IDs to loop through. const int buttons[BUTTON_COUNT] = { BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_CENTER }; // Why on earth does FIO0PIN use a 0 to signify a button being pressed? curr = ~FIO0PIN; // Find the buttons that have been pressed since last invocation. diff = (curr ^ prev) & curr; // Remember the current button state for next time. prev = curr; // Find the first button that has just been pressed and return it. for (i = 0; i < BUTTON_COUNT; ++i) { if (getBit(diff, buttons[i])) return buttons[i]; } // Otherwise, nothing new has been pressed. return BUTTON_NONE; }
void Gene::matrixExtractColumnToInt(unsigned * extracted, unsigned column, unsigned columnSize) const{ unsigned start = column * columnSize; for (int i = 0 ; i < columnSize ; i++){ unsigned pos = start +i; extracted[i] = getBit(pos); } }
int main() { int pos = 0; int config = 0; int flash = 0; FIO3DIR = 0xFFFFFFFF; for (;;) { switch (input_getButtonPress()) { case BUTTON_LEFT: pos = (pos + 1) & 31; break; case BUTTON_RIGHT: pos = (pos - 1) & 31; break; case BUTTON_UP: config = setBit(config, pos); break; case BUTTON_DOWN: config = clrBit(config, pos); break; case BUTTON_CENTER: config = cpyBit(config, pos, 1 ^ getBit(config, pos)); break; case BUTTON_NONE: FIO3PIN = cpyBit(config, pos, flash < 125); flash = (flash + 1) % (250); wait(1); break; } } return 0; }
void U2Bits::setBits(uchar* dstBits, int pos, const uchar* srcBits, int nBits) { // TODO: optimize for (int i = 0; i < nBits; i++) { bool val = getBit(srcBits, i); setBit(dstBits, i + pos, val); } }
void BitmapEncoding::Bitmap::decodeBitmap(uint8_t const * const baseValue, uint8_t const * const bitmap, uint8_t * const target) { uint32_t i; uint8_t *writePtr = target; uint8_t *readPtr = const_cast<uint8_t *>(bitmap); uint8_t offset = 0; for(i = 0; i < _bitmapElements; ++i) { if(getBit(*readPtr, offset)) { memcpy(writePtr, baseValue, _elementSize); } writePtr += _elementSize; ++offset; if(offset == 8) { ++readPtr; offset = 0; } } }
void UncompressFile::doit(HuffmanTree& ht) { if (!osPtr.is_open()) { // Open file osPtr.open(outFile.c_str()); if (!osPtr) { cerr << "Error: Could not open: '" << outFile << "' for writing" << endl; return; } } unsigned char c; while (true) { c = ht.getChar(getBit()); if (c < (unsigned char)'\x80') { // if ( c > ' ' ) printf("'%c'\n", c); // else printf("'%02x'\n", c); osPtr.put(c); continue; } else if (c == (unsigned char)EOF) { break; } else if (c == (unsigned char)'\xf0') { // Not there yet continue; } else if (c == (unsigned char)'\xf1') { cout << "uncompress() : Internal Error\n"; break; } } if ( isPtr.is_open()) isPtr.close(); if ( osPtr.is_open()) osPtr.close(); } // End doit()
uchar *unRLE(uchar *buff, ulong buffLen, uchar *cmdBuff, ulong cmdLen, ulong *outLen) { bitBuffer *bitBuff = NULL; uchar copyFlag = 0; ulong n = 0, i = 0; uchar *outBuff = NULL; bitBuff = bitBuffer_new(cmdBuff, cmdLen); copyFlag = getBit(bitBuff); *outLen = getEliasGamma(bitBuff); smalloc(outBuff, *outLen); for(i = 0; i < *outLen; i += n) { n = getEliasGamma(bitBuff); if(copyFlag) { memcpy(outBuff + i, buff, n); buff += n; } else memset(outBuff + i, 0, n); copyFlag = !copyFlag; } free(bitBuff); return outBuff; }
/* * Attempt to read all (usable) blocks on the device. * Bad blocks are immediately reserved and (later) will be gathered * together into one or more bad-block files. */ void checkBlocks(void) { int block; if (Verbose) { fprintf(stderr,"Checking disk blocks..."); fflush(stderr); } for (block = 0; block < SB->sb_blocks; block++) { if (bad(block)) { if (!getBit(BMap,block)) BadBlocks++; setBit(BMap,block); if (Verbose) { fprintf(stderr,"Block %d is bad.\n",block); } if (block < SB->sb_first_block) { fprintf(stderr,"Early bad block (%d) makes file system impossible.\n", block); exit(1); } } } if (Verbose) { fprintf(stderr,"done.\n"); } }
void BitWriter::addByte(LByte input) { for(int i = 0; i < 8; i++) { addBit(getBit(input, i)); } }