Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
/VM/station wiki
Search
Search
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Binary flags
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== A quick summary of binary == === Counting in binary === We are used to digits from 0 to 9. But did you ever think how we actually count? When we start with 0 and keep adding 1 we get the following sequence: 0 1 2 3 ... 8 9 After this happens we run out of digits. So we reset the current digit and add 1 to the next one 9 10 11 ... This works the same in binary, however there are only two digits available: 0 and 1. So if we want to count, we count: 0 1 And we're already out of digits, so we reset the current digit and add 1 to the next one: 0 1 10 11 100 101 110 111 ... You may have heard about Hex or "Hexadecimal", which is the same principe as this, but it uses 16 different digits. 0-9, and after that, A-F. === Variable representation in binary === Okay so now that we can count, I'll tell you something that you have probably never heard of. Computers use binary for everything! (No shit...) So yeah, computers do show us numbers like 1,2,3,4,5 and so on as decimal numbers, but in reality they store all of them as binary. So if you write: var/c = 8 the actual value that is stored is (The 0b only says that what follows is a binary number) c == 0b1000 === Operations in binary === What can we do with these numbers tho? There are 3 binary operators available in byond: And (&), Or (|) and Not (~). NOTE: These are binary operators, which means they do the operation on each individual bit. A good way to understand how these work is to not think of these 1s and 0s as numbers, but as truth and lies (false). So wherever you have a 1, you can substitute it with a true statement, while where you have a 0 you substitute it with a lie (a false statement). ==== Binary Not ( ~ ) ==== [[file:NOTgate.gif]] This is the simplest one. All it does it inverts the statement. ~0 = 1 ~1 = 0 The easiest way to understand this is simply, if you have a true statement "The sky is blue" and you stick a not in it, the statement's meaning is inverted: "The sky is not blue". So if the original statement was true (1), then the inverted one is of course false (0). ~1 = 0. The same applies if you negate a false statement. If you have a false statement: "The sky is green" and you negate it, it becomes the truth: "The sky is not green". ~0 = 1. ==== Binary And ( & ) ==== [[file:ANDgate.gif]] AND is defined with two statements. If you either of them is a lie, then the whole statement will be a lie. Because both the first AND the second must be true for the statement to be the truth. 0 & 0 = 0 (If you tell a lie, and then tell another lie, overall you are lying) 0 & 1 = 0 (If you tell a lie, and then tell some truth, you have still lied overall) 1 & 0 = 0 (If you tell the truth, but then lie, you have lied overall) 1 & 1 = 1 (If you tell the truth and then tell another truth, you have told the truth overall) ==== Binary Or ( | ) ==== [[file:ORgate.gif]] OR is also defined with two statements. It however only requires one of the two to be true for the statement to be true. 0 | 0 = 0 ( The sky is green or the night is sunny. Both statements are lies, so it is still a lie overall ) 0 | 1 = 1 ( The sky is green or the night is dark. One of the statements is true, so the overall statement is true ) 1 | 0 = 1 ( the sky is blue or the night is sunny. One of the statements is true, so the overall statement is true ) 1 | 1 = 1 ( the sky is blue or the night is dark. Both statements are true, meaning the overall statement is true ) ==== Binary Xor ( ^ ) ==== [[file:XORgate.gif]] XOR is like OR, except if both statements are true, the output is false. XOR is also known as a difference detector, it'll output true if the two inputs are different from each other. *0 ^ 0 = 0 *0 ^ 1 = 1 *1 ^ 0 = 1 *1 ^ 1 = 0 === Actual use === In code you don't have 1b variables, let's suppose you have 8b variables (can store values from 0 to 255). In actual code they're larger, but let's suppose that to make this a bit shorter. Binary operators work on each individual bit so if you have a variable '''d''' defined as 230 (decimal), which is 11100110 (binary) - what would happen if you used binary operators on this and other values? var/c = 230 // In binary this is 0b11100110 d = ~c //This inverts each bit, so 0b11100110 becomes 0b00011001 (25 decimal) Okay, so negation is easy enough, but what about if you did the following: var/f = 255 // full - this is 0b11111111 var/e = 0 // empty - this is 0b00000000 v1 = c | f // In each of the binary operations the bit in v1 = bit in c OR 1. And we know that anything OR 1 = 1. v1 becomes 0b11111111. This is useful for '''SETTING''' bits. v2 = c | e // In each of the binary operations the bit in v2 = bit in c OR 0. We know that anything or 0 remains unchanged. so v2 == c (0b11100110) v3 = c & f // In each of the binary operations the bit in v3 = bit in c AND 1. 1 does not change the outcome in AND operations, so v3 == c (0b11100110) v4 = c & e // In each of the binary operations the bit in v4 = bit in c AND 0. Anything AND 0 = 0, so v4 becomes 0 (0b00000000). This is useful for '''RESETTING''' bits.
Summary:
Please note that all contributions to /VM/station wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
/VM/station wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width