Hey, /wsr/. I'm working on an assingment for my CS class, and I've hit a snag on it. I'm supposed to use bitshifting for an Arduino to find different base values for a given dec number. I found the bin fine, and I found a crude way to find hex values, but I can't get negative values correct for octal conversion which tells me that my ghetto rigged hex code is also wrong somewhere.
Not sure if /wsr/ has a code option like /g/ does, so I've just screencapped the part of the assignment that I'm needing
>>375783
They way you are probably meant to do this is not by converting dec->hex / dec->oct directly but by going dec->bin->hex and dec->bin->oct. Since Hex is just 4bytes and octal 3 bytes you can easily convert it to from binary.
Anyway that how I did it for a completely different course and language.
>>375797
bits not bytes sorry.
>>375797
That is what we're supposed to do, but I can't figure out a code to take the certain number of bits at a time even with googling since most places will just tell you to use the built in oct/hex functions. Arduino is essentially C++, so if you have any knowledge of that, it would help
Just copy the input in a temporary variable and use repeated modulo and division by the base.
For powers of two you can replace this by masking the bits and shifting, respectlively.
Problem: You get the digits in reverse order (from right to left), but since you are storing the string instead of directly outputting this should be no problem.
>negative numbers
It depends if you need to display the stored bits or a mathematically correct number.
So -5 = -101 in base 2, but it's usually stored differently (e.g. 1...11111011 as two's complement).