Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

BitBool, Set bits in a byte

So basically this allows you to have 8 booleans in one byte, instead of the normal 1 bool for one byte. It also allows you to acces the shared GetBit and Setbit methods, so you can fuck around yourselves with tiny datatypes and whatnot.

PHP Code

<?php 
Public Class BitBool

    Dim InternalBools
(7) As Boolean

    
Public Sub SetBool(ByRef Index As Integer, ByRef Value As Boolean)
      If 
Index < 0 Or Index > 7 Then
      
Throw New IndexOutOfRangeException("Index must be between 0 or 7")
      
End If

      
InternalBools(Index) = Value
    End Sub

    
Public Function GetBool(ByRef Index As Integer) As Boolean
      
If Index < 0 Or Index > 7 Then
      
Throw New IndexOutOfRangeException("Index must be between 0 or 7")
      
End If

      Return 
InternalBools(Index)
    
End Function

    Public Function 
ToByte() As Byte
      Dim IByte 
As Byte

      IByte 
= SetBit(IByte, 0, InternalBools(0))
      
IByte = SetBit(IByte, 1, InternalBools(1))
      
IByte = SetBit(IByte, 2, InternalBools(2))
      
IByte = SetBit(IByte, 3, InternalBools(3))

      
IByte = SetBit(IByte, 4, InternalBools(4))
      
IByte = SetBit(IByte, 5, InternalBools(5))
      
IByte = SetBit(IByte, 6, InternalBools(6))
      
IByte = SetBit(IByte, 7, InternalBools(7))

      Return 
IByte
    End 
Function

    Public 
Sub ToBools(ByVal IByte As Byte)
      
InternalBools(0) = GetBit(IByte, 0)
      
InternalBools(1) = GetBit(IByte, 1)
      
InternalBools(2) = GetBit(IByte, 2)
      
InternalBools(3) = GetBit(IByte, 3)

      
InternalBools(4) = GetBit(IByte, 4)
      
InternalBools(5) = GetBit(IByte, 5)
      
InternalBools(6) = GetBit(IByte, 6)
      
InternalBools(7) = GetBit(IByte, 7)
    
End Sub

    
Public Shared Function SetBit(ByVal IByte As Byte, ByVal Offset As Byte, ByVal Value As Boolean) As Byte
      
If Offset < 0 Or Offset > 7 Then
      
Throw New IndexOutOfRangeException("Offset must be between 0 and 7")
      
End If

      If 
Value Then
      Dim Mask 
As Byte
      Mask 
= 1 << Offset
      
Return IByte Or Mask
      
Else
      
Dim Mask As Byte
      Mask 
= 255 - (1 << Offset)
      Return 
IByte And Mask
      End 
If
    
End Function

    Public 
Shared Function GetBit(ByVal IByte As Byte, ByVal Offset As Byte) As Boolean
      
If Offset < 0 Or Offset > 7 Then
      
Throw New IndexOutOfRangeException("Offset must be between 0 and 7")
      
End If

      
Dim Mask As Byte
      Mask 
= 1 << Offset

      
Return (IByte And Mask) <> 0
    End 
Function
End Class 

- SetBool(Index As Integer, Value As Boolean)
Set's a boolean at the specified index (0-7) to the specified value

-GetBool(Index As Integer) As Boolean
Get's the value of the boolean at the specified index (0-7)

-ToByte() As Byte
Returns a byte which contains all of the booleans

-ToBools(IByte As Byte)
Set's the booleans values to be the ones that are contained within the byte

-Shared SetBit(IByte As Byte, Offset As Byte, Value As Boolean) As Byte
Set's a bit at the specified offset to the value specified, inside of the specified Byte

-Shared GetBit(IByte As Byte, Offset As Byte) As Boolean
Returns a bit's value at the specified offset, in the specified byte

Hopefully someone finds this helpfull. It's main purpose is to be used in networking.
And please leave a constructive comment ^^
[Image: WMwSRhx.gif]
By doing this aren't you already using eight booleans?
Code
Dim InternalBools(7) As Boolean
(Aug 15, 2014 06:21 AM)DBots Wrote:  By doing this aren't you already using eight booleans?
Code
Dim InternalBools(7) As Boolean

Quote:So basically this allows you to have 8 booleans in one byte, instead of the normal 1 bool for one byte

The code you provided requires 8 bytes of data (8 * 1 byte), but the method provided only requires 1 byte to store all 8 booleans.
[Image: IQE6i8G.png]

BlackCap Wrote:You're even more verbose than Java.
About using GoTo's in VB/C#:
Only3 Wrote:asm use it why u shouldnt
(Aug 15, 2014 06:27 AM)Temploit Wrote:  The code you provided requires 8 bytes of data (8 * 1 byte), but the method provided only requires 1 byte to store all 8 booleans.

Yes it does, now have in mind this is made for networking as I stated.
Meaning it has 8 booleans in memory, but when you are going to send it over a network. You simply send one byte. With the ToByte() function that is, and then you can re-assemble it on the other side.
Using the ToBools(Byte) sub, hopefully that explained a bit. :)
[Image: WMwSRhx.gif]
Hrmmm interesting.... something different on HF to say the least. Nice share homie :)
The concept is interesting, but I really think that you can store everything in one byte in the app itself aswell, try that.
Nice work, i will need to test this later, i like seeing something new keep up the good work.
Thanks, I will be using this for a custom encryption.
Thank you. I'll try it out later, much appreciated
HF Member Since 2010
cuz i like ur class i will show u simple way
Dim Bools() As Boolean = {True, False, True, True, False}
Dim Ba As New BitArray(Bools)
MsgBox(Ba.Get(0))
Ba.Set(0, False)
MsgBox(Ba.Get(0))
http://msdn.microsoft.com/en-us/library/...array.aspx
(Aug 16, 2014 04:18 PM)Only3 Wrote:  cuz i like ur class i will show u simple way
Dim Bools() As Boolean = {True, False, True, True, False}
Dim Ba As New BitArray(Bools)
MsgBox(Ba.Get(0))
Ba.Set(0, False)
MsgBox(Ba.Get(0))
http://msdn.microsoft.com/en-us/library/...array.aspx

I never saw that class :P
That will certaintly make my life easier :D
Thanks Only3, and thanks everyone that commented
[Image: WMwSRhx.gif]
Hrmmm interesting.... something different on HF to say the least
Why do you declare all your parameters ByRef?
Probably coding some useless shit.
This post is so old, and mainly because I was a bit more stupid back then when I made this.
As I understand it now, using ByVal copies the reference to an object, which is fine. Back then I thought it would create a new instance of the object and copy the data even when passing classes as parameters.
Which would use 2x as much ram, or something like that.
[Image: WMwSRhx.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Julius013Mar 2, 2022 05:12 PM 4 314 Mar 2, 2022 05:12 PM
Last Post: Julius013
[C] by Byte™Feb 19, 2017 09:25 PM 10 266 Feb 19, 2017 09:25 PM
Last Post: Byte™
[Python] by datguy.devFeb 8, 2017 10:18 PM 21 9,696 Feb 8, 2017 10:18 PM
Last Post: michael5323