/* Driver program to check above functions */
int main()
{
    int arr[] =   {1, 1, 0, 0, 0, 0, 0, 0};
    int n = sizeof(arr)/sizeof(arr[0]);
    std::cout<< "Count of zeroes is "<<countOnes(arr, n)<<std::endl;
    return 0;
}
Exemple #2
0
static void test_countOnesInBitMask(void)
{
    unsigned int i = 0;
    for (i = 0; i < 32; i++)
    {
        TEST_ASSERT_EQUAL_UINT32(i, countOnes(makeBitmask(i, 0)));
    }
}
Exemple #3
0
int main(void)
{
    int n, code;
    code = scanf("%d", &n);
    if (code != 1)
        return ERR_INPUT;
    printf("%d\n", countOnes(n));
    return 0;
}
Exemple #4
0
void main()
{
    //Test Variables
    int year, leap, num, num2, month, day, bitCount;
    month = 2;
    num = 22;
    num2 = 37;
    year = 2004;
    day = daysInMonth(month, year);
    bitCount = countOnes(num);
    leap = isLeap(year);

    //Testing countOnes
    printf("The number of bits in %u is %d\n", num, bitCount);

    //Testing isEven
    if (isEven(num))
    {
        printf("%d is an even number\n", num);
    }
    else
    {
        printf("%d is not an even number\n", num);
    }

    //Testing isOdd
    if (isOdd(num2))
    {
        printf("%d is an odd number\n", num2);
    }
    else
    {
        printf("%d is not an odd number\n", num2);
    }

    //Testing isLeap
    if (leap == 1)
    {
        printf("The year %d is a leap year\n",year);
    }
    else
    {
        printf("The year %d is not a leap year\n",year);
    }

    //Testing daysInMonth
    printf("Febuary has %d days in the year %d\n",day, year);

    //Testing packChars
    printf("Packing %d and %d together forms 0x%x\n", 3, 5, packChars(3,5));

    getchar();
}
Exemple #5
0
int main(void)
{
    unsigned char buffer[1024];
    unsigned int i = 0;
    unsigned int ones = 0, zeroes = 0;
    unsigned int pattern2[4] = {0,0,0,0};

    TPM_setlog(0);

    printf("Counting number of '1's and '0's of the rng.\n");
    while (i < 10) {
        uint32_t bufferSize = sizeof(buffer);
        unsigned int j = 0;
        uint32_t ret;
        ret = TPM_GetRandom(bufferSize,
                            buffer, &bufferSize);
        if (0 != ret) {
            printf("Error %s from TPM_GetRandom.\n",
                   TPM_GetErrMsg(ret));
            exit (ret);
        }
        while (j < bufferSize) {
            unsigned int c = countOnes(buffer[j]);
            ones += c;
            zeroes += (8-c);
            pattern2[0] += matchPattern(buffer[j], 0x0, 2);
            pattern2[1] += matchPattern(buffer[j], 0x1, 2);
            pattern2[2] += matchPattern(buffer[j], 0x2, 2);
            pattern2[3] += matchPattern(buffer[j], 0x3, 2);
            j++;
        }
        ret = TPM_StirRandom(buffer, 10);
        if (0 != ret) {
            printf("Error %s from TPM_StirRandom.\n",
                   TPM_GetErrMsg(ret));
            exit(ret);
        }
        i++;
    }
    printf("Percentage of '1': %d percent.\n", (ones*100)/(ones+zeroes));
    printf("Percentage of '00' bits:  %d percent\n",
           (pattern2[0]*200)/(ones+zeroes));
    printf("Percentage of '01' bits:  %d percent\n",
           (pattern2[1]*200)/(ones+zeroes));
    printf("Percentage of '10' bits:  %d percent\n",
           (pattern2[2]*200)/(ones+zeroes));
    printf("Percentage of '11' bits:  %d percent\n",
           (pattern2[3]*200)/(ones+zeroes));
    return 0;
}
int computerPlayer::play(const playfield& field) {
  if (first_round) {
    if (countOnes(field) == 0) {
      colour_of_opponent = 2;
      colour_of_player = 1;
    } else {
      colour_of_opponent = 1;
      colour_of_player = 2;
    }
    first_round = false;
  }
  weighted_cols.clear();

  for (int x = 0; x < width_of_field; ++x) {
    // Only if column isn't full.
    if (field.stoneat(x, 0) == 0) {
      int max_of_column_player {0}, max_of_column_opponent {0};

      for (int y = 0; y < height_of_field; ++y) {
        if (field.stoneat(x, y) == 0) {
          max_of_column_player = std::max(max_of_column_player,
                                          weightOfPlace(x, y, colour_of_player, field));
          max_of_column_opponent = std::max(max_of_column_opponent,
                                            weightOfPlace(x, y, colour_of_opponent, field));
        }
      }

      if (max_of_column_player >= 3 || max_of_column_opponent >= 3) return x;

      weighted_cols.push_back(
         std::make_tuple(max_of_column_player, max_of_column_opponent, x));
    }
  }

  std::random_shuffle(begin(weighted_cols), end(weighted_cols));
  
  std::stable_sort(begin(weighted_cols), end(weighted_cols),
            [](const weight &left, const weight &right)
              {return std::get<0>(left) < std::get<0>(right);});
  
  std::stable_sort(begin(weighted_cols), end(weighted_cols),
                   [](const weight &left, const weight &right)
                      {return std::get<1>(left) < std::get<1>(right);});

  return std::get<2>(weighted_cols.back());
}
Exemple #7
0
/* internal function! */
static UEnumeration *selectForMask(const UConverterSelector* sel,
                                   uint32_t *mask, UErrorCode *status) {
  // this is the context we will use. Store a table of indices to which
  // encodings are legit.
  struct Enumerator* result = (Enumerator*)uprv_malloc(sizeof(Enumerator));
  if (result == NULL) {
    uprv_free(mask);
    *status = U_MEMORY_ALLOCATION_ERROR;
    return NULL;
  }
  result->index = NULL;  // this will be allocated later!
  result->length = result->cur = 0;
  result->sel = sel;

  UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
  if (en == NULL) {
    // TODO(markus): Combine Enumerator and UEnumeration into one struct.
    uprv_free(mask);
    uprv_free(result);
    *status = U_MEMORY_ALLOCATION_ERROR;
    return NULL;
  }
  memcpy(en, &defaultEncodings, sizeof(UEnumeration));
  en->context = result;

  int32_t columns = (sel->encodingsCount+31)/32;
  int16_t numOnes = countOnes(mask, columns);
  // now, we know the exact space we need for index
  if (numOnes > 0) {
    result->index = (int16_t*) uprv_malloc(numOnes * sizeof(int16_t));

    int32_t i, j;
    int16_t k = 0;
    for (j = 0 ; j < columns; j++) {
      uint32_t v = mask[j];
      for (i = 0 ; i < 32 && k < sel->encodingsCount; i++, k++) {
        if ((v & 1) != 0) {
          result->index[result->length++] = k;
        }
        v >>= 1;
      }
    }
  } //otherwise, index will remain NULL (and will never be touched by
Exemple #8
0
int main(void) {
	int t;
	scanf("%d", &t);
	while (t--) {
		memset(Data, 0, sizeof(Data));
		Finished = 0;
		MaxK = 0;
		nResult = 0;
		BuildData();
		LoadPuzzle();
		struct str_node *r;
		for (r = RootNode->Right; r != RootNode; r = r->Right)
			r->count = countOnes(r);
		Search(0);
		if (t > 0)
			printf("\n");
	}
	return 0;
}
Exemple #9
0
static void test_countOnes(void)
{
    TEST_ASSERT_EQUAL_UINT32(0, countOnes(0x0));
    TEST_ASSERT_EQUAL_UINT32(32, countOnes(0xffffffff));
    TEST_ASSERT_EQUAL_UINT32(16, countOnes(0x5a5a5a5a));
}
Exemple #10
0
void  main(int argc, char* argv[]) { //二进制数位1计数算法测试入口
   for (unsigned int i = 0; i < 512; i++)
      printf("%6d = %4X: %6d %6d %6d\n", i, i, countOnes(i), countOnes1(i), countOnes2(i));
}
Exemple #11
0
int main()
{
	printf("countOnes(0x30303)=%d\n",
	       countOnes(0x30303));
}
/*
 * Create a new string with human-readable access flags.
 *
 * In the base language the access_flags fields are type u2; in Dalvik
 * they're u4.
 */
static char* createAccessFlagStr(u4 flags, AccessFor forWhat)
{
#define NUM_FLAGS   18
    static const char* kAccessStrings[kAccessForMAX][NUM_FLAGS] = {
        {   
            /* class, inner class */
            "PUBLIC",           /* 0x0001 */
            "PRIVATE",          /* 0x0002 */
            "PROTECTED",        /* 0x0004 */
            "STATIC",           /* 0x0008 */
            "FINAL",            /* 0x0010 */
            "?",                /* 0x0020 */
            "?",                /* 0x0040 */
            "?",                /* 0x0080 */
            "?",                /* 0x0100 */
            "INTERFACE",        /* 0x0200 */
            "ABSTRACT",         /* 0x0400 */
            "?",                /* 0x0800 */
            "SYNTHETIC",        /* 0x1000 */
            "ANNOTATION",       /* 0x2000 */
            "ENUM",             /* 0x4000 */
            "?",                /* 0x8000 */
            "VERIFIED",         /* 0x10000 */
            "OPTIMIZED",        /* 0x20000 */
        },
        {
            /* method */
            "PUBLIC",           /* 0x0001 */
            "PRIVATE",          /* 0x0002 */
            "PROTECTED",        /* 0x0004 */
            "STATIC",           /* 0x0008 */
            "FINAL",            /* 0x0010 */
            "SYNCHRONIZED",     /* 0x0020 */
            "BRIDGE",           /* 0x0040 */
            "VARARGS",          /* 0x0080 */
            "NATIVE",           /* 0x0100 */
            "?",                /* 0x0200 */
            "ABSTRACT",         /* 0x0400 */
            "STRICT",           /* 0x0800 */
            "SYNTHETIC",        /* 0x1000 */
            "?",                /* 0x2000 */
            "?",                /* 0x4000 */
            "MIRANDA",          /* 0x8000 */
            "CONSTRUCTOR",      /* 0x10000 */
            "DECLARED_SYNCHRONIZED", /* 0x20000 */
        },
        {
            /* field */
            "PUBLIC",           /* 0x0001 */
            "PRIVATE",          /* 0x0002 */
            "PROTECTED",        /* 0x0004 */
            "STATIC",           /* 0x0008 */
            "FINAL",            /* 0x0010 */
            "?",                /* 0x0020 */
            "VOLATILE",         /* 0x0040 */
            "TRANSIENT",        /* 0x0080 */
            "?",                /* 0x0100 */
            "?",                /* 0x0200 */
            "?",                /* 0x0400 */
            "?",                /* 0x0800 */
            "SYNTHETIC",        /* 0x1000 */
            "?",                /* 0x2000 */
            "ENUM",             /* 0x4000 */
            "?",                /* 0x8000 */
            "?",                /* 0x10000 */
            "?",                /* 0x20000 */
        },
    };
    const int kLongest = 21;        /* strlen of longest string above */
    int i, count;
    char* str;
    char* cp;

    /*
     * Allocate enough storage to hold the expected number of strings,
     * plus a space between each.  We over-allocate, using the longest
     * string above as the base metric.
     */
    count = countOnes(flags);
    cp = str = (char*) malloc(count * (kLongest+1) +1);

    for (i = 0; i < NUM_FLAGS; i++) {
        if (flags & 0x01) {
            const char* accessStr = kAccessStrings[forWhat][i];
            int len = strlen(accessStr);
            if (cp != str)
                *cp++ = ' ';

            memcpy(cp, accessStr, len);
            cp += len;
        }
        flags >>= 1;
    }
    *cp = '\0';

    return str;
}