Bit-Stream Class

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Bit-Stream Class

Post by Eclipzer »

This is a bit-stream class I started developing, primarily for use with my GIF library. A bit-stream is a data structure that allows you to read/write n-bit values. It functions similar to an array, in that each bit is stored in a 'slot' of the bit-stream. However, when you read an n-bit value each of the n-slots are added together to produce the original value.

Class Source:

Code: Select all

' Program:  (Bit) Stream Class Source Code
' Version:  0.01.2a (1st Generation)
' Language: FreeBASIC (v0.18.3b)
' Author:   Quinton Roberts (Eclipzer)
' Date:     09-07-08
' Update:   09-08-08
'
' Copyright (c) 2008.

  type bitStreamClass
    as uinteger  count 'element count
    as uinteger  index 'element position
    as uinteger  limit 'element limit
    as ubyte ptr data    
    declare constructor(bitCount as uinteger=0)
    declare destructor()
    declare sub      append(value as ulongint,bits as ubyte=8)
    declare sub      clear()
    declare sub      create(bitCount as uinteger)
    declare sub      reset(value as uinteger=0)
    declare function get overload(start as uinteger,bits as uinteger) as uinteger
    declare function get         (bits as uinteger) as uinteger
  end type

' Stream Constructors
  constructor bitStreamClass(bitCount as uinteger)
    if bitCount then create bitCount
  end constructor
  
' Stream Destructor
  destructor bitStreamClass: clear: end destructor

' =============================================================================
'          Name: bitStreamClass.append (09.07.08)
'    Parameters:
'         value: container holding the value to append
'        [bits]: number of bits to append [8]
' -----------------------------------------------------------------------------
'   Description: Append a specified numbers of bits to the bit stream.
'      Comments: Bits are added to the stream lo-bit first. Note that each
'                bit uses a single 'slot' on the bit stream.
' =============================================================================
  sub bitStreamClass.append(value as ulongint,bits as ubyte)
    for i as integer=1 to bits
      if count>=limit then exit for         'exit if count exceeds our limit
      data[count]=((value shr (i-1)) and 1) 'strip and store each bit
      count+=1
    next
  end sub

' =============================================================================
'          Name: bitStreamClass.clear (09.07.08)
'    Parameters:
'         value: byte to append
' -----------------------------------------------------------------------------
'   Description: Releases a bit-stream from memory.
'      Comments: Automatically called from the destructor. 
' =============================================================================
  sub bitStreamClass.clear()    
    if data then delete[] data: data=0
  end sub

' =============================================================================
'          Name: bitStreamClass.create (09.07.08)
'    Parameters:
'      bitCount: number of bits the stream can contain
' -----------------------------------------------------------------------------
'   Description: Create a new bit-stream.
'      Comments: Deallocates old stream if present.
' =============================================================================
  sub bitStreamClass.create(bitCount as uinteger)
    if bitCount=0 then return  'return on null bitCount
    if data then clear         'deallocate old stream if necessary
    data = new ubyte[bitCount] 'allocate our new stream
    limit=bitCount             'register limit
  end sub

' =============================================================================
'          Name: bitStreamClass.get (09.08.08)
'    Parameters:
'         start: position to start pulling bits from stream
'          bits: number of bits to pull from stream
' -----------------------------------------------------------------------------
'   Description: Get specified number of bits from stream.
'      Comments: Returns -1 on out-of-bounds access
' =============================================================================
  function bitStreamClass.get(start as uinteger,bits as uinteger) as uinteger
    
    dim as uinteger value=0
  
  ' Return -1 on out-of-bound access
    if start<0      then return -1   'outside lower bound
    if start>=limit then return -1   'outside upper bound
    
    for i as uinteger=0 to bits-1
      if (start+i)>=limit then return -1 'return -1 on out-of-bounds access
      value+=(data[start+i] shl i)      
    next
    
    index=start+bits
    
    return value
    
  end function
  
  function bitStreamClass.get(bits as uinteger) as uinteger
    
    dim as uinteger value=0
    
    if index<0 then return -1
    
    for i as uinteger=0 to bits-1
      if (index+i)>=limit then return -1 'return -1 on out-of-bounds access
      value+=(data[index+i] shl i)      
    next
    
    index+=bits
    
    return value
    
  end function

' =============================================================================
'          Name: bitStreamClass.reset (09.07.08)
'    Parameters:
'       [value]: index to reset stream to [0]
' -----------------------------------------------------------------------------
'   Description: Reset stream
'      Comments:
' =============================================================================
  sub bitStreamClass.reset(value as uinteger)    
    index=value
    count=value
  end sub
Example:

Code: Select all

  #include "bit_stream_002a.bas"
  
  dim as bitStreamClass stream
  dim as integer a=&b1011,b=&b11001,c=&b101101

' Create Bit-Stream
  stream.create(256) 'create a 256bit stream
  
  ? "Original Values:"
  ? "a =   " & bin(a,4) & " 4bits"
  ? "b =  "  & bin(b,5) & " 5bits"
  ? "c = "   & bin(c,6) & " 6bits"
  ?

' Append Various N-bit Values
  stream.append(&b001011,4) 'append 4bits
  stream.append(&b011001,5) 'append 5bits
  stream.append(&b101101,6) 'append 6bits
  
  ? "Bit Count: " & stream.count
  ?
  
' Display Literal Bit-Stream
  stream.index=0
  ? "Literal Bit-Stream"
  for i as integer=stream.count-1 to 0 step -1 'output stream 'backwards' to simulate
    ? stream.get(i,1);
  next
  ? " <-- bit-stream start"
  ?

' Display Conceptual Bit-Stream  
  ? "Conceptual Bit-Stream"
  dim as integer j=stream.count-1
  for i as integer=1 to 6
    ? stream.get(j,1);
    j-=1
  next
  ? "-";
  for i as integer=1 to 5
    ? stream.get(j,1);
    j-=1
  next
  ? "-";
  for i as integer=1 to 4
    ? stream.get(j,1);
    j-=1
  next  
  ?
  ?  
  
' Get N-Bit Values from Bit-Stream
  stream.index=0
  ? "Bit-Stream Values:"
  ? "      " & bin(stream.get(4)) 'get 4bits
  ? "     "  & bin(stream.get(5)) 'get 5bits
  ? "    "   & bin(stream.get(6)) 'get 6bits
  
  sleep
Example + Class Source (Stand-Alone):

Code: Select all

' Program:  (Bit) Stream Class Source Code
' Version:  0.01.2a (1st Generation)
' Language: FreeBASIC (v0.18.3b)
' Author:   Quinton Roberts (Eclipzer)
' Date:     09-07-08
' Update:   09-08-08
'
' Copyright (c) 2008.

  type bitStreamClass
    as uinteger  count 'element count
    as uinteger  index 'element position
    as uinteger  limit 'element limit
    as ubyte ptr data    
    declare constructor(bitCount as uinteger=0)
    declare destructor()
    declare sub      append(value as ulongint,bits as ubyte=8)
    declare sub      clear()
    declare sub      create(bitCount as uinteger)
    declare sub      reset(value as uinteger=0)
    declare function get overload(start as uinteger,bits as uinteger) as uinteger
    declare function get         (bits as uinteger) as uinteger
  end type

' Stream Constructors
  constructor bitStreamClass(bitCount as uinteger)
    if bitCount then create bitCount
  end constructor
  
' Stream Destructor
  destructor bitStreamClass: clear: end destructor

' =============================================================================
'          Name: bitStreamClass.append (09.07.08)
'    Parameters:
'         value: container holding the value to append
'        [bits]: number of bits to append [8]
' -----------------------------------------------------------------------------
'   Description: Append a specified numbers of bits to the bit stream.
'      Comments: Bits are added to the stream lo-bit first. Note that each
'                bit uses a single 'slot' on the bit stream.
' =============================================================================
  sub bitStreamClass.append(value as ulongint,bits as ubyte)
    for i as integer=1 to bits
      if count>=limit then exit for         'exit if count exceeds our limit
      data[count]=((value shr (i-1)) and 1) 'strip and store each bit
      count+=1
    next
  end sub

' =============================================================================
'          Name: bitStreamClass.clear (09.07.08)
'    Parameters:
'         value: byte to append
' -----------------------------------------------------------------------------
'   Description: Releases a bit-stream from memory.
'      Comments: Automatically called from the destructor. 
' =============================================================================
  sub bitStreamClass.clear()    
    if data then delete[] data: data=0
  end sub

' =============================================================================
'          Name: bitStreamClass.create (09.07.08)
'    Parameters:
'      bitCount: number of bits the stream can contain
' -----------------------------------------------------------------------------
'   Description: Create a new bit-stream.
'      Comments: Deallocates old stream if present.
' =============================================================================
  sub bitStreamClass.create(bitCount as uinteger)
    if bitCount=0 then return  'return on null bitCount
    if data then clear         'deallocate old stream if necessary
    data = new ubyte[bitCount] 'allocate our new stream
    limit=bitCount             'register limit
  end sub

' =============================================================================
'          Name: bitStreamClass.get (09.08.08)
'    Parameters:
'         start: position to start pulling bits from stream
'          bits: number of bits to pull from stream
' -----------------------------------------------------------------------------
'   Description: Get specified number of bits from stream.
'      Comments: Returns -1 on out-of-bounds access
' =============================================================================
  function bitStreamClass.get(start as uinteger,bits as uinteger) as uinteger
    
    dim as uinteger value=0
  
  ' Return -1 on out-of-bound access
    if start<0      then return -1   'outside lower bound
    if start>=limit then return -1   'outside upper bound
    
    for i as uinteger=0 to bits-1
      if (start+i)>=limit then return -1 'return -1 on out-of-bounds access
      value+=(data[start+i] shl i)      
    next
    
    index=start+bits
    
    return value
    
  end function
  
  function bitStreamClass.get(bits as uinteger) as uinteger
    
    dim as uinteger value=0
    
    if index<0 then return -1
    
    for i as uinteger=0 to bits-1
      if (index+i)>=limit then return -1 'return -1 on out-of-bounds access
      value+=(data[index+i] shl i)      
    next
    
    index+=bits
    
    return value
    
  end function

' =============================================================================
'          Name: bitStreamClass.reset (09.07.08)
'    Parameters:
'       [value]: index to reset stream to [0]
' -----------------------------------------------------------------------------
'   Description: Reset stream
'      Comments:
' =============================================================================
  sub bitStreamClass.reset(value as uinteger)    
    index=value
    count=value
  end sub
  
' -----------------------------------------------------------------------------
' Program Start!
' -----------------------------------------------------------------------------

  dim as bitStreamClass stream
  dim as integer a=&b1011,b=&b11001,c=&b101101

' Create Bit-Stream
  stream.create(256) 'create a 256bit stream
  
  ? "Original Values:"
  ? "a =   " & bin(a,4) & " 4bits"
  ? "b =  "  & bin(b,5) & " 5bits"
  ? "c = "   & bin(c,6) & " 6bits"
  ?

' Append Various N-bit Values
  stream.append(&b001011,4) 'append 4bits
  stream.append(&b011001,5) 'append 5bits
  stream.append(&b101101,6) 'append 6bits
  
  ? "Bit Count: " & stream.count
  ?
  
' Display Literal Bit-Stream
  stream.index=0
  ? "Literal Bit-Stream"
  for i as integer=stream.count-1 to 0 step -1
    ? stream.get(i,1);
  next
  ? " <-- bit-stream start"
  ?

' Display Conceptual Bit-Stream  
  ? "Conceptual Bit-Stream"
  dim as integer j=stream.count-1
  for i as integer=1 to 6
    ? stream.get(j,1);
    j-=1
  next
  ? "-";
  for i as integer=1 to 5
    ? stream.get(j,1);
    j-=1
  next
  ? "-";
  for i as integer=1 to 4
    ? stream.get(j,1);
    j-=1
  next  
  ?
  ?  
  
' Get N-Bit Values from Bit-Stream
  stream.index=0
  ? "Bit-Stream Values:"
  ? "      " & bin(stream.get(4)) 'get 4bits
  ? "     "  & bin(stream.get(5)) 'get 5bits
  ? "    "   & bin(stream.get(6)) 'get 6bits
  
  sleep  
Quadrescence
Posts: 10
Joined: Jul 30, 2008 17:28
Location: Minnesota, USA
Contact:

Post by Quadrescence »

This looks like it could be useful for some "bit wizardry". :D Keep us updated.
Post Reply