help with basic decimal to binary converter
| kiltux (1) | |||
| oke here is the problem. i have made a simple decimal to binary converter but the binary is standing in reverse order, how can i do it correct?
| |||
| naja (18) | |||
| Hi, the algorithm you use for the conversion just gets the most significant bit first. save them in a c++ string; otherwise, you could do some low level magic with bitwise operators to reverse your number first, and then output it. Im not going to write that for you, but see this site and google is your friend: http://graphics.stanford.edu/%7Eseander/bithacks.html#BitReverseObvious There is also a book called hackers delight, which is great if you want to wet your appetite on bitwise operations... this is what i would come up with:
One strange thing is that input of -1 gives a value of 2052. This is trough some peculiarity of how cin interpretes "-1" to ushort... I don't have a clue where the value 2052 comes from though. | |||
This topic is archived - New replies not allowed.
