Hack Forums
[.NET] BitBool, Set bits in a byte - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] BitBool, Set bits in a byte (/showthread.php?tid=4396680)



BitBool, Set bits in a byte - sanderele1 - Aug 15, 2014

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 ^^


* - DBots - Aug 15, 2014

By doing this aren't you already using eight booleans?
Code
Dim InternalBools(7) As Boolean



* - Temploit - Aug 15, 2014

(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.


* - sanderele1 - Aug 15, 2014

(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. :)


* - nuk - Aug 15, 2014

Hrmmm interesting.... something different on HF to say the least. Nice share homie :)


* - SIM-1456245 - Aug 15, 2014

The concept is interesting, but I really think that you can store everything in one byte in the app itself aswell, try that.


* - SIM-1847363 - Aug 15, 2014

Nice work, i will need to test this later, i like seeing something new keep up the good work.


* - DeMoNi - Aug 16, 2014

Thanks, I will be using this for a custom encryption.


* - Hopsin - Aug 16, 2014

Thank you. I'll try it out later, much appreciated


* - Only3 - Aug 16, 2014

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/system.collections.bitarray.aspx


* - sanderele1 - Aug 17, 2014

(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/system.collections.bitarray.aspx

I never saw that class :P
That will certaintly make my life easier :D
Thanks Only3, and thanks everyone that commented


* - en3rgizz - Sep 22, 2015

Hrmmm interesting.... something different on HF to say the least


* - ubbelol - Sep 22, 2015

Why do you declare all your parameters ByRef?


* - sanderele1 - Sep 22, 2015

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.