|
发表于 2019-3-31 00:54:57
|
显示全部楼层
本帖最后由 jackz007 于 2019-3-31 00:57 编辑
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char * dec2bin(char * bin , const int n , const int m)
- {
- char c ;
- int d , k ;
- if(n > 0) {
- c = n % 2 ;
- if(c) c = '1' ;
- else c = '0' ;
- dec2bin(bin , n / 2 , m + 1) ;
- for(k = 0 ; k < 32 && bin[k] ; k ++) ;
- bin[k ++] = c ;
- bin[k] = '\0' ;
- } else {
- bin[0] = '\0' ;
- }
- if(! m) {
- d = strlen(bin) ;
- if(d < 32) {
- for(k = 0 ; k < d ; k ++) bin[32 - 1 - k] = bin[d - 1 - k] ;
- for(k = 0 ; k < 32 - d ; k ++) bin[k] = '0' ;
- bin[32] = '\0' ;
- }
- }
- return bin ;
- }
- void plus(char b[])
- {
- int k ;
- k = 32 ;
- do {
- if(b[k - 1] == '1') {
- b[k - 1] = '0' ;
- } else {
- b[k - 1] = '1' ;
- break ;
- }
- k -- ;
- } while(k > 0) ;
- }
复制代码
运行实况:
C:\Bin\Exercise\C>plus2
2147483647
2147483647
01111111111111111111111111111111
10000000000000000000000000000000
C:\Bin\Exercise\C> |
|