Difference between revisions of "Bitwise Operations"
(Created page with "Bitwise operations are used to perform an action on the bits (the 1's and 0's) of a number. Understanding what bitwise operators do requires you to understand how to convert ...") |
m |
||
Line 1: | Line 1: | ||
Bitwise operations are used to perform an action on the bits (the 1's and 0's) of a number. Understanding what bitwise operators do requires you to understand how to convert from decimal to binary and from binary to decimal. | Bitwise operations are used to perform an action on the bits (the 1's and 0's) of a number. Understanding what bitwise operators do requires you to understand how to convert from decimal to binary and from binary to decimal. | ||
− | In this article a "binary" formatted number (which represents the "bits" of a number) will be written with a <sup>2</sup>... | + | In this article a "binary" formatted number (which represents the "bits" of a number) will be written with a <sup>2</sup>... and a decimal number with a <sup>10</sup> |
==Converting from decimal to binary and from binary to decimal== | ==Converting from decimal to binary and from binary to decimal== | ||
Line 17: | Line 17: | ||
|Quotient: ||391 ||195 ||97 ||48 ||24 ||12 ||6 ||3 ||1 ||0 | |Quotient: ||391 ||195 ||97 ||48 ||24 ||12 ||6 ||3 ||1 ||0 | ||
|- | |- | ||
− | |Remainder: ||1 1 ||1 ||1 ||0 ||0 ||0 ||0 | + | |Remainder: ||1 ||1 ||1 ||1 ||0 ||0 ||0 ||0 ||1 ||1 |
|} | |} | ||
You have to stop dividing if the quotient is 0. | You have to stop dividing if the quotient is 0. | ||
− | Now, read the sequence of remainders from right to left, then you get the binary number 1100001111. | + | Now, read the sequence of remainders from '''right to left''', then you get the binary number 1100001111. |
+ | |||
+ | ;Negative decimal number to binary | ||
+ | |||
+ | For example: -783 | ||
+ | |||
+ | # Take the binary form of 783: 0000001100001111 | ||
+ | # Invert it: 1111110011110000 | ||
+ | # Add up 1111110011110000 with 1 | ||
+ | # So, -783<sup>10</sup> is 11111100111100012 | ||
+ | |||
+ | How you can be sure this number is negative? It depends on the data type. If the data type is an Int16, then if the first bit is a 0, then the number is positive. If the first bit is a 1, the number is negative. So, 1111110011110000 (Int16) is -783, but for an unsigned number, UInt16 for example, the first number DOESN'T tell whether the number is negative or not. For an UInt16 for example, we can be sure it's positive because it's unsigned. So, 1111110011110000 as an UInt16 is 64752. | ||
+ | |||
+ | In Sphere objects, an example of a signed integer would be MORE1 and MORE2 |
Revision as of 21:02, 28 March 2013
Bitwise operations are used to perform an action on the bits (the 1's and 0's) of a number. Understanding what bitwise operators do requires you to understand how to convert from decimal to binary and from binary to decimal.
In this article a "binary" formatted number (which represents the "bits" of a number) will be written with a 2... and a decimal number with a 10
Converting from decimal to binary and from binary to decimal
If you use a bitwise operator, there will be an action performed for each bit in the binary form of the integer. For example 110100112 is 21110. And 14310 is 100011112.
- From decimal to binary
If we have a decimal number, 783 for example, you can convert it to a decimal number using this way:
Division: | 783 / 2 | 391 / 2 | 195 / 2 | 97 / 2 | 48 / 2 | 24 / 2 | 12 / 2 | 6 / 2 | 3 / 2 | 1 / 2 |
Quotient: | 391 | 195 | 97 | 48 | 24 | 12 | 6 | 3 | 1 | 0 |
Remainder: | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
You have to stop dividing if the quotient is 0.
Now, read the sequence of remainders from right to left, then you get the binary number 1100001111.
- Negative decimal number to binary
For example: -783
- Take the binary form of 783: 0000001100001111
- Invert it: 1111110011110000
- Add up 1111110011110000 with 1
- So, -78310 is 11111100111100012
How you can be sure this number is negative? It depends on the data type. If the data type is an Int16, then if the first bit is a 0, then the number is positive. If the first bit is a 1, the number is negative. So, 1111110011110000 (Int16) is -783, but for an unsigned number, UInt16 for example, the first number DOESN'T tell whether the number is negative or not. For an UInt16 for example, we can be sure it's positive because it's unsigned. So, 1111110011110000 as an UInt16 is 64752.
In Sphere objects, an example of a signed integer would be MORE1 and MORE2