type casting

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

32 bit Signed/unsigned
16 bit signed/unsigned
8 bit signed/unsigned

THis is a standard in a COMPUTER - not a language

SByte Signed 8-bit -128 to 127
Byte Unsigend 8 bit 0 to 255

11111111 = -1 or 255 dependent upon which "type" you use.

So why can't i do this:

VB.NET:
dim value as integer = -1
dim x as UInteger

x = CUint(value)

That defies the concept of using a computer in the first place. A bit is a bit is a bit, it doesn't matter what language is interpreting it. and for those that can't think in "concepts" here is the code i'm trying to do:

VB.NET:
Public MustInherit Class THashKey
  Public Shared ReadOnly S_HW As Integer = 16
  Public Shared ReadOnly S_HWHB As Integer = 24
  Public Shared ReadOnly S_LWHB As Integer = 8
  Public Shared ReadOnly C_HW As Integer = 65535 << S_HW
  Public Shared ReadOnly C_LW As Integer = 65535
  Public Shared ReadOnly C_HWHB As Integer = 255 << S_HWHB
  Public Shared ReadOnly C_HWLB As Integer = 255 << S_HW
  Public Shared ReadOnly C_LWLB As Integer = 255
  Public Shared ReadOnly C_LWHB As Integer = 255 << S_LWHB

  Public Enum KeyBytes
    kbValue
    kbHW
    kbLW
    kbHWHB
    kbHWLB
    kbLWHB
    kbLWLB
  End Enum

  Private f_Hash As UInteger

  Public Sub New()
  End Sub

  Public Sub New(ByVal iHash As UInteger)
    f_Hash = iHash
  End Sub

  Public Property HW() As UShort
    Get
      Return CUShort((f_Hash And C_HW) >> S_HW)
    End Get
    Set(ByVal value As UShort)
      f_Hash = CUInt(LW) Or (CUInt(value) << S_HW)
    End Set
  End Property

  Public Property LW() As UShort
    Get
      Return CUShort(f_Hash And C_LW)
    End Get
    Set(ByVal value As UShort)
      f_Hash = (CUInt(HW) << S_HW) Or CUInt(value)
    End Set
  End Property

  Public Property HWHB() As Byte
    Get
      Return CByte((f_Hash And C_HWHB) >> S_HWHB)
    End Get
    Set(ByVal value As Byte)
      f_Hash = CUInt(LW) Or (CUInt(HWLB) << S_HW) Or (CUInt(value) << S_HWHB)
    End Set
  End Property

  Public Property HWLB() As Byte
    Get
      Return CByte((f_Hash And C_HWLB) >> S_HW)
    End Get
    Set(ByVal value As Byte)
      f_Hash = CUInt(LW) Or (CUInt(HWHB) << S_HWHB) Or (CUInt(value) << S_HW)
    End Set
  End Property

  Public Property LWHB() As Byte
    Get
      Return CByte((f_Hash And C_LWHB) >> S_LWHB)
    End Get
    Set(ByVal value As Byte)
      f_Hash = (CUInt(HW) << S_HW) Or CUInt(LWLB) Or (CUInt(value) << S_LWHB)
    End Set
  End Property

  Public Property LWLB() As Byte
    Get
      Return CByte(f_Hash And C_LWLB)
    End Get
    Set(ByVal value As Byte)
      f_Hash = (CUInt(HW) << S_HW) Or (CUInt(LWHB) << S_LWHB) Or CUInt(value)
    End Set
  End Property

  Default Public Property Value(ByVal part As KeyBytes) As UInteger
    Get
      Select Case part
        Case KeyBytes.kbValue : Return f_Hash
        Case KeyBytes.kbHW : Return HW
        Case KeyBytes.kbHWHB : Return HWHB
        Case KeyBytes.kbHWLB : Return HWLB
        Case KeyBytes.kbLW : Return LW
        Case KeyBytes.kbLWHB : Return LWHB
        Case KeyBytes.kbLWLB : Return LWLB
      End Select
    End Get
    Set(ByVal value As UInteger)
      Select Case part
        Case KeyBytes.kbValue : f_Hash = value
        Case KeyBytes.kbHW : HW = CUShort(value)
        Case KeyBytes.kbHWHB : HWHB = CByte(value)
        Case KeyBytes.kbHWLB : HWLB = CByte(value)
        Case KeyBytes.kbLW : LW = CUShort(value)
        Case KeyBytes.kbLWHB : LWHB = CByte(value)
        Case KeyBytes.kbLWLB : LWLB = CByte(value)
      End Select
    End Set
  End Property

  Friend Overridable Sub Copy(ByVal value As THashKey)
    If value IsNot Nothing Then f_Hash = value.f_Hash
  End Sub

  Public Overridable Function Init(ByVal ParamArray args() As Object) As THashKey
    If (args.Length >= 1) AndAlso IsNumeric(args(0)) Then
      f_Hash = CUInt(args(0))
    End If
    Return Me
  End Function

End Class

From this I am using the High Word (upper 16 bits) as the Row indicator, and the Low Word (lower 16 bits) as the Column indicator. If the Row and/or Column are -1 then they represent property items for rows, columns, or for the entire grid. (please don't tell me that something else exists to do this because i don't care. I want to learn the language and by writing things myself I get better in understand the flaws and virtues - if they exist - of a new language).

So Upon grid creation I am creating a THashKey descentent that tries to call this:
VB.NET:
Me.Value(KeyBytes.kbHW) = -1
Me.Value(KeyBytes.kbLW) = -1

This, should set the two parts of f_Hash to the appropriate values affecting the BITS not the "typed value" and being a 32-bit unsigned integer, converting -1 to a UShort (16 bit unsiged) should equate to 65535, shift left 16 and thus in the end the value of f_Hash after setting the High Word to -1 and the Low Word to -1 should be:
4,294,967,295

So, why won't it let me BITWISE manipulate these items. This is fundamental to what a computer is, and i'm quite tired of the supposed "high level languages" preventing me from manipulating them at the most basic level. Perhaps i'm using the wrong types to achieve this, but I don't quite see why I should need to use a 64 bit to retain what I can use a 32 bit to do.

*shrug*
Tis just frustrating, because C, C++, Pascal, even Fortran can do this, though Fortran might not be up to the 32 bit technology *chuckle*
Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
 
So why can't i do this:

VB.NET:
dim value as integer = -1
dim x as UInteger

x = CUint(value)

That defies the concept of using a computer in the first place. A bit is a bit is a bit, it doesn't matter what language is interpreting it.
You can't do that because -1 is out of range for UInteger data type. If you want the bytes you can use BitConverter, with this you can also attempt to read the byte values into various other data types.
Your question is the same as complaining CInt("why?") doesn't give you 1064921207.
 
Your question is the same as complaining CInt("why?") doesn't give you 1064921207.

Absolutely not.

VB.NET:
Delphi:
Var X: Integer;
begin
  x = -1;
  Writeln(DWORD(x))
end;
output = 4294967295

C++:
{
  int x = -1;
  dword y = (dword) x;
}
y now equals 4294967295

It's called Type Casting, and a number is a number.

You can't do that because -1 is out of range for UInteger data type.

I would agree that if i did this:
VB.NET:
dim x as integer = -1;
dim y as uinteger = x
then yes, the value in x is out of the range of uinteger but the whole point of using CUInt() is to TypeCast the underlying data (32 bits of 1 or 0) into an acceptable range of UInteger from Integer
And if you were to typecast "why?" you would not be typecasting the string you would be typecasting the 32-bit stack pointer of the address that contains the stack values of "w", "h", "y", "?", not the characters themselves.

Sorry, but I am of the belief that no matter how high level the language is, it should still be able to have a "Simple" data type, which is limited to:
8/16/32/64-Bit S/U Numbers, Pointers, and the standard FPU values of Single, Double, and Extended. (i apologize but I don't have the stats for the FPU simple data types memories, but they usually take 6,8, or 12 bytes to store).

Thus the limitation is in place by the compiler and the language, NOT the computer.
If you want the bytes you can use BitConverter, with this you can also attempt to read the byte values into various other data types.

However, thanks for the point in the direction to this. I will look into the BitConverter class (which seems sooooo much overhead for something that assembly can do in a less than a clock cycle)

Shrug
Jaeden "Sifo Dyas" al'Raec Ruiner

All Things in a computer is a pointer no matter what any language expert tells you otherwise. Procedures, functions, variables, and objects are ALL POINTERS. :)
 
The most likely reason is that VB.Net does not support pointers. (which C++ and C# do) Why? Perhaps for better type safety?
Type safety is closely linked to memory safety, a restriction on the ability to copy arbitrary bit patterns from one memory location to another.
I'm sure MS has chosen to not include pointer support in VB on purpose, and not because it wasn't possible or they didn't have time yet to add it.

Here is another example that shows how one were to operate in VB.Net to achieve that cast by accessing unmanaged memory through interop marshaling:
VB.NET:
Dim i As Int32 = -1
Dim ptr As IntPtr = Runtime.InteropServices.Marshal.AllocCoTaskMem(4)
Runtime.InteropServices.Marshal.StructureToPtr(i, ptr, True)
Dim ui As UInt32 = Runtime.InteropServices.Marshal.PtrToStructure(ptr, GetType(UInt32))
Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr)
Here is another using the Buffer class, which requires use of arrays:
VB.NET:
Dim i() As Integer = {-1}
Dim ui(0) As UInteger
Buffer.BlockCopy(i, 0, ui, 0, 4)
It may be easier/faster to calculate +/- 128 etc.

All the C... functions like CUInt etc are type conversion functions with the types defined range of what expression values can be converted.
 
Back
Top