![]() The following make use of a table that stored all the reversed value of each byte, table = reversed_byte, and reverse the 4 bytes of the unsigned integer. swap bits at index i and 31-i for i between 0-15 Going over from bits 0 to 15 (31/2), swap bit i with bit (31-i). I wrote this with an approach similar to string reverse in c. Here's a golang version of reverse bits in an integer, if anyone is looking for one. The comparison values can be generated by dividing by 2, which means right-shifting by 1 bit.)Īny of these solutions might need a hack to prevent the value 0 from displaying as nothing (a.k.a. (These divisions can be accomplished using comparisons. Display the quotients (0,0,1,1,1,1,0,1,1) in the same order, skipping any leading first zeros, to display "1111011".Finally, divide 1 by 1, yielding 1 remainder 0.Divide 11 by 8, yielding 1 remainder 3.Divide 27 by 16, yielding 1 remainder 11. ![]() Divide 59 by 32, yielding 1 remainder 27.Divide 123 by 64, yielding 1 remainder 59.Divide 123 by 128, yielding 0 remainder 123.Divide 123 by 256, yielding 0 remainder 123.The second method yields the digits (bits) in order. (Dividing by 2 can be accomplished by right-shifting by 1 bit.) Finally, divide 1 by 2, yielding 0 remainder 1.Divide 15 by 2, yielding 7 remainder 1.Divide 30 by 2, yielding 15 remainder 0.Divide 61 by 2, yielding 30 remainder 1.Divide 123 by 2, yielding 61 remainder 1.To display the value 123 as base-2, using the first method: You have to start with a large-enough divisor. You could output the digits in order as they are computed. Display the quotients (0, 1, 2, 3) in the same order, skipping the leading zeros, to display "123".Finally, divide 3 by 1, yielding 3 remainder 0. ![]() Divide 23 by 10, yielding 2 remainder 3.Divide 123 by 100, yielding 1 remainder 23.Divide 123 by 1000, yielding 0 remainder 123.With this approach, you don't know how many digits are needed until you compute the most-significant digit, so you can't output the first digit until you have computed all of them.Īlternatively, you can compute the most-significant digit first. ![]() We could put any number of zeros before the 123, but that is not proper, because they do not contribute anything.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |