Results 1 to 7 of 7

Thread: Reading bits in Big Endian format.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Reading bits in Big Endian format.

    I have never had to bother with Big Endian format before. Recently, my work has required me to mess with it a bit and it's a complete headache.

    I understand that to convert from little endian (&h12345678) to big endian we just switch the byte order (&h78563412). I get that.

    My problem comes when I need to read and write bits. There is a part in a file where each value consists of nine (9) bits. These values are consecutive. This means the first value uses all eight bits of the first byte and the first bit of the second byte. The second value uses the last seven bits of the second byte and the first two bits of the third byte, and so on.

    For little endian I would just create a mask, such as &H1FF for nine bits, and mask off the part I need. No matter what I try, I cannot figure out how I would do this for big endian.

    So, my question is: How would I read / write a specific amount of bits from a value stored in big endian format?

    Thanks.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Reading bits in Big Endian format.

    Not sure how you read that "value" and how long that value (bytes) is. Normally I would suggest reading the "value" into a byte array.

    Have a look at this:
    http://msdn.microsoft.com/en-us/libr...converter.aspx

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Reading bits in Big Endian format.

    Let me try this again. Say I have the following value: &H5858A911A. This is the little endian representation of the value.

    The big endian representation of the value would be: &H1A918A855.

    Now, I figured out how to read bits from the value. For example, to read 9 bits starting at bit 0 I would simply use the following:

    vb.net Code:
    1. result = (value >> (32 - Length - b)) And Mask

    Length = 9 (bits)
    b is the bit to start at (0)
    Mask is a nine bit mask (&H1FF)

    My issue comes when I try to write nine bits in big endian format. I figure it would simply be the opposite:

    vb.net Code:
    1. result = value << (32 - Length - b)

    Then I could OR the bits in. It's just not working out. I was hoping someone had dealt with this before and had any info to share.
    Prefix has no suffix, but suffix has a prefix.

  4. #4
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Reading bits in Big Endian format.

    Quote Originally Posted by Troy Lundin View Post
    ...I was hoping someone had dealt with this before......
    Ehhh....yes.
    Did you read the link?
    Just read the bytes in the right order in a byte array.
    Convert to a datatype depending the nbr of bytes.
    Mask the bits (9 in your case).

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading bits in Big Endian format.

    Quote Originally Posted by Troy Lundin View Post
    Let me try this again. Say I have the following value: &H5858A911A. This is the little endian representation of the value.

    The big endian representation of the value would be: &H1A918A855.

    Now, I figured out how to read bits from the value. For example, to read 9 bits starting at bit 0 I would simply use the following:

    vb.net Code:
    1. result = (value >> (32 - Length - b)) And Mask

    Length = 9 (bits)
    b is the bit to start at (0)
    Mask is a nine bit mask (&H1FF)

    My issue comes when I try to write nine bits in big endian format. I figure it would simply be the opposite:

    vb.net Code:
    1. result = value << (32 - Length - b)

    Then I could OR the bits in. It's just not working out. I was hoping someone had dealt with this before and had any info to share.
    It is unclear which bit 0 (little or big version of the value) you want.

    Code:
            Dim foo As Integer = &HDEADBEEF
    
            Dim bar As Integer = Net.IPAddress.HostToNetworkOrder(foo) 'convert endianess
    
            Dim mask As Integer = &H1FF
    
            Dim result As Integer = bar And (mask << 0) 'adjust shift as needed
    
            Dim resultli As Integer = Net.IPAddress.NetworkToHostOrder(result) 'convert endianess
    IPAddress has an overload for longs also.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Reading bits in Big Endian format.

    I would perform the extraction of the 9 bit values separately from considering the endian-ness. One way you could do this is reading the big-endian encoded bytes and writing a little-endian encoded set of bytes. Since you now have the bytes in little-endian format you can use the same access code as before. It's unclear at what level the endian-ness is relevant, so this might be exactly what you need, but the principle is to separate orthogonal concerns: how bytes are encoded is separate from the values that they represent.

  7. #7

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Reading bits in Big Endian format.

    Just wanted to let you guys know I figured it out. The functions are below.


    vb.net Code:
    1. ''' <summary>
    2.         ''' Writes the specified value to the buffer.
    3.         ''' </summary>
    4.         ''' <param name="Offset">Required. The offset to start writing.</param>
    5.         ''' <param name="Bit">Required. The bit to start writing.</param>
    6.         ''' <param name="Length">Required. The number of bits to write.</param>
    7.         ''' <param name="Value">Required. The value to write.</param>
    8.         Public Shared Sub NumberBase(ByVal Offset As Int32, ByVal Bit As Int32, ByVal Length As Int32, ByVal Value As Int32)
    9.             Try
    10.                 Dim b As Int32 = Bit        ' Bit currently being manipulated
    11.                 Dim ofs As Int32 = Offset   ' Offset being written
    12.                 Dim val As Int32 = 0        ' Value to write
    13.                 Dim State As Boolean        ' State of current bit
    14.  
    15.                 ' Precautionary removal of negative sign.
    16.                 Length = Math.Abs(Length)
    17.  
    18.                 ' Modify the bit and offset accordingly.
    19.                 While b > 8
    20.                     b -= 8
    21.                     ofs += 1
    22.                 End While
    23.  
    24.                 ' Shift the value to the left.
    25.                 val = Value << (32 - Length - b)
    26.  
    27.                 ' Loop through each bit.
    28.                 For i As Int32 = b To Length + b - 1
    29.                     State = CBool((val >> (31 - i)) And 1)  ' Get the state of the bit.
    30.                     BitflagBase(ofs, i, State, False)       ' Set it in the buffer.
    31.                 Next
    32.             Catch ex As Exception
    33.                 GetError(ex)
    34.             End Try
    35.         End Sub
    36.  
    37.         ''' <summary>
    38.         ''' Sets the state of the specified bit relative to the specified offset, inverting if necessary.
    39.         ''' </summary>
    40.         ''' <param name="Offset">Required. The offset at which to start.</param>
    41.         ''' <param name="Bit">Required. The bit whose state is to be set.</param>
    42.         ''' <param name="State">Required. Whether or not the bit is set.</param>
    43.         ''' <param name="Invert">Required. Inverts the state.</param>
    44.         Public Shared Sub BitflagBase(Offset As Int32, Bit As Int32, State As Boolean, Invert As Boolean)
    45.             Try
    46.                 Dim b As Int32 = Bit
    47.                 Dim ofs As Int32 = Offset
    48.                 Dim temp As Int32 = 0
    49.  
    50.                 ' If the bit we want isn't in the offset provided,
    51.                 ' Move on to the next offset.
    52.                 While b >= 8
    53.                     b -= 8
    54.                     ofs += 1
    55.                 End While
    56.  
    57.                 ' Read the byte.
    58.                 Select Case EditType
    59.                     Case EditTypeEnum.Buffer
    60.                         temp = Common.Buffer(ofs)
    61.                     Case EditTypeEnum.Memory_IRAM
    62.                         temp = ReadIRAM(ofs, 1)
    63.                     Case EditTypeEnum.Memory_WRAM
    64.                         temp = ReadWRAM(ofs, 1)
    65.                 End Select
    66.  
    67.                 ' Invert, if necessary.
    68.                 State = If(Invert, Not State, State)
    69.  
    70.                 ' Manipulate the bit.
    71.                 temp = If(State, temp Or (1 << (7 - b)), temp And Not (1 << (7 - b)))
    72.  
    73.                 ' Write the byte.
    74.                 Select Case EditType
    75.                     Case EditTypeEnum.Buffer
    76.                         Common.Buffer(ofs) = CByte(temp And &HFF)
    77.                     Case EditTypeEnum.Memory_IRAM
    78.                         WriteIRAM(ofs, 1, temp And &HFF)
    79.                     Case EditTypeEnum.Memory_WRAM
    80.                         WriteWRAM(ofs, 1, temp And &HFF)
    81.                 End Select
    82.             Catch ex As Exception
    83.                 GetError(ex)
    84.             End Try
    85.         End Sub
    Prefix has no suffix, but suffix has a prefix.

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