Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Hello
//clang 3.8.0 #include <stdio.h> #include <stdlib.h> #include <string.h> /** * parameters: szBin const array of chars already containing the mask * to be printed in (with or without leading/ trailing/ separating blanks) * len is the string lenght of the szBin (without evtl. terminating zero) * and x is the unsigned number to be expressed in binary '1' and '0'-s * return value: the input integer to be expressed * no error outputs * note: every characters in input other than '0' is considered blank * and will be skipped */ int int2bin(char* const szBin, size_t len, const int x); /** * parameters: szBin is the input string; a sequence of '1's and '0's * (incl. trailing/ leading/ separating blanks) expressing a binary * number to be expressed as unsigned integer, len is the length of * the szBin (without terminating zero) and x is the offset added to * the return value - usually 0 * note: every other character than '1' will be interpreted as '0' except * blanks which will be skipped */ int bin2int(const char* const szBin, size_t len, int x); /** * */ int main(void) { char* szBin = (char*)malloc(18); int i; for(i=0x0000; i<0x010; i++) { memcpy(szBin, " 0000 0000 0000 0000\0", 20); int2bin(szBin, 20, 1<<i); printf("%04x\t%s\t%d\n", 1<<i, szBin, bin2int(szBin, 20, 0)); } memcpy(szBin, " 0000 0000 0000 0000\0", 20); int2bin(szBin, 20, 2289); printf("%04x\t%s\t%d\n", 2289, szBin, bin2int(szBin, 20, 0)); free(szBin); return 0; } int int2bin(char* const szBin, size_t len, const int x) { size_t i = 0; char* szi = szBin+len; while(szi-->szBin) { if('0'==*szi) { if((0!=(x&1<<i))) *szi = '1'; i++; } } return x; } int bin2int(const char* const szBin, size_t len, int x) { size_t i = 0; const char* szi = szBin+len; while(szi-->szBin) { if('1'==*szi) { x+=(1<<i); } if(' '!=*szi) i++; } return x; }
run
|
edit
|
history
|
help
2
Bucles: Múltiplos de 4 hasta N tecleado por el usuario
lab7
Bucles: Media 10 positivos y negativos tecleados
Bucles: triángulo asteriscos
Pointer_Indirectare_N
UDP - Pytagorova veta
interestCalculationSample
lab7_OOP 0.3 alha
Herout (67) - 12
Hello World C