HELP! I don't know binary, hexadecimal, octal, and bit-wise

前端 未结 7 982
不知归路
不知归路 2021-02-10 23:25

I haven\'t learned this in programming class before, but now I need to know it. What are some good resources for learning these numbers and how to convert them? I pretty much am

7条回答
  •  悲哀的现实
    2021-02-10 23:29

    Converting between binary, octal, and hex is pretty easy.

    binary <=> octal: three binary digits <=> one octal digit
    binary <=> hex:   four binary digits <=> one hex digit
    octal <=> hex:    four octal digits <=> three hex digits
                      (by way of binary, if necessary)
    

    These are easy because the radixes for binary, octal, and hex are all powers of 2. The trick is going between decimal and the other three, because 10 (the radix for decimal) has that pesky factor of 5.

    Several other answers show how to convert from binary, octal, and hex to decimal. The algorithm I was taught to go from decimal to another radix is to continually divide by the radix and read off the remainders as the answer going right to left. For instance, here's how to express 227 in hex:

     n   n / 16  remainder
    ---  ------  ---------
    227    14     3
     14     0    14 (=E)
    

    so the answer is E3.

提交回复
热议问题