/** * Read bits from a number into a string * The string is limited to 0 and 1 * * @author gtowell * Created: April 2021 * **/ #include #include /** * readbits from a char using bit shifting, starting with the * most significant bit. * * @param xx -- the integer to be read * @param rtn -- a stirng into which to write the bits * **/ void pbBITSUP(int xx, char rtn[]) { unsigned int val = 1 << ((8 * sizeof(int)) - 1); for (int i = 0; i < 8 * sizeof(int); i++) { rtn[i] = '0' + ((xx&val)?1:0); //printf("bit num=%1d value=%1d\n", i, xx % 2); xx <<= 1; } } int main(int argc, char const *argv[]) { char aa[8*sizeof(int)+1]; aa[8 * sizeof(int) + 1] = '\0'; aa[8 * sizeof(int)] = '\0'; for (int i = 1; i < argc; i++) { pbBITSUP(atoi(argv[i]), aa); printf("BITS:%s\n", aa); } return 0; }