CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    24 bit integer in VB?

    Believe it or not have a 3bytes that I want to convert to a unsigned 24 bit integer in VB. Anyone know how to do this in .net? Is it even possible?

  2. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 24 bit integer in VB?

    Quote Originally Posted by DaBoonDockSaint View Post
    Believe it or not have a 3bytes that I want to convert to a unsigned 24 bit integer in VB. Anyone know how to do this in .net? Is it even possible?
    As I know, VB does not support 24 bit integer, but depending on why you need to do it, it may be many solutions.

    For example, you can create your own data type, emulating a 24 bit behavior, but why no use int32? to save space? for compatibility with some algorithm?
    [Vb.NET 2008 (ex Express)]

  3. #3
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: 24 bit integer in VB?

    Quick solution: use the top 24bits of a 32 bit integer: just multiply everything by 2^8, manipulate, divide by 2^8 (ideally, you'd use logical shifts, but I'mm not certain that's possible in VB)
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  4. #4
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    Re: 24 bit integer in VB?

    Yea I was trying to avoid breaking everything down into binary and doing the math but it looks like i have no choice. The 24 bit thing was not my choice just happens to be what I am looking at I wish it was 32bit I could just use the Bitconverter and that would be the end of it but that would be too easy.

  5. #5
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 24 bit integer in VB?

    Quote Originally Posted by javajawa View Post
    Quick solution: use the top 24bits of a 32 bit integer: just multiply everything by 2^8, manipulate, divide by 2^8 (ideally, you'd use logical shifts, but I'm not certain that's possible in VB)
    This class intend to raise exceptions on 24 bit overflows.
    Note that it needs more operators defined (i/e. integer + SignedInt24 is not the same as SignedInt24 + Integer)
    it does not use bit shift, but the bit shift operator is
    integer << 8 '(equals integer*256)
    integer >> 8 '(equals integer/256)
    Code:
    Public Class Form1
        Friend Class SignedInt24
    
    
            Private Shared Function ToInternalRepresentation(ByRef value As Int32) As Int32
                Return 256 * value 'Shift the bits to the left <-8 bits-
            End Function
            Private Shared Function ToExternalRepresentation(ByRef value As Int32) As Int32
                Return CInt(value / 256) 'Shift the bits to the right -8 bits->
            End Function
    
            Private InternalValue As Int32 = 0
            Public Property Value() As Int32
                Get
                    Return ToExternalRepresentation(InternalValue)
                End Get
                Set(ByVal NewValue As Int32)
                    InternalValue = ToInternalRepresentation(NewValue)
                End Set
            End Property
    
            Private ReadOnly Property ToExternal() As Int32
                Get
                    Return ToExternalRepresentation(InternalValue)
                End Get
            End Property
    
            Friend Sub New(Optional ByRef NewValue As Int32 = 0)
                InternalValue = ToInternalRepresentation(NewValue)
            End Sub
    
            Public Shared Operator *(ByVal class1 As SignedInt24, _
                                     ByVal class2 As SignedInt24) As SignedInt24
                Static Result As New SignedInt24
                Result.InternalValue = ToInternalRepresentation(class1.ToExternal * class2.ToExternal)
                Return Result
            End Operator
            Public Shared Operator *(ByVal class1 As SignedInt24, _
                                     ByVal Other As Integer) As SignedInt24
                Return New SignedInt24(class1.ToExternal * Other)
            End Operator
            Public Shared Operator +(ByVal class1 As SignedInt24, _
                                     ByVal class2 As SignedInt24) As SignedInt24
                Static Result As New SignedInt24
                Result.InternalValue = class1.InternalValue + class2.InternalValue
                Return Result
            End Operator
            Public Shared Operator +(ByVal class1 As SignedInt24, _
                                     ByVal other As Int32) As SignedInt24
                Static Result As New SignedInt24
                Result.InternalValue = class1.InternalValue + ToInternalRepresentation(other)
                Return Result
            End Operator
    
            Friend Shared ReadOnly Property MaxValue() As Int32
                Get
                    Return ToExternalRepresentation(Int32.MaxValue)
                End Get
            End Property
    
        End Class 'SignedInt24
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim a As New SignedInt24(Int16.MaxValue)
            Dim b As New SignedInt24 : b.Value = 256
            Dim c As New SignedInt24
            c = a * b
            MsgBox("Int16.MaxValue * 256 = " & c.Value & vbCrLf & _
                   "(1 + Int16.MaxValue) * 256 = " & 256 + c.Value & vbCrLf & _
                   "SignedInt24.MaxValue = " & SignedInt24.MaxValue)
    
            MsgBox("SignedInt24.MaxValue +1 should throw an overflow exception")
            Dim d As New SignedInt24(SignedInt24.MaxValue)
            d += 1
        End Sub
    End Class
    [Vb.NET 2008 (ex Express)]

  6. #6
    Join Date
    Apr 2008
    Posts
    82

    Re: 24 bit integer in VB?

    Code:
            Dim b() As Byte = New Byte() {7, 5, 3} 'test bytes
            Dim x, y, bi, result As Integer
            Dim ab As Byte
            x = 0
            For y = 23 To 0 Step -1
                If (y + 1) Mod 8 = 0 Then
                    ab = b(x) 'get next byte
                    x += 1
                    bi = 7
                End If
                If (ab And CByte(2 ^ bi)) = CByte(2 ^ bi) Then
                    result = result Or CInt(2 ^ y)
                End If
                bi -= 1
            Next

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: 24 bit integer in VB?

    Why over complicate it???

    Just use a regulat 32 bit integer for all internal calculations. IGNORE the most significant byte.

    Remember integer operations "wrap around" they do NOT "overflow".

    The only possible check you MIGHT need is to clean up the upper byte in the event of partial overflows, but that is rarely necessary.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: 24 bit integer in VB?

    I agree entirely, although I'd use the MSB
    Surely it makes more sense to ignore the LSB from a design perspective (i.e. two's complement interpretation, allowing certain assembler flags will be correct). Of course, sign extension may solve this, but leads to it's own set of problems.
    Or is there a simple correction which I'm missing here?
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: 24 bit integer in VB?

    Quote Originally Posted by javajawa View Post
    I agree entirely, although I'd use the MSB
    Surely it makes more sense to ignore the LSB from a design perspective (i.e. two's complement interpretation, allowing certain assembler flags will be correct). Of course, sign extension may solve this, but leads to it's own set of problems.
    Or is there a simple correction which I'm missing here?
    Consider all of the extra work you have to do (that I dont) to perform operations with integer variables of differing sizes.

    The LEAST significant bit should ALWAYS mean "1". You use as many significant bits as necessary to carry the necessary information.

    "Psuedo"
    Code:
    System.Int8 a;
    System.Int16 b;
    MySpecial.Int24 c;
    System.Int32 d;
    
    Console.Writeline(a+b+c+d);
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    Re: 24 bit integer in VB?

    Is bit padding much more difficult to do?

  11. #11
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    Re: 24 bit integer in VB?

    Quote Originally Posted by TheCPUWizard View Post
    Why over complicate it???

    Just use a regulat 32 bit integer for all internal calculations. IGNORE the most significant byte.

    Remember integer operations "wrap around" they do NOT "overflow".

    The only possible check you MIGHT need is to clean up the upper byte in the event of partial overflows, but that is rarely necessary.
    Any suggestions on how to do this?

  12. #12
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    Re: 24 bit integer in VB?

    Quote Originally Posted by TheCPUWizard View Post
    Why over complicate it???

    Just use a regulat 32 bit integer for all internal calculations. IGNORE the most significant byte.

    Remember integer operations "wrap around" they do NOT "overflow".

    The only possible check you MIGHT need is to clean up the upper byte in the event of partial overflows, but that is rarely necessary.
    THIS WORKED!!!! I just treated it like a 32bit integer ignored the most significant bit and that was that. Thanks for the help

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: 24 bit integer in VB?

    Quote Originally Posted by DaBoonDockSaint View Post
    THIS WORKED!!!! I just treated it like a 32bit integer ignored the most significant bit and that was that. Thanks for the help
    Hopefully you meant byte.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    18

    Re: 24 bit integer in VB?

    lol yea byte sorry. So just out of curiosity what if you had a to convert 9 bytes into a 6 bit integer, is this even possible?

  15. #15
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 24 bit integer in VB?

    Quote Originally Posted by DaBoonDockSaint View Post
    THIS WORKED!!!! I just treated it like a 32bit integer ignored the most significant bit and that was that. Thanks for the help
    Remember that the most significant bit on the most significant Byte contains the sign bit, so whatever thing you are doing, do not eliminate the first bit, unless you are using unsigned integer (UintXX, instead of IntXX).
    Last edited by Marraco; February 2nd, 2009 at 01:04 PM.
    [Vb.NET 2008 (ex Express)]

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured