The values passed on the command line are strings. Inside the program, you may treat the strings in any manner you choose.
If you need to get a single bit from 0..15 (or 1..16) set, you'd be better off taking the decimal number of the bit than forcing the user to do the counting. You simply take argv[2]
or whatever index contains the number, convert it using a function such as atoi()
or strtol()
into a number bitnum
(error check the value for negative or large values), and then use 1U << bitnum
to get the value in a 16-bit unsigned short
, or equivalent.
If the user can specify several registers in one command invocation, then binary is more reasonable (though not necessarily wholly reasonable), but do consider the merits of hex over binary (one hex digit controls the value of four bits).
If you insist on using binary, then strtol()
is the function to use; it will convert a string as binary (char *eos; int bitnum = strtol(argv[2], &eos, 2);
— and then check the conversion carefully; you need to set errno
to 0 before the conversion, and look hard at the return values and where eos
points).