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

前端 未结 7 1239
不知归路
不知归路 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:39

    Use Google:

    http://www.google.com/search?q=0b11110000+to+hex
    http://www.google.com/search?q=0b11110000+to+decimal
    http://www.google.com/search?q=0b11110000+to+octal
    http://www.google.com/search?q=4232+to+binary
    http://www.google.com/search?q=4232+to+hex
    http://www.google.com/search?q=4232+to+octal
    http://www.google.com/search?q=0xaf0e23+to+decimal
    http://www.google.com/search?q=0xaf0e23+to+binary
    http://www.google.com/search?q=0xaf0e23+to+octal
    

    The fundamental concept of number systems is this: a number is the sum of each of its digits times its base raised to the power of the position of the number.

    Hex, decimal, octal, and binary are all "bases" of number systems, but they count the same thing. You know decimal already, so it is the easiest to explain:

    4232 = 4 * 10^3 + 2 * 10^2 + 3 * 10^1 + 2 * 10^0
    3210 <- the base that you raise each of the above digits to
    

    This exact principle applies to every base system.

    Binary:

    0b11110000 = 1 * 2^7 + 1 * 2^6 + 1 * 2^5 + 1 * 2^4 + 0 * 2^3 + 0 * 2^2 + 0 * 2^1 + 0 * 2^0
      76543210 <- the base that you raise each of the above digits to
    

    Hexadecimal (hex):

    0xaf0e23 = 10 * 16^5 + 15 * 16^4 + 0 * 16^3 + 14 * 16^2 + 2 * 16^1 + 3 * 16^0
      543210 <- the base that you raise each of the above digits to
    

    Hex is really the only common base that isn't intuitively obvious, because it uses alpha characters to describe the values 10,11,12,13,14, and 15, using the letters a,b,c,d,e, and f respectively, instead.

    We use binary, octal, and hex because binary is the language of computers (remember, a digital wire can either have current or not have current (values 1 or 0)). Each single hex character describes four binary digits exactly, while an octal character describes 3 binary digits. Hex is used much more often than octal.

    0b0000 = 0x0 = 0
    0b0001 = 0x1 = 1
    0b0010 = 0x2 = 2
    0b0011 = 0x3 = 3
    0b0100 = 0x4 = 4
    0b0101 = 0x5 = 5
    0b0110 = 0x6 = 6
    0b0111 = 0x7 = 7(this is as far as octal goes)
    0b1000 = 0x8 = 8
    0b1001 = 0x9 = 9
    0b1010 = 0xa = 10
    0b1011 = 0xb = 11
    0b1100 = 0xc = 12
    0b1101 = 0xd = 13
    0b1110 = 0xe = 14
    0b1111 = 0xf = 15
    

    Capitalization of hexadecimal numbers isn't important. The most important thing for you to memorize in terms of number systems is the above table. You should usually use Google when converting long hexadecimal or binary numbers to decimal, but if you know the table above then you won't need Google for many short strings.

    As an exercise, I also recommend that you write conversion methods in the language of your choice to convert from one base system to another. They are simple iterators and will help solidify the concepts in your head. I love writing them like this: decimal_ot_binary(binarynum) instead of binary_to_decimal(binarynum). Then you can nest them sensically: int x = decimal_ot_hex(hex_ot_binary(binary_ot_decimal(40001)));

    Now, any time you see a hex number in the form 0x????? you'll know that it is simply a representation for a string of binary digits. Just convert each character in the hex to the corresponding binary digit, as above.

提交回复
热议问题