| Mirage Source http://miragesource.net/forums/ |
|
| Bit Fields http://miragesource.net/forums/viewtopic.php?f=210&t=998 |
Page 1 of 1 |
| Author: | Lea [ Sat Dec 30, 2006 3:20 pm ] |
| Post subject: | Bit Fields |
Hi Everyone! Here's some code for bitfields. It's 100% written by me. Code: Private Function SetBit(ByVal Field As Byte, ByVal Bit As BitField, ByVal Value As Boolean) As Byte 'Use AND to read, and use OR to write true, use AND NOT to write false SetBit = 0 If Value = True And (Field And Bit) <> Bit Then 'If the setting was false, and we want to set it true Field = (Field Or Bit) SetBit = 1 Exit Sub ElseIf Value = False And (Field And Bit) = Bit Then 'If the setting is true, and we want to set it false Field = (Field And Not Bit) SetBit = 1 Exit Sub Else SetBit = 2 'Symbol that we didn't need to change the value Exit Sub End If End Function Private Function GetBit(ByVal Field as byte, ByVal Bit As BitField) As Boolean GetBit = False If (Field And Bit) = Bit Then GetBit = True End If End Function Now use an enum to hold the names and 'values' of a bit. Code: Enum BitField
Unused_1 = 1 Unused_2 = 2 Unused_4 = 4 Unused_8 = 8 Unused_16 = 16 Unused_32 = 32 Unused_64 = 64 Unused_128 = 128 End Enum The names are unimportant, but the values are. Now create a variable to hold the value of this bitfield. Feel free to make it global. If you will be only using one bitfield, you can modify the GetBit and SetBit functions to access that directly, instead of passing it as a variable. Now when you call GetBit or SetBit, you even get to see the name of the bit you want to get/set So what's this used for? A bit is a on or off variable, true or false. In visual basic, two bytes (16 bits) are used to store a true or false. We can store 16 bitfield values in that ammount of space! To expand on this code, you could add the ability to do an integer, or long's worth of bits. Just add to the Enum continuing the powers of 2, and change the data type in the functions. Enjoy~ |
|
| Author: | William [ Sat Dec 30, 2006 3:40 pm ] |
| Post subject: | |
Code: Unused_1 = 1
Unused_2 = 2 Unused_4 = 4 Unused_8 = 8 Unused_16 = 16 Unused_32 = 32 Unused_64 = 64 Unused_128 = 128 Isnt that the fibbinaschi code (from 'the davinci code') or however you spell it? |
|
| Author: | Misunderstood [ Sat Dec 30, 2006 4:27 pm ] |
| Post subject: | |
no way william those are mostly powers of 2... http://en.wikipedia.org/wiki/Fibonacci_number |
|
| Author: | William [ Sat Dec 30, 2006 4:32 pm ] |
| Post subject: | |
Ohh yeah, now I remember |
|
| Author: | Spodi [ Sat Dec 30, 2006 9:02 pm ] |
| Post subject: | |
Wow, you've managed to complicate simple bitwise operators. |
|
| Author: | Lea [ Sun Dec 31, 2006 3:04 am ] |
| Post subject: | |
That is simple. It's a simple function for both setting and getting bits |
|
| Author: | Dragoons Master [ Tue Jan 02, 2007 3:43 am ] |
| Post subject: | |
Fibonati is like this: Code: F(0)=0
F(1)=1 F(N)=F(N-1)+F(N-2) That's all you need, so, for ex Fibonati(6): Fibonati(6)=Fibonati(5)+Fibonati(4) Fibonati(5)=Fibonati(4)+Fibonati(3) Fibonati(4)=Fibonati(3)+Fibonati(2) Fibonati(4)=Fibonati(3)+Fibonati(2) Fibonati(3)=Fibonati(2)+Fibonati(1) Fibonati(3)=Fibonati(2)+Fibonati(1) Fibonati(2)=Fibonati(1)+Fibonati(0) Replacing... Fibonati(3)=Fibonati(2)+1 Fibonati(2)=1+0=1 Fibonati(3)=1+1=2 Fibonati(4)=2+1=3 Fibonati(3)=2 Fibonati(5)=3+2=5 Fibonati(4)=3 Fibonati(6)=5+3=8 So Fibonati(6)=8 Pretty good example of a recursive algorithm. |
|
| Author: | Bakekitsune [ Sun Feb 11, 2007 2:24 am ] |
| Post subject: | |
Sorry for being noob but... Whats enum? and where do you put it? Also i dont fully understand why the bitfields double each time and why the bitfield has to = 1 byte (can it be less? so i have 1bit in the bitfield?) |
|
| Author: | Misunderstood [ Sun Feb 11, 2007 2:54 am ] |
| Post subject: | |
Enums are kind of like fancy constants. You should probably put it in either modtypes or modconstants, though really you could put it almost anywhere. |
|
| Author: | Lea [ Sun Feb 11, 2007 3:34 am ] |
| Post subject: | |
Enums put all the variables inside of one "name". they really just make it simple. You can do Dim x as Enum and when you do "x =" it will show the variables in the enum. So if you put your enum in the get and set bits functions, it will show you the variables that you can pick from. ALL of the variables must be powers of 2. You can not have less than a bit's worth, but you could add to the enum to get a int or a long to do the same thing. An int would be 16 bits, a long is 32 bits. You use a byte to get 8, and you can't get any smaller than that in Visual Basic. |
|
| Author: | Spodi [ Sun Feb 11, 2007 9:07 am ] |
| Post subject: | |
Just for the record, since Ints and Longs are signed, you have to get the last bit by: Int: -(((2 ^ 16) / 2) - 1) Long: -(((2 ^ 32) / 2) - 1) I believe that is correct, anyways. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|