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

前端 未结 7 956
不知归路
不知归路 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.

    0 讨论(0)
  • 2021-02-10 23:36

    In our everyday decimal system, the base number, or radix is 10. A number system's radix tells us how many different digits are in use. In decimal system we use digits 0 through 9.

    The significance of a digit is radix ^ i, where i is digit's position counting from right, starting at zero.

    Decimal number 6789 broken down:

     6  7  8  9              radix ^ i
     |  |  |  |             --------------
     |  |  |  +-- ones       10 ^ 0 = 1
     |  |  +----- tens       10 ^ 1 = 10
     |  +-------- hundreds   10 ^ 2 = 100
     +----------- thousands  10 ^ 3 = 1000
    
      ones      tens       hundreds    thousands
      -----------------------------------------------
      (9 * 1) + (8 * 10) + (7 * 100) + (6 * 1000)
    = 9       + 80       + 700       + 6000
    = 6789
    

    This scheme will help us understand any number system in terms of decimal numbers.


    Hexadecimal system's radix is 16, so we need to employ additional digits A...F to denote 10...15. Let's break down hexadecimal number CDEFh in a similar fashion:

     C  D  E  F              radix ^ i
     |  |  |  |             --------------
     |  |  |  +-- ones       16 ^ 0 = 1
     |  |  +----- sixteens   16 ^ 1 = 16
     |  +-------- 256:s      16 ^ 2 = 256
     +----------- 4096:s     16 ^ 3 = 4096
    
      ones       sixteens    256:s        4096:s
      -----------------------------------------------
      (Fh * 1) + (Eh * 16) + (Dh * 256) + (Ch * 4096)
    = (15 * 1) + (14 * 16) + (13 * 256) + (12 * 4096)
    = 15       + 224       + 3328       + 49152
    = 52719
    

    We have just converted the number CDEFh to decimal (i.e. switched base 16 to base 10).


    In binary system, the radix is 2, so only digits 0 and 1 are used. Here is the conversion of binary number 1010b to decimal:

     1  0  1  0              radix ^ i
     |  |  |  |             --------------
     |  |  |  +-- ones       2 ^ 0 = 1
     |  |  +----- twos       2 ^ 1 = 2
     |  +-------- fours      2 ^ 2 = 4
     +----------- eights     2 ^ 3 = 8
    
      ones      twos      fours     eights
      -----------------------------------------------
      (0 * 1) + (1 * 2) + (0 * 4) + (1 * 8)
    = 0       + 2       + 0       + 8
    = 10
    

    Octal system - same thing, radix is 8, digits 0...7 are in use. Converting octal 04567 to decimal:

     4  5  6  7              radix ^ i
     |  |  |  |             --------------
     |  |  |  +-- ones       8 ^ 0 = 1
     |  |  +----- eights     8 ^ 1 = 8
     |  +-------- 64:s       8 ^ 2 = 64
     +----------- 512:s      8 ^ 3 = 512
    
      ones      eights    64:s       512:s
      -----------------------------------------------
      (7 * 1) + (6 * 8) + (5 * 64) + (4 * 512)
    = 7       + 48      + 320      + 2048
    = 2423
    

    So, to do a conversion between number systems is to simply change the radix.

    To learn about bitwise operators, see http://www.eskimo.com/~scs/cclass/int/sx4ab.html.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-10 23:41

    Decimal, hexadecimal, octal, binary and multi-radix converter

    Maybe playing with this inline snippet could help...

    This little javascript snippet could convert in all ways:

    You can type any valid entry in one of the input field, they will be instantly converted into each other fields.

    Enter for sample 10 or 100, successively in each fields below (or try to enter 1767707668033969 in integer field ;) ...

    function doChange(ent) {
      var val=ent.target.value;
      if      (ent.target.id == 'hex') val=parseInt(val, 16);
      else if (ent.target.id == 'oct') val=parseInt(val,  8);
      else if (ent.target.id == 'bin') val=parseInt(val,  2);
      else if (ent.target.id == 'sel') val=parseInt(val, 
          document.getElementById('radix').value);
      document.getElementById('int').value=(val*1).toString(10);
      document.getElementById('hex').value=(val*1).toString(16).toUpperCase();
      document.getElementById('oct').value=(val*1).toString( 8);
      document.getElementById('bin').value=(val*1).toString( 2);
      document.getElementById('sel').value=(val*1).toString(
          document.getElementById('radix').value).toUpperCase();
    }
    function selRadix(ent) {
        var radix=ent.target.value;
        document.getElementById('sel').value=
            (1*document.getElementById('int').value).
               toString(radix).toUpperCase();
    }
    function wStart() {
        var ent=document.getElementsByTagName('input');
        for (var i=0;i<ent.length;i++) {
          ent[i].addEventListener('keyup',doChange);
          ent[i].addEventListener('change',doChange);
          };
        ent=document.getElementById('radix');
        for (i=2;i<36;i++) ent.innerHTML+="<option>"+i+"</option>";
        ent.innerHTML+='<option selected="true">36</option>';
        ent.addEventListener('change',selRadix);
    }
    setTimeout(wStart,300);
    body {  font-family: sans; font-size: .8em; margin: 0pt; padding:1% }
    input#int { width: 12%;  }
    input#hex { width: 10%;  }
    input#oct { width: 18%;  }
    input#bin, input#sel { width: 32%;  }
    <div>
      Int<input id="int"></input>
      Hex<input id="hex"></input>
      Oct<input id="oct"></input>
      Bin<input id="bin"></input>
      <hr />
      Radix: <select id="radix"></select>
      <input id="sel"></input>
    </div>

    0 讨论(0)
  • 2021-02-10 23:42

    Learning to convert number bases (also known as radixes) is much easier with a radix conversion tool that does all the hard work for you.

    That way you can learn quickly by converting a bunch of numbers to and from different radixes, and see right away the result of the conversion.

    Use this radix converter -- http://www.sooeet.com/math/base-converter.php

    to convert a list of decimal numbers to binary, octal, and hexadecimal (one number at a time).

    Here are two lists of decimal numbers to get you started:

    1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536

    0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535

    The two lists look similar, but produce very different results when you convert them to binary, octal, and hexadecimal. Try it and see.

    To use this number base converter, type a number, or copy and paste any number from the above lists, into the "Base-10" box, and press Enter or Return on your keyboard. The number you entered is converted to binary (base-2), octal (base-8), and hexadecimal (base-16), plus many other number bases (radixes), from base-2 and base-36.

    If you want to better understand radix conversion, read the help pop-ups next to each radix box, to learn about the internal workings of each radix.

    Now, try changing the binary, octal, and hex numbers that you got from converting the above lists, by replacing binary, octal, or hex "digits".

    For example: Decimal 15 = binary 1111

    Now, in the binary result (1111), replace any of the 1 binary digits (bits), with a zero (0) and press Enter or Return on your keyboard.

    In this example: Binary 1101 = decimal 13

    You can see that the second bit from the right in a binary number has a weight of 2 decimal.

    Keep experimenting like this, with decimal, binary, octal, and hexadecimal number conversions, and you will soon master the subject.

    0 讨论(0)
  • 2021-02-10 23:45

    This: http://members.tripod.com/numeric_systems/ seems like a good start.
    By the way, there's information about that everywhere, you just have to look for it.

    0 讨论(0)
提交回复
热议问题