/ * BEGIN bitstr.c * / #include< stdio.h> #include< limits.h> #define E_TYPE浮动 #define STRING" %s =%f \ n" typedef E_TYPE e_type; char * bitstr(char *,void const *,size_t) ; int main(无效) { e_type e,d; char ebits [CHAR_BIT * sizeof e + 1],* s; s = STRING; for(e = 0.2f; 0.75> e; e + = 0.125){ bitstr(ebits,& e,sizeof e); printf(s,ebits,e); } for(d = 2; 20000> d; d * = 2){ for(e = d - 1; 0.75> e - d; e + = 0.5){ bitstr(ebits,& e,sizeof e); printf(s,ebits,e); } } 返回0; } char * bitstr(char * str,const void * obj,size_t n) { unsigned char mask; const unsigned char * byte = obj; char * const ptr = str; while(n--!= 0){ mask =((unsigned char)-1>> ; 1)+ 1; do { * str ++ =(char)(mask& amp; ; byte [n]? ''1'':''0''); mask>> = 1; } while(mask!= 0); } * str =''\ 0''; 返回ptr; } / * END bitstr.c * / - pete Hi, can somebody help me out with a better version of the followingfunctions which only convert decimal integer to it''s correspondingbinary form. theproblem i''m having now is that I can''t figure out how to handle when 0ispassed as parameter in only one function, my code below have to addone more function to handle this situation, any help is appreciated,thanx.char *Dec2Bin(const int decimal) { char *binary=new char[64];int dividend,i; dividend=decimal;for(i=0;dividend!=0;++i) {binary[i]=(dividend&1)+''0''; // (a % b) == (a & (b-1))dividend>>=1;}binary[i]=''\0''; return strrev(binary);} char *Decimal2Binary(const int decimal) {return (decimal==0)?"0":Dec2Bin(decimal);} 解决方案 There is no ''new'' in C. Please repost to the appropriate newsgroup. It seemsthat C is not your langage. If you insist in writing your code in C, please change it so that it uses Cfunctions and instructions. ---ed- em**********@noos.fr [remove YOURBRA before answering me]The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlC-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cppFAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ There is no ''new'' in C. It''s a perfectly valid identifier name (albeit used in a rather strange andsyntactically erroneous manner in the code you quote here). In fact, Ioften use it myself in code such as: FOO *prefix_CreateFoo(args...){FOO *new = malloc(sizeof *new);if(new != NULL){...set up valid FOO instance...}return new;} --Richard Heathfield : bi****@eton.powernet.co.uk"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.C FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlK&R answers, C books, etc: http://users.powernet.co.uk/eton /* BEGIN bitstr.c */ #include <stdio.h>#include <limits.h> #define E_TYPE float#define STRING " %s = %f\n" typedef E_TYPE e_type; char *bitstr(char *, void const *, size_t); int main(void){e_type e, d;char ebits[CHAR_BIT * sizeof e + 1], *s; s = STRING;for (e = 0.2f; 0.75 > e; e += 0.125) {bitstr(ebits, &e, sizeof e);printf(s, ebits, e);}for (d = 2; 20000 > d; d *= 2) {for (e = d - 1; 0.75 > e - d; e += 0.5) {bitstr(ebits, &e, sizeof e);printf(s, ebits, e);}}return 0;} char *bitstr(char *str, const void *obj, size_t n){unsigned char mask;const unsigned char *byte = obj;char *const ptr = str; while (n-- != 0) {mask = ((unsigned char)-1 >> 1) + 1;do {*str++ = (char)(mask & byte[n] ? ''1'' : ''0'');mask >>= 1;} while (mask != 0);}*str = ''\0'';return ptr;} /* END bitstr.c */--pete 这篇关于想要更好的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 02:26