int main()
{
    printf("Bit at position 2 of 2: %d\n",bit_i(2,2));
    printf("Bit at position 1 of 2: %d\n",bit_i(2,1));
    printf("Binary print of 128: ");
    write_bits(128);
    printf("\n");
    printf("Binary print of 12: ");
    write_bits(12);
    printf("\n");
    printf("Uint with bit 1 at position 13: %u\n",uint_bit_on_at_pos(13));
    printf("Number of bits on in 12: %d\n",num_of_bits_on(12));
    printf("Set bit 2 of 12 on: %d\n",set_bit_at_pos(12,2));
    printf("Unset bit 3 of 12 on: %d\n",unset_bit_at_pos(12,3));
    printf("Toggle bit 3 of 12 on (set to off): %d\n",toggle_bit_at_pos(12,3));
    printf("Toggle bit 2 of 12 on (set to on): %d\n",toggle_bit_at_pos(12,2));
    printf("Equal test (22==22): %d\n",equal(22,22));
    printf("Equal test (22==100): %d\n",equal(22,100));
    printf("Even test (23): %d\n",is_even(23));
    printf("Even test (22): %d\n",is_even(22));
    printf("Even test (10211): %d\n",is_even(10211));
    uint test_swapper = 356;
    printf("Swapping even and odd bits of: \t\t");
    write_bits(test_swapper);
    swap_even_odd(&test_swapper);
    printf("\nResult of swapping even an odd bits: \t");
    write_bits(test_swapper);
    printf("\n");
}
Пример #2
0
void test_some_math()
{
      // GCD tests
      assert(gcd(54,24) == gcd(12,90) && gcd(13,11) == 1);
      // LCM tests
      assert(lcm(114920, 14300) == 6320600);
      // Binomial coefficient test
      assert(binomial(10,2) == 45);
      // Test add
      assert(add(3,5) == 8 && add(7,3) == 10);
      // Test odd or even numbers
      assert(!is_even(1) && is_even(2) && !is_even(77) && is_even(500));
      // Test if bit n is set
      assert(!isbitNset(22, 3) && isbitNset(22,4));
      // Test set bit
      assert(setbitN(22,3) == 30 && setbitN(2,0) == 3);
      // Test unset bit
      assert(unsetbitN(30,3) == 22 && unsetbitN(3,0) == 2);
      // Test toggle
      assert(togglebitN(togglebitN(22,3),3) == 22);
      // Test modulo (power of two)
      assert(modulo(7,2) == 1 && modulo(6666,2) == 0 && modulo(64,8) == 0);
      // Test negate nibbles
      assert(neg_nibbles(173,0) == 160 && neg_nibbles(429,1) == 256);;
      // Test powX
      assert(powX(7,11) == 1977326743 && pow(2,8) == 256 && pow(123123,0) == 1);
}
Пример #3
0
int main(int argc,char *argv[]) {
  double timediff=0;
  time_t start,end;
  freopen("CON", "w", stdout); 
  printf("\n\nQ2\n");
  time(&start);
  //START
  int sum       =0;
  int previous  =1;
  int current   =2;
  
  while(current<4000000){
    int temp = current;
    current=previous+current;
    previous=temp;
    if(is_even(temp)) sum+=temp;
    printf("%d %d\n",temp,is_even(temp));
  }
  printf("Answer: %d\n",sum);
  
  //END
  time(&end);
  timediff = difftime(start, end);
  printf("Execution Time: %f\n",timediff);
  exit(0);
}
Пример #4
0
int main() {

	printf("start testing\n"); 

	int from = -100, to = 100; 
	for (int i = from; i <= to; i++) {
		assert(0 - i == negate(i)); 
		assert(((i % 2) == 0) == is_even(i)); 
		assert(i * 2 == multiply_by_two(i)); 
		if (is_even(i)) {
			assert(i / 2 == divide_by_two(i)); 
		}
	}

	for (int i = from;i <= to; i++) {
		for (int j = from; j <= to; j++) {
			assert(i + j == add_i(i, j)); 
			assert(i - j == subtract(i, j)); 
			assert(i * j == multiply(i, j)); 
		}
	}

	printf("end testing\n"); 
	return 0; 
}
Пример #5
0
void token_grid_test09()
{
   std::cout << "token_grid_test09\n";

   std::string data;
   data += "1.1,1.1,1.1,1.1,1.1,1.1\n"
           "2.2,2.2,2.2,2.2,2.2,2.2\n"
           "3.3,3.3,3.3,3.3,3.3,3.3\n"
           "4.4,4.4,4.4,4.4,4.4,4.4\n"
           "5.5,5.5,5.5,5.5,5.5,5.5\n"
           "6.6,6.6,6.6,6.6,6.6,6.6\n"
           "7.7,7.7,7.7,7.7,7.7,7.7\n";

   {
      strtk::token_grid grid(data,data.size(),",");

      for (std::size_t r = 0; r < grid.row_count(); ++r)
      {
         std::string row = "";
         if (grid.join_row(r,"|",row))
            std::cout << "row["<< r <<"] = " << row << std::endl;
         else
            std::cout << "failed row["<< r <<"]" << std::endl;
      }

      for (std::size_t c = 0; c < grid.max_column_count(); ++c)
      {
         std::string col = "";
         if (grid.join_column(c,"|",col))
            std::cout << "col["<< c <<"] = " << col << std::endl;
         else
            std::cout << "failed col["<< c <<"]" << std::endl;
      }
   }

   {
      strtk::token_grid grid(data,data.size(),",");

      for (std::size_t r = 0; r < grid.row_count(); ++r)
      {
         std::string row = "";
         if (grid.join_row(r,is_even(),"|",row))
            std::cout << "row["<< r <<"] = " << row << std::endl;
         else
            std::cout << "failed row["<< r <<"]" << std::endl;
      }

      for (std::size_t c = 0; c < grid.max_column_count(); ++c)
      {
         std::string col = "";
         if (grid.join_column(c,is_even(),"|",col))
            std::cout << "col["<< c <<"] = " << col << std::endl;
         else
            std::cout << "failed col["<< c <<"]" << std::endl;
      }
   }

}
Пример #6
0
static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) {
    if (xAxis) {
        SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight));
        rect->fLeft /= 2;
        rect->fRight /= 2;
    }
    if (yAxis) {
        SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom));
        rect->fTop /= 2;
        rect->fBottom /= 2;
    }
}
Пример #7
0
int main() {
    printf("Zijn gelijk 5, 5: %d\n", zijn_gelijk(5, 5));
    printf("Zijn gelijk 5, 6: %d\n", zijn_gelijk(5, 6));
    printf("Zijn gelijk 223, 15: %d\n", zijn_gelijk(5, 6));

    printf("Aantal 1 bits van 5 moet 2 zijn: %d\n", tel_aantal_1bits(5));
    printf("Aantal 1 bits van 15 moet 4 zijn: %d\n", tel_aantal_1bits(15));

    printf("is_even? 5 moet 0 geven (oneven): %d\n", is_even(5));
    printf("is_even? 6 moet 1 geven (even): %d", is_even(6));

}
bool tiles_adjacent(const map_location& a, const map_location& b)
{
	// Two tiles are adjacent:
	// if y is different by 1, and x by 0,
	// or if x is different by 1 and y by 0,
	// or if x and y are each different by 1,
	// and the x value of the hex with the greater y value is even.

	const int xdiff = abs(a.x - b.x);
	const int ydiff = abs(a.y - b.y);
	return (ydiff == 1 && a.x == b.x) || (xdiff == 1 && a.y == b.y) ||
	       (xdiff == 1 && ydiff == 1 && (a.y > b.y ? is_even(a.x) : is_even(b.x)));
}
inline   bool VMRegImpl::is_concrete() {
  assert(is_reg(), "must be");
  int v = value();
  if ( v  <  ConcreteRegisterImpl::max_gpr ) {
    return is_even(v);
  }
  // F0..F31
  if ( v <= ConcreteRegisterImpl::max_gpr + 31) return true;
  if ( v <  ConcreteRegisterImpl::max_fpr) {
    return is_even(v);
  }
  assert(false, "what register?");
  return false;
}
size_t distance_between(const map_location& a, const map_location& b)
{
	const size_t hdistance = abs(a.x - b.x);

	const size_t vpenalty = ( (is_even(a.x) && is_odd(b.x) && (a.y < b.y))
		|| (is_even(b.x) && is_odd(a.x) && (b.y < a.y)) ) ? 1 : 0;

	// For any non-negative integer i, i - i/2 - i%2 == i/2
	// previously returned (hdistance + vdistance - vsavings)
	// = hdistance + vdistance - minimum(vdistance,hdistance/2+hdistance%2)
	// = maximum(hdistance, vdistance+hdistance-hdistance/2-hdistance%2)
	// = maximum(hdistance,abs(a.y-b.y)+vpenalty+hdistance/2)

	return std::max<int>(hdistance, abs(a.y - b.y) + vpenalty + hdistance/2);
}
int compare_rule(int x, int y)
{
	if (is_even(x)) {
		if (is_even(y))
			return y - x;
		else
			return 1;
	}
	else {
		if (is_even(y))
			return -1;
		else
			return x - y;
	}
}
Пример #12
0
int is_odd(int n)
{
    if (n==0)
        return 1;
    else
        return is_even(n-2);
}
Пример #13
0
int main()
{
    size_t n, n2, v, i;
    void *res[DATA_ITEMS];
	int result = 0;
    
    n = select(idata, sizeof(*idata), DATA_ITEMS, is_even, res, idata);
    for (v = i = 0; i < n; i++)
        if (is_even(res[i], idata))
            v++;
    if (n != 8 || v != n) {
        printf("-> check even numbers test failed\n");
        result = 1;
    }

    n2 = select(idata, sizeof(*idata), DATA_ITEMS, is_odd, res, &idata[DATA_ITEMS - 1]);
    for (v = i = 0; i < n2; i++)
        if (is_odd(res[i], &idata[DATA_ITEMS - 1]))
            v++;
    if (n2 != 7 || v != n2) {
        printf("-> check odd numbers test failed\n");
        result += 1;
    }
    if (n + n2 != DATA_ITEMS) {
        printf("-> test even plus odd numbers failed\n");
        result += 1;
    }
	if (result == 0)
    	printf("-> all tests succeeded\n");
    return result;
}
Пример #14
0
TEST(algorithm, copy_if_test)
{
    namespace algo = ::pfi::lang::algorithm;

    {
        std::vector<int> v;
        for (int i = 0; i < 5; ++i)
            v.push_back(i);
        std::vector<int> actual;
        algo::copy_if(v.begin(), v.end(), std::back_inserter(actual), is_even());

        std::vector<int> expected;
        expected.push_back(0);
        expected.push_back(2);
        expected.push_back(4);

        EXPECT_EQ(expected.size(), actual.size());
        EXPECT_TRUE(std::equal(expected.begin(), expected.end(), actual.begin()));
    }
    {
        std::vector<char> v;
        for (char c = CHAR_MIN; c < CHAR_MAX; ++c) {
            v.push_back(c);
        }
        v.push_back(CHAR_MAX);
        std::string actual;
        algo::copy_if(v.begin(), v.end(), std::back_inserter(actual), ::isdigit);

        std::string expected("0123456789");

        EXPECT_EQ(expected, actual);
    }
}
Пример #15
0
INT base::is_odd()
{
	if (is_even())
		return FALSE;
	else
		return TRUE;
}
Пример #16
0
void
opustex_print_note (FILE *f, char pitch)
{
  if (is_even (otexclef))
    {
      if (pitch - otexclef < 104)
        {
          fprintf (f, "%c", pitch - otexclef - 25);
        }
      else
        {
          fprintf (f, "%c", pitch - otexclef - 7);
        }
    }
  else
    {
      if (pitch - otexclef < 97)
        {
          fprintf (f, "%c", pitch - otexclef - 18);
        }
      else
        {
          fprintf (f, "%c", pitch - otexclef);
        }
    }
}
Пример #17
0
int is_odd(int i)
{
    if (i == 0)
        return 0;
    else
        return is_even(i - 1);
}
Пример #18
0
inline   bool is_concrete() {
  assert(is_reg(), "must be");
#ifndef AMD64
  if (is_Register()) return true;
#endif // AMD64
  return is_even(value());
}
Пример #19
0
bool is_prime(int tocheck){
  //Bruteforce is_prime
  if(tocheck>2 && is_even(tocheck)) return false;
  for(int x=3;x<(tocheck/2);x++){
    if(!(tocheck%x)){ return false;}
  }
  return true;
}
Пример #20
0
void test_jacobi(rand_t state)
{
   ZZ_t a, b, c, d;
   long i;
   int j1, j2;

   cout << "jacobi... ";

   for (i = 0; i < 1000; i++) {
      randbits(a, state, n_randint(state, 1000));
      do {
         randbits(b, state, n_randint(state, 1000));
      } while (is_even(b));

      randbits(c, state, n_randint(state, 1000));
      
      j1 = jacobi(a, b);
      j2 = jacobi(c, b);

      a *= c;
           
      assert(jacobi(a, b) == j1*j2);
   }

   for (i = 0; i < 1000; i++) {
      randbits(a, state, n_randint(state, 1000));
      do {
         randbits(b, state, n_randint(state, 1000));
      } while (is_even(b));

      do {
         randbits(d, state, n_randint(state, 1000));
      } while (is_even(d));


      j1 = jacobi(a, b);
      j2 = jacobi(a, d);

      b *= d;
           
      assert(jacobi(a, b) == j1*j2);
   }

   cout << "PASS" << endl;
}
Пример #21
0
void *
function_odd(void *arg) {
  int n = (int) arg;
  assert(0 == is_even(n));
  printf("thread 0x%x: ODD   <-->  %d\n", (int) pthread_self(), n);
  count++;
  sleep(1);
  return NULL;
}
Пример #22
0
int main(void){
  unsigned int fib;
  unsigned int sum = 2;
  while ((fib = get_next_fib()) <= MAX_FIB){
    if (is_even(fib)){
      sum += fib;
    }
  }
  fprintf(stderr, "Sum: %d\n", sum);
}
Пример #23
0
int main()
{
    int ten_is_even = is_even(10);
    printf("10 is even: %i\n", ten_is_even);

    int ten_is_odd = is_odd(10);
    printf("10 is odd: %i\n", ten_is_odd);

    return 0;
}
Пример #24
0
int main() //@ : main
    //@ requires true;
    //@ ensures true;
    //@ terminates;
{
    //@ produce_call_below_perm_();
    //@ close exists(5);
    is_even(10);
    return 0;
}
map_location map_location::get_direction(
			map_location::DIRECTION dir, int n) const
{
	if (n < 0 ) {
		dir = get_opposite_dir(dir);
		n = -n;
	}
	switch(dir) {
		case NORTH:      return map_location(x, y - n);
		case SOUTH:      return map_location(x, y + n);
		case SOUTH_EAST: return map_location(x + n, y + (n+is_odd(x))/2 );
		case SOUTH_WEST: return map_location(x - n, y + (n+is_odd(x))/2 );
		case NORTH_EAST: return map_location(x + n, y - (n+is_even(x))/2 );
		case NORTH_WEST: return map_location(x - n, y - (n+is_even(x))/2 );
		default:
			assert(false);
			return map_location();
	}
}
void get_adjacent_tiles(const map_location& a, map_location* res)
{
	res->x = a.x;
	res->y = a.y-1;
	++res;
	res->x = a.x+1;
	res->y = a.y - (is_even(a.x) ? 1:0);
	++res;
	res->x = a.x+1;
	res->y = a.y + (is_odd(a.x) ? 1:0);
	++res;
	res->x = a.x;
	res->y = a.y+1;
	++res;
	res->x = a.x-1;
	res->y = a.y + (is_odd(a.x) ? 1:0);
	++res;
	res->x = a.x-1;
	res->y = a.y - (is_even(a.x) ? 1:0);
}
Пример #27
0
void test_copy_while ( Container const &c ) {

    typedef typename Container::value_type value_type;
    typename Container::const_iterator it;
    std::vector<value_type> v;

//  None of the elements
    v.clear ();
    ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_false);
    BOOST_CHECK ( v.size () == 0 );

    v.clear ();
    ba::copy_while ( c, back_inserter ( v ), is_false);
    BOOST_CHECK ( v.size () == 0 );

//  All the elements
    v.clear ();
    ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_true);
    BOOST_CHECK ( v.size () == c.size ());
    BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));

    v.clear ();
    ba::copy_while ( c, back_inserter ( v ), is_true);
    BOOST_CHECK ( v.size () == c.size ());
    BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));

//  Some of the elements
    v.clear ();
    it = ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
    BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
    BOOST_CHECK ( it == c.end () || !is_even ( *it ));
    BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
    BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));

    v.clear ();
    it = ba::copy_while ( c, back_inserter ( v ), is_even ).first;
    BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
    BOOST_CHECK ( it == c.end () || !is_even ( *it ));
    BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
    BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
    }
Пример #28
0
double cheb(int n, double x)
{
  it_assert((n >= 0), "cheb(): need a non-negative order n!");

  if (x < 1.0 && x > -1.0) {
    return std::cos(n * std::acos(x));
  }
  else if (x <= -1) {
    return (is_even(n) ? std::cosh(n * ::acosh(-x))
            : -std::cosh(n * ::acosh(-x)));
  }
  return std::cosh(n * ::acosh(x));
}
Пример #29
0
void
opustex_print_augmentum_note (FILE *f, char pitch)
{
  int realpitch;

  if (is_even (otexclef))
    {
      if (pitch - otexclef < 104)
        {
          realpitch = pitch - otexclef - 25;
        }
      else
        {
          realpitch = pitch - otexclef - 7;
        }
    }
  else
    {
      if (pitch - otexclef < 97)
        {
          realpitch = pitch - otexclef - 18;
        }
      else
        {
          realpitch = pitch - otexclef;
        }
    }
  if (is_even (realpitch))
    {
      fprintf (f, "%c", realpitch);
    }
  else
    {
      fprintf (f, "%c", realpitch + 1);
    }
}
Пример #30
0
void
opustex_print_episem_under (FILE *f, char pitch, char length)
{
  int realpitch;

  if (is_even (otexclef))
    {
      if (pitch - otexclef < 104)
        {
          realpitch = pitch - otexclef - 25;
        }
      else
        {
          realpitch = pitch - otexclef - 7;
        }
    }
  else
    {
      if (pitch - otexclef < 97)
        {
          realpitch = pitch - otexclef - 18;
        }
      else
        {
          realpitch = pitch - otexclef;
        }
    }
  if (!is_even (pitch) && pitch > 'c')  // if the note is between staff lines
    {
      fprintf (f, "\\episem %c%d", realpitch - 2, length);
    }
  else
    {
      fprintf (f, "\\episem %c%d", realpitch - 1, length);
    }
}