Advertisement
Guest User

Hatter on Hex & Bitwise Basics

a guest
Aug 17th, 2011
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. Binary, Hex and Basic Commands – By Hatter
  2.  
  3. 1.0 - Introduction to Binary
  4.  
  5. Binary math and hex math have slight differences to decimal math but the same principles apply. For example, in the decimal number 2481, the ‘1’ is in the “ones” placeholder, the ‘8’ is in the “tens” placeholder, the ‘4’ in the “hundreds” placeholder and the ‘2’ in the “thousands” placeholder.
  6.  
  7. Example 1: Decimal number 2481
  8. >----------------------------------------------------------------<
  9. | Thousands(1x10^3)| Hundreds(1x10^2)| Tens(1x10^1)| Ones(1x10^0)|
  10. >------------------+-----------------+-------------+-------------<
  11. | 2 | 4 | 8 | 1 |
  12. >----------------------------------------------------------------<
  13.  
  14. Binary operates a little bit differently, instead of having 1’s, 10’s, 100’s etc, it has 1’s, 2’s, 4’s and 8’s. So let’s analyse this for a moment, the binary number 1010, has a ‘1’ in the “eights” placeholder and and a ‘1’ in the “twos” placeholder. Add these together and you get 10 in decimal numbers.
  15.  
  16. Example 2: Binary number 1010 = 8+2 = decimal number 10
  17. >------------------------------------------------------<
  18. | Eights(1x2^3)| Fours(1x2^2)| Twos(1x2^1)| Ones(1x2^0)|
  19. >--------------+-------------+------------+------------<
  20. | 1 | 0 | 1 | 0 |
  21. >------------------------------------------------------<
  22.  
  23. Another example is 1111…
  24. Example 3: Binary number 1111 = 8+4+2+1 = decimal number 15
  25. >------------------------------------------------------<
  26. | Eights(1x2^3)| Fours(1x2^2)| Twos(1x2^1)| Ones(1x2^0)|
  27. >--------------+-------------+------------+------------<
  28. | 1 | 1 | 1 | 1 |
  29. >------------------------------------------------------<
  30.  
  31.  
  32. For a detailed example:
  33. >-----------------------------------------------------------------------<
  34. | Eights(1x2^3)| Fours(1x2^2)| Twos(1x2^1)| Ones(1x2^0)|
  35. >----------------+--------------+-------------+------------+------------<
  36. | Binary Values | 1 | 1 | 1 | 1 |
  37. >----------------+--------------+-------------+------------+------------<
  38. | Decimal Values | 8 | 4 | 2 | 1 |
  39. >-----------------------------------------------------------------------<
  40.  
  41. Through the use of the binary table, we can multiply each of the binary values (1’s) by the above multipliers (1x23 1x22 1x21 1x100) or “8, 4, 2, 1”. This will then give us, 8+4+0+1=13.
  42.  
  43. 1.1 - Basic Addition
  44. Much like the decimal system, binary numbers can be added together. For this example we are going to use the binary numbers 0110 and 0010 and add them.
  45.  
  46. Addition example, 0110+0010:
  47. >---------------------------------------------------------------------------------<
  48. | | Eights | Fours | Twos | Ones | | Eights | Fours | Twos | Ones |
  49. >---------+--------+-------+------+------+--------+--------+-------+------+-------<
  50. | Binary | 0 | 1 | 1 | 0 | Binary | 0 | 0 | 1 | 0 |
  51. >---------+--------+-------+------+------+--------+--------+-------+------+-------<
  52. | Decimal | 0 | 4 | 2 | 0 | Decimal| 0 | 0 | 2 | 0 |
  53. >---------------------------------------------------------------------------------<
  54.  
  55. Thus by using decimal addition, 4+2 + 2 = 8, we can determine the value of the two binary numbers together is 8.
  56.  
  57. 2.0 - Binary to Hexadecimal
  58. For the past exercises, we have been working with 4 bits at once (4 values ranging from 0-1, e.g. 0001). This is a /nybble/ in hexadecimal. A byte is made of two nybbles as 8 bits make a byte. In hexadecimal, we have a 1’s placeholder and a 16’s placeholder. Hexadecimal is 0 through 9 and A through F. A nybble can hold 16 unique values but the highest value is 15 because one of the values is 0. A nybble is a single hex digit. So, A = 10, B = 11, so on and so forth, F = 15.
  59.  
  60. So in hex, say we’ve got AF as a byte, AF = 175 in decimal because A is in the 16’s placeholder, A = 10, 10*16=160, plus F which is in the 1’s placeholder, 15*1=15. Therefore 160(A)+15(F) = 175.
  61.  
  62. 3.0 - NOT, AND, OR and XOR
  63.  
  64. 3.1 - NOT
  65. Not is an instruction that takes only ONE operand. The best way to describe it is that it invests the binary value.
  66.  
  67. Example:
  68. A = 1010 in binary or 10 in decimal. NOT A results in the inversion of 1010 which is 0101. Therefore NOT A = 5.
  69.  
  70. 3.2 - AND
  71. Our next instruction is AND, which returns “TRUE” per bits that are the same and takes two operands. True is 1 and false is 0. It operates bit by bit, just like NOT.
  72.  
  73. Example:
  74. F AND A = A, F AND * = *
  75.  
  76. Any time F is AND’d with something, whatever it is AND’d with is the result.
  77.  
  78. Example: F AND A
  79. -----------
  80. F = 1111 so...> 1|1|1|1 < - All four bits are true.
  81. -----------
  82. A = 1010 so...> 1|0|1|0 < - The first and third bits are true.
  83. -----------
  84. 1010 > 1|0|1|0 < - The first and third bits are the only true values in both F and A.
  85. -----------
  86.  
  87. Because the bits in F and A that are the same, generate A but…
  88.  
  89. Example:
  90. -----------
  91. 6 = 0110 so...> 0|1|1|0 < - The second and third bits are true.
  92. -----------
  93. 5 = 0101 so...> 0|1|0|1 < - The second and fourth bits are true.
  94. -----------
  95. 0100 > 0|1|0|0 < - The second bit is the only true values in both 6 and 5.
  96. -----------
  97.  
  98. AND compares each bit and if both bits per placeholder are true, then it returns a true for that placeholder, all else gets turned into 0.
  99.  
  100. 3.3 - OR
  101.  
  102. OR will return true for each placeholder if ANY of the bits are true.
  103.  
  104. Example: 5 OR C
  105. (0101 = 5) OR (1100 = C) = 1101 which equals D.
  106. -----------
  107. 5 = 0101 so...> 0|1|0|1 < - The second and fourth bits are true.
  108. -----------
  109. C = 1100 so...> 1|1|0|0 < - The first and second bits are true.
  110. -----------
  111. D = 1101 > 1|1|0|1 < - The first, second and fourth bits are true in at least one instance.
  112. -----------
  113.  
  114. 3.4 - XOR
  115. Xor is kind of a strange command, if the placeholder bits are different, it returns a true bit. If they are the same, it returns a false bit. Thus, anything XOR’d with itself, results in 0.
  116.  
  117. General Examples:
  118. 1 xor 1 = 0, 0 xor 0 = 0, 1 xor 0 = 1.
  119.  
  120. Detailed Example: A xor F = 5
  121. -----------
  122. A = 1010 so...> 1|0|1|0 < - The first and third bits are true.
  123. -----------
  124. F = 1111 so...> 1|1|1|1 < - The first, second and third bits are true.
  125. -----------
  126. 0101 = 5 > 0|1|0|1 < - The second and fourth bits are true in ONLY one instance as opposed to two found in bits 1 and 3.
  127. -----------
  128. The 1’s placeholders are the same so they return 0, same with the 8’s placeholder. The 4’s and 2’s are different, therefore return true. XOR is really WHICH BITS ARE NOT THE SAME.
  129.  
  130. 4.0 - Conclusion
  131. So that wraps up our basics of hex math, part two will contain bit shifting, bit rotation and some more advanced hex math (difference between logical and arithmetic shifts etc).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement