/* * bigv_bitcount: * -------------- * Function: Count the number of bits necessary to code the bigvalues region. */ int bigv_bitcount(int ix[GRANULE_SIZE], gr_info *gi) { int bits = 0; unsigned int table; if( (table=gi->table_select[0])) /* region0 */ bits += count_bit(ix, 0, gi->address1, table ); if( (table=gi->table_select[1])) /* region1 */ bits += count_bit(ix, gi->address1, gi->address2, table ); if( (table=gi->table_select[2])) /* region2 */ bits += count_bit(ix, gi->address2, gi->address3, table ); return bits; }
vector<string> readBinaryWatch(int num) { vector<string> result; string temp_str; char temp[6]; for (unsigned char i = 0; i < 12; ++i) { for (unsigned char j = 0; j < 60; ++j) { if (count_bit(i) + count_bit(j) == num) { sprintf(temp, "%d:%02d", i, j); temp_str = temp; result.push_back(temp_str); } } } return result; }
int main() { char input_num = 0xa; printf("%d\n,",count_bit(input_num)); /*调用函数计算input_num中为1的位的个数*/ return 0; }
const char *flag_string_init( struct flag_type *flag_table, long bits ) { static char buf[512]; int countBit = count_bit( bits ); int countWord = 0; buf[0] = '\0'; for (int flag = 0; flag_table[flag].name != NULL; flag++) { if ( !is_stat_init( flag_table ) && IS_SET(bits, flag_table[flag].bit) ) { strcat( buf, " " ); strcat( buf, flag_table[flag].name ); countWord++; } else if ( flag_table[flag].bit == bits ) { strcat( buf, " " ); strcat( buf, flag_table[flag].name ); countWord = countBit; // artifically set countWord to avoid entering bug test break; } } if ( countWord != countBit ) bug("Flag_String_Init: invalid number of word: %d [%s], number of bits %d [flag = %ld (%s)] [table: %s]", countWord, buf[0] != 0 ? buf+1: "none", countBit, bits, convert_flag( bits), get_flag_table_name(flag_table) ); return (buf[0] != '\0') ? buf+1 : "none"; }
int main(int argc, const char *argv[]) { int i=0x11111111; int pos=2; printf("%d\n",count_bit(i)); printf("%d\n",get_bit(i,pos)); return 0; }
// Convert a flag into a string each word inside '' and separated by , const char *list_flag_string( const long flag, struct flag_type *flag_table, const char *quote, const char *separator ) { char buf2[MAX_STRING_LENGTH]; static char buf[MAX_STRING_LENGTH]; strcpy( buf2, flag_string( flag_table, flag ) ); if ( !str_cmp( buf2, "none") ) return str_dup( quotify("none",quote) ); int nb_space = 1; // if not bitvector -> only 1 value if ( !is_stat( flag_table ) ) // if bitvector -> count bit nb_space = count_bit( flag ); // number of words // log_stringf("table: %s nb_space: %d buf2: %s flag: %ld", get_flag_table_name(flag_table), nb_space, buf2, flag ); buf[0] = '\0'; char *s = buf2; char* tok = strsep(&s, " " ); int i = 0; while ( tok != NULL ) { if ( tok[0] != '\0' ) { //strcat( buf, bracket); //strcat( buf, tok ); //strcat( buf, bracket); strcat( buf, quotify( tok, quote ) ); if ( i < nb_space-1 ) // && tok != NULL && tok[0] != NULL ) strcat( buf, separator ); i++; } tok = strsep(&s, " " ); } if ( i != nb_space && !is_stat(flag_table) ) { bug("List_Flag_String: invalid number of word: %d, number of bits %d [flag = %ld] [table: %s]", i, nb_space, flag, get_flag_table_name(flag_table) ); buf[strlen(buf)-strlen(separator)] = '\0'; } return str_dup(buf); }
/* * new_choose_table: * ----------------- * Choose the Huffman table that will encode ix[begin..end] with * the fewest bits. * Note: This code contains knowledge about the sizes and characteristics * of the Huffman tables as defined in the IS (Table B.7), and will not work * with any arbitrary tables. */ int new_choose_table( int ix[GRANULE_SIZE], unsigned int begin, unsigned int end ) { int i, max; int choice[2]; int sum[2]; max = ix_max(ix,begin,end); if(!max) return 0; choice[0] = 0; choice[1] = 0; if(max<15) { /* try tables with no linbits */ for ( i =14; i--; ) if ( shine_huffman_table[i].xlen > max ) { choice[0] = i; break; } sum[0] = count_bit( ix, begin, end, choice[0] ); switch (choice[0]) { case 2: sum[1] = count_bit( ix, begin, end, 3 ); if ( sum[1] <= sum[0] ) choice[0] = 3; break; case 5: sum[1] = count_bit( ix, begin, end, 6 ); if ( sum[1] <= sum[0] ) choice[0] = 6; break; case 7: sum[1] = count_bit( ix, begin, end, 8 ); if ( sum[1] <= sum[0] ) { choice[0] = 8; sum[0] = sum[1]; } sum[1] = count_bit( ix, begin, end, 9 ); if ( sum[1] <= sum[0] ) choice[0] = 9; break; case 10: sum[1] = count_bit( ix, begin, end, 11 ); if ( sum[1] <= sum[0] ) { choice[0] = 11; sum[0] = sum[1]; } sum[1] = count_bit( ix, begin, end, 12 ); if ( sum[1] <= sum[0] ) choice[0] = 12; break; case 13: sum[1] = count_bit( ix, begin, end, 15 ); if ( sum[1] <= sum[0] ) choice[0] = 15; break; } } else { /* try tables with linbits */ max -= 15; for(i=15;i<24;i++) if(shine_huffman_table[i].linmax>=max) { choice[0] = i; break; } for(i=24;i<32;i++) if(shine_huffman_table[i].linmax>=max) { choice[1] = i; break; } sum[0] = count_bit(ix,begin,end,choice[0]); sum[1] = count_bit(ix,begin,end,choice[1]); if (sum[1]<sum[0]) choice[0] = choice[1]; } return choice[0]; }