ID:160914
![]() Jul 11 2008, 5:44 am
|
|
I've seen Popisfizzy use bitwise operations a lot and I only understood like 10% of the code he posted. So far, I got the hang of & and |, but now I want to know more about >>, <<, ^, and others that I missed out. I want to know when the operators that I specified would be very useful.
|
Thanks, but I was looking for applications. It would be really nice if I got a scenario in actual programming in which it could be used.
|
Do a search around - BYOND (and the internet generally) has many tutorials on these.
Kakashi24142 wrote: It would be really nice if I got a scenario in actual programming in which it could be used. Basically, bit flags are used to efficiently combine multiple boolean flags* in a single value (and variable). A quick example would be, in implementing an admin system: client/var This example can be done much more cleanly, in a single variable, using bit flags: client/var *: This means a flag that represents an on/off value, meaning eg 1 or 0. Well, I've also used bit flags in multiple code examples throughout the forum, this seems to be what you want, I'll link to some more: [link], [link] ID:598223 |
Yea I'm aware of using the & and | and I use it quite a lot, but I was wondering what >>, <<, and ^ can be used for.
|
Until I get bored, I'm working on a Pokemon game. In this game, like the actual games, there are EVs (Effort Values). When you defeat another Pokemon, you get one or two EVs on up to two values. I'm able to store up to three sets of data for EVs on one field, two bytes, with a single bit leftover.
The way it works is like this: /* This works because the numbers 0, 1, 2, and 3 only take up, at most, two bits (00, 01, 10, and 11). For the EV's, I use three bits, meaning I can have up to 7 stored. Each set of data takes up a total of five bits. /* |
Popisfizzy wrote:
Until I get bored, I'm working on a Pokemon game. In this game, like the actual games, there are EVs (Effort Values). When you defeat another Pokemon, you get one or two EVs on up to two values. I'm able to store up to three sets of data for EVs on one field, two bytes, with a single bit leftover. If you program EVs, you will be my hero. If you program the IV, DV and breeding systems you will be my god. |
IVs and DVs are, as far as I can tell, the same thing (at least they are according to Bulbapedia), but none of the stuff you just mentioned is difficult. It's just a matter of how long it takes me to get bored and move on, which I estimate will be next week.
|
Popisfizzy wrote:
IVs and DVs are, as far as I can tell, the same thing (at least they are according to Bulbapedia), but none of the stuff you just mentioned is difficult. It's just a matter of how long it takes me to get bored and move on, which I estimate will be next week. The difficulty wasn't what I was referring to. The patience is. I never would have the patience. P.S. I am not done with it, but if you want it, I started mapping the Johto region in DM using the R/S/E/FR/LG sprites. I'll probably never do anything with it. |
The more I learn about bitwise operations...
...the more I hate them and are confused by them. I'm too lazy to actually think. :( |
This isn't really the place for it, so this is my last reply on the matter, but I'm not making a straight port of any of the games, and I plan on making a new map for a bit more freedom is making things for players to do. Thank you for the offer, but it won't be helpful for what I'm doing.
|
GhostAnime wrote:
The first article I read about bit operation and it even explains about the ones you want to know about! I've gone through that article, and like many other people, it just doesn't make sense to me. I think what would be more useful is something along the lines of ZBT - a step by step tutorial for creating your own bitwise operations. That way, you can program a little bit of code that you don't understand at all, and observe the results, Then you can change the code a little bit, and observe the results, and continue that process. Some people, me included, grasp things much more easily that way (through hands-on interaction) than from just reading an article explaining it. |
Kakashi24142 wrote:
Yea I'm aware of using the & and | and I use it quite a lot, but I was wondering what >>, <<, and ^ can be used for. << and >> are shift operators. Some of their more common uses are for quick multiplication and division (respectively) by powers of 2. The expression (1 << 1) returns 2 (1 * 21); (1 << 2) returns 4 (1 * 22). Inversely, (2 >> 1) returns 1, as does (4 >> 2). These methods of multiplication and division are faster than the normal arithmetic operators, but you must consider that bitwise operators operate on a 16-bit field, as well as the fact that they operate on integers (rounding down in the case of division). They are also useful, say, if you want to store multiple smaller numbers in a single variable. In BYOND, for bitwise operations, you get a 16-bit field for bitwise operations (as mentioned previously). So if you wanted to store two 8-bit integers (8-bit unsigned integers have a range of 0-255), you could shift one number over 8 bits to the left, and combine them with the bitwise OR operator to form a single 16-bit number. Something like: var/a = 16 As for a practical use of these shifts and other bitwise operators, I used them extensively in hub://hiead.base64, if you want to snoop around the code a bit. (Disclaimer: I am not responsible if your eyes spontaneously combust from viewing the code). The XOR operator (^) is used to compare the bits of two operands and return the comparison in the form of a resultant number, which has bits that are 1 for areas where the two operands' bits were different, and 0 if they were the same. Basically, the number it produces has a "1" bit only in places where the compared numbers' corresponding bits differ. There are many uses of this, but I'll give a (very short and incomplete) list here:
|
Foomer wrote:
I've gone through that article, and like many other people, it just doesn't make sense to me. I think what would be more useful is something along the lines of ZBT - a step by step tutorial for creating your own bitwise operations. I don't know about any ZBT-like articles on bitwise operators, but I would like to take the chance to point you to the article that kick-started my bitwise knowledge (perhaps in conjunction with Wikipedia). Check out Wizkidd0123's Hello, Operator? (This next link has Hello, Operator? in its original, semi-extinct context). |
Consider this usage, which will loop through all directions.
verb/Spin()//Not exactly a spin On another note, here's an example I thought of for choosing a random direction: mob/Move(L,D,var/allow=0) |
Thanks everyone for the help. If there are any more tips anyone can give me, go ahead and post.
EDIT: WOW! I experimented and read a few articles on google on bitwise operations and I finally understand how useful the shift operators are :D! I'll make an article on this as soon as I'm sure I have a good understanding. 2nd EDIT: After I though about it, the easiest way to get a random dir is like so: var/dir=(1 << rand(0,3)) |
Kakashi24142 wrote:
2nd EDIT: var/dir=(1 << rand(0,3)) var/dir=(1 << rand(0,3)) |
Actually, we're both wrong, it's like this:
var/dir=(1 << rand(0,3)) |
As you're getting a random direction, not caring whether it's cardinal or orthogonal, the following will work just as well, with a bit less code:
proc/rand_dir() The possibility of overlapping bits is irrelevant, as it is simply a random direction. |
http://www.byond.com/members/ DreamMakers?command=view_post&post=39760
Ex:
101 << 2 would return "10100"
10100 >> 2 = 101
You can think ^ as a toggle operator:
6 (1010) ^ 2 (10) = 4 (1000) ^ 2 (10) = 6 (1010) ^ 4 (1000) = 2 (10) ... etc
... Either ways, read the article, it explains a bit more clearly then what I tried to explain >_>