unsigned long Robot::Readings::update(void) { unsigned long start = micros(); shift_right(left_readings, NUM_READINGS); shift_right(right_readings, NUM_READINGS); shift_right(front_readings, NUM_READINGS); //take a reading left_readings[0] = analogRead(PINS::LEFT_SENSOR); right_readings[0] = analogRead(PINS::RIGHT_SENSOR); front_readings[0] = analogRead(PINS::FRONT_SENSOR); int left_avg = avg(left_readings, NUM_READINGS), right_avg = avg(right_readings, NUM_READINGS), front_avg = avg(front_readings, NUM_READINGS); is_wall_left = left_avg > THRESHOLDS::WALL; is_wall_right = right_avg > THRESHOLDS::WALL; is_wall_front = front_avg > THRESHOLDS::WALL; is_wall_left_close = left_avg > THRESHOLDS::WALL_CLOSE; is_wall_right_close = right_avg > THRESHOLDS::WALL_CLOSE; is_wall_front_close = front_avg > THRESHOLDS::FRONT_WALL_CLOSE; return micros() - start; }
void insert(uint64_t i, uint64_t x){ if(bitsize(x)>width_){ //auto vec = to_vector(i,x); //rebuild(vec); rebuild_ins( i, x ); return; } //not enough space for the new element: //alloc extra_ new words if(size_+1>(words.size()*(int_per_word_))){ //resize words auto temp = vector<uint64_t>(words.size()+extra_,0); uint64_t j = 0; for(auto x:words) temp[j++] = x; words = vector<uint64_t>(temp); } //shift right elements starting from number i shift_right(i); //insert x set(i,x); ++size_; }
void primitive_root(int n, int* a) { /** * TERMINATE condition */ if (n <= 2) return; int p = find_closest_prime(n); int l = (p+1)/2; int r = (n+1)/2 - l; /** * Before shift: * |---l---| |--r--| |---l---| |--r--| * ^^^^^^^^^^^^^^^^^ * * After shift: * * |---l---| |---l---| |--r--| |--r--| * ^^^^^^^^^^^^^^^^^ */ shift_right(r+l, a+l, l); primitive_root_prime(l+l, a); primitive_root(r+r, a+l+l); }
/* PPS kernel consumer compensates the whole phase error immediately. * Otherwise, reduce the offset by a fixed factor times the time constant. */ static inline s64 ntp_offset_chunk(s64 offset) { if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL) return offset; else return shift_right(offset, SHIFT_PLL + time_constant); }
template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) { SkASSERT(count > 0); auto p0 = static_cast<const typename F::Type*>(src); auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); auto d = static_cast<typename F::Type*>(dst); // Given pixels: // a0 b0 c0 d0 e0 ... // a1 b1 c1 d1 e1 ... // We want: // (a0 + 2*b0 + c0 + a1 + 2*b1 + c1) / 8 // (c0 + 2*d0 + e0 + c1 + 2*d1 + e1) / 8 // ... auto c0 = F::Expand(p0[0]); auto c1 = F::Expand(p1[0]); auto c = c0 + c1; for (int i = 0; i < count; ++i) { auto a = c; auto b0 = F::Expand(p0[1]); auto b1 = F::Expand(p1[1]); auto b = b0 + b0 + b1 + b1; c0 = F::Expand(p0[2]); c1 = F::Expand(p1[2]); c = c0 + c1; auto sum = a + b + c; d[i] = F::Compact(shift_right(sum, 3)); p0 += 2; p1 += 2; } }
int main(void) { int a[10]; rand_a(a, 10); print_a(a, 10); printf("max ele is: %d\n", max(a, 10)); printf("min ele is: %d\n", min(a, 10)); printf("shift left 3 eles:\n"); shift_left(a, 10, 3); print_a(a, 10); printf("shift right 3 eles:\n"); shift_right(a, 10, 3); print_a(a, 10); printf("reverse:\n"); reverse(a, 10); print_a(a, 10); printf("sort:\n"); bubble_sort(a, 10); print_a(a, 10); return 0; }
/** * 1st one: devide and conqer suggested by Leo Zhu */ void devide_and_conquer(int n, int* a) { /** * terminate condition * n == 2 */ if (n <= 2) return; int m = n / 2; int l = m / 2; /** * devide by shift right the middle part * * [0,l) [l, m, m+l) [m+l, n) */ shift_right(m, a+l, l); /** * conquer each sub part * [0, l) [l, 2l) [2l, n) */ devide_and_conquer(2*l, a); devide_and_conquer(n-2*l, a+2*l); }
void cal_fraction(unsigned sub[], unsigned numerator, unsigned denominator, unsigned bit_num) { memset(sub, 0, sizeof(unsigned) * LEN); sub[0] = numerator; divide(sub, denominator); shift_right(sub, bit_num); }
// OUT: matching by color position between two vectors of markers. bool PatternMatching::matchingMarkersByPosition(std::vector<Marker *> &m1, std::vector<Marker *> &m2) { /* std::cout << "marcador original : "; for (unsigned int j=0; j < m1.size(); ++j) { std::cout << m1[j]->colorid; } std::cout << std::endl; */ if (m1.size() == m2.size()) { for (unsigned int j=0; j < m1.size(); ++j) { unsigned int i; for (i=0; i < m1.size(); ++i) { if (m1[i]->colorid != m2[i]->colorid) break; }//for /* std::cout << "i : " << i << std::endl; std::cout << "m2.size(): " << m2.size() << std::endl; */ if (i == m2.size()) return true; shift_right(m2); }//for } return false; }
static void menu(void) { draw_glyph(modes[mode_id].glyph_id); while(state == STATE_MENU) { if(keypresses & KEY_LEFT) { keypresses &= ~KEY_LEFT; if(mode_id > 0) { mode_id--; const uint8_t glyph_id = modes[mode_id].glyph_id; for(uint8_t i = 0; i < 8; i++) { shift_right(); display[0] = read_glyph_column(glyph_id, 7 - i); _delay_ms(50); } } } else if(keypresses & KEY_RIGHT) { keypresses &= ~KEY_RIGHT; if(modes[mode_id + 1].run != NULL) { mode_id++; const uint8_t glyph_id = modes[mode_id].glyph_id; for(uint8_t i = 0; i < 8; i++) { shift_left(); display[7] = read_glyph_column(glyph_id, i); _delay_ms(50); } } } else if(keypresses & KEY_ENTER) { keypresses &= ~KEY_ENTER; state = STATE_NORMAL; } } }
/* * Remove "bits" bits from data */ XID removeXID(int bits, XID data) { int i; XID tmp; tmp = shift_right(shift_left(data, bits), bits); return tmp; }
void heapify(int a[], int low, int high) { int midIdx = (high - low - 1) / 2; while (midIdx >= 0) { shift_right(a, midIdx, high); --midIdx; } return; }
/*************************************************************************** * Function: pack_longs ***************************************************************************/ static void pack_longs(LONG *lng, int n, BYTE **target, int *bit, int size) { LONG mask, window; int valids, i, temp; int temp_bit = *bit; BYTE *temp_target = *target; if (size > 0) { mask = setbits[size]; for(i = 0; i < n; ++i) { window = lng[i] & mask; valids = size; if(temp_bit == 0) *temp_target = (BYTE) window; else { temp = shift_left(window, temp_bit); *temp_target |= temp; } window = shift_right(window, 8 - temp_bit); valids = valids - (8 - temp_bit); if(valids < 0) temp_bit += size; else { while (valids > 0) { *++temp_target = (BYTE) window; window = shift_right(window, 8); valids -= 8; } temp_bit = 8 + valids; } if(valids == 0) { temp_bit = 0; ++temp_target; } } *target = temp_target; *bit = (*bit + (size * n)) % 8; } }
static xid removexid(int bits, xid data) { int i; xid tmp; tmp = shift_right(shift_left(data, bits), bits); return tmp; }
void binary_type::prepend(const binary_type& prior) { shift_right(prior.size()); data_chunk prior_blocks = prior.blocks(); for (size_type i = 0; i < prior_blocks.size(); i++) { blocks_[i] = blocks_[i] | prior_blocks[i]; } }
int can_move_right(board *b) { board *tmp; tmp = dup_board(b); shift_right(tmp); merge_right(tmp); shift_right(tmp); if (cmp_board(b, tmp)) { free(tmp); return 0; } else { free(tmp); return 1; } }
void restore_key(Node *pnt, int k){ if (k==0) if (pnt->branch[1]->count >MIN_ORDER) shift_left(pnt,1); else merge(pnt,1); else if (k==pnt->count) if (pnt->branch[k-1]->count >MIN_ORDER) shift_right(pnt,k); else merge(pnt,k); else if (pnt->branch[k-1]->count>MIN_ORDER) shift_right(pnt,k); else if (pnt->branch[k+1]->count>MIN_ORDER) shift_left(pnt,k+1); else merge(pnt,k); }
void Vector<T>::insert(size_t before, T elem) { if (before > _size_) { throw std::out_of_range("subscript out of bounds"); } shift_right(before); //shift all element one step to right from pos _elemAry_[before] = elem; ++_size_; }
/* * Extract length bits starting at pos from data */ XID extractXID(int pos, int length, XID data) { int i; XID tmp; // length = 0 is handled in the boolean expression and is short-circuit // even though length of zero works in case of xids tmp = shift_right(shift_left(data, pos), (160-length)); return tmp; }
// Kupacrendezés void heap_sort(int a[], int n) { heapify(a, 0, n - 1); int high = n - 1; while (high > 0) { swap(&a[0], &a[high]); --high; shift_right(a, 0, high); } return; }
static void encode_sequence(const char *plain, int len, char *coded) { assert(len >= 0 && len <= 5); for (int block = 0; block < 8; block++) { int octet = get_octet(block); int junk = get_offset(block); if (octet >= len) { pad(&coded[block], 8 - block); return; } char c = shift_right(plain[octet], junk); if (junk < 0 && octet < len - 1) { c |= shift_right(plain[octet+1], 8 + junk); } coded[block] = encode_char(c); } }
// rozdil 2 dlouhych cisel (x-y) s #cifer rovnym #cifer mensence LNum operator-(const LNum & x, const LNum & y) { LNum ret(x.get_size()); LNum y2 = shift_right(y, x.get_size() - y.get_size()); // prictu dvojkovy doplnek bool carry = 1; for (int i = 0; i < x.get_size(); i++) { ret[i] = (x[i] ^ !y2[i]) ^ carry; carry = (x[i] && !y2[i]) || (x[i] && carry) || (!y2[i] && carry); } return ret; }
void delete_digit() { if( justify == Justify::Right ) { shift_right(); s.front() = ' '; } else { auto first_digit = std::find_if(s.rbegin(), s.rend(), [](const char& a) { return a != ' '; }); if( first_digit != s.rend() ) { *first_digit = ' '; } } }
int main(int argv, char **argc) { struct timespec mon, raw, start, end; long long delta1, delta2, interval, eppm, ppm; struct timex tx1, tx2; setbuf(stdout, NULL); if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) { printf("ERR: NO CLOCK_MONOTONIC_RAW\n"); return -1; } tx1.modes = 0; adjtimex(&tx1); get_monotonic_and_raw(&mon, &raw); start = mon; delta1 = diff_timespec(mon, raw); if (tx1.offset) printf("WARNING: ADJ_OFFSET in progress, this will cause inaccurate results\n"); printf("Estimating clock drift: "); sleep(120); get_monotonic_and_raw(&mon, &raw); end = mon; tx2.modes = 0; adjtimex(&tx2); delta2 = diff_timespec(mon, raw); interval = diff_timespec(start, end); /* calculate measured ppm between MONOTONIC and MONOTONIC_RAW */ eppm = ((delta2-delta1)*NSEC_PER_SEC)/interval; eppm = -eppm; printf("%lld.%i(est)", eppm/1000, abs((int)(eppm%1000))); /* Avg the two actual freq samples adjtimex gave us */ ppm = (tx1.freq + tx2.freq) * 1000 / 2; ppm = (long long)tx1.freq * 1000; ppm = shift_right(ppm, 16); printf(" %lld.%i(act)", ppm/1000, abs((int)(ppm%1000))); if (llabs(eppm - ppm) > 1000) { printf(" [FAILED]\n"); return ksft_exit_fail(); } printf(" [OK]\n"); return ksft_exit_pass(); }
void onewire_write(int data) { int count; for (count=0; count<8; ++count) { output_low(ONE_WIRE_PIN); delay_us( 2 ); // pull 1-wire low to initiate write time-slot. output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire delay_us( 60 ); // wait until end of write slot. output_float(ONE_WIRE_PIN); // set 1-wire high again, delay_us( 2 ); // for more than 1us minimum. } }
int remove_next(int seq) { int k,val; for(k=head; k>tail; k--) { if(status[queue[k]][seq]!=1) { val=queue[k]; shift_right(k); return val; } } return -1; }
// soucet 2 dlouhych cisel LNum operator+(const LNum & x, const LNum & y) { LNum longer = (x.get_size() > y.get_size()) ? x : y; LNum shorter = (x.get_size() > y.get_size()) ? y : x; // doplneni kratsiho zprava "0", aby byly obe cisla stejne dlouha shorter = shift_right(shorter, longer.get_size() - shorter.get_size()); LNum ret(longer.get_size()); bool carry = 0; for (int i = 0; i < longer.get_size(); i++) { ret[i] = (longer[i] ^ shorter[i]) ^ carry; carry = (longer[i] && shorter[i]) || (longer[i] && carry) || (shorter[i] && carry); } // posledni prenos -> pridam dalsi cifru k vysledku if (carry) { ret = shift_right(ret, 1); ret[ret.get_size()-1] = 1; } return ret; }
template <typename F> void downsample_2_1(void* dst, const void* src, size_t srcRB, int count) { SkASSERT(count > 0); auto p0 = static_cast<const typename F::Type*>(src); auto d = static_cast<typename F::Type*>(dst); for (int i = 0; i < count; ++i) { auto c00 = F::Expand(p0[0]); auto c01 = F::Expand(p0[1]); auto c = c00 + c01; d[i] = F::Compact(shift_right(c, 1)); p0 += 2; } }
PlayerTank::PlayerTank() { this->setLife(100); this->setFlag(QGraphicsItem::ItemIsFocusable); this->speed = 20; multi_kill_timer = new QTimer(this); multi_kill_timer->setInterval(2000); multi_kill_count_flags = 0; QObject::connect(multi_kill_timer, SIGNAL(timeout()), this, SLOT(shift_right())); multi_kill_timer->start(); }
int onewire_read() { int count, data; for (count=0; count<8; ++count) { output_low(ONE_WIRE_PIN); delay_us( 2 ); // pull 1-wire low to initiate read time-slot. output_float(ONE_WIRE_PIN); // now let 1-wire float high, delay_us( 8 ); // let device state stabilise, shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result. delay_us( 120 ); // wait until end of read slot. } return( data ); }