arrays - Convert Decimal to Binary in c++ with correct Bit Size -
i writing program takes in numbers entered user , stores them array. program convert values decimal , store in new array. having trouble converting binary value correct bit size.
for example user enters 3
, 4
. program stores , converts them binary resulting in 11
, 100
. how can store 011
, 100
?
i believe i'll need convert char array or string of sort have no idea steps should follow.
i think you’re trying this:
void convert(int number, char *array, int bits) { int bit; (bit = 0; bit < bits; bit++) { if ((number & (1 << (bits - (bit + 1)))) > 0) { array[bit] = '1'; } else { array[bit] = '0'; } } }
Comments
Post a Comment