Results 1 to 9 of 9

Thread: Bitwise Operations and Array Access

  1. #1
    Guest

    Question

    I have an array of BYTEs and would like to know if anyone knows how I can access the array a word at a time (2 bytes).
    At the moment I loop through the array and step 2 horizontal positions. eg.

    for i = 0 to 64
    jor j = 0 to 64 step 2
    array(j,i) = low byte
    array(j+1, i) = high byte
    next j
    next i

    also. is it possible to do bit shifts in vb. ala the c++ operators >> and <<. or do I have to settle for divides.




  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What you're doing there seems like the most likely solution.

    Unfortunately there are no bitwise shift operators in VB. I guess you could write a C++ function in a dll to do it, but that might be a bit over-the-top
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553

    Talking

    Hohohooo! Yes you can shift in VB!

    Examples:

    C language:
    Code:
      int a;
      int b;
      b = a<<4;  // which means b = a * (2^4)
    VB equivalent:
    Code:
      Dim a As Integer;
      Dim b As Integer;
    
      b = a * (2^4)
    So, to concatenate two bytes into ONE word, do the following:
    Code:
      Dim word As Long
      '...For Next loop
      word = (High_Byte * &H100) + Low_Byte 'This performs a 8-bit shift to the left (2^8=256) of the high-order byte, added with the low-order byte
    By this, you can fill an array of long integers.

    USE LONG INTEGERS, because VB can't handle unsigned int like C. A Long Integer prevents the Overflow Error (since Bit 15 can't be 1 in a VB Integer).



  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    The whole point was whether he could use shifts to do arithmetic operations. You'd want to do that for the sake of speed, no? Doing it like that isn't going to save any performance.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Guest
    Mad Compie, thanks for your input, but as HarryW said. I'm trying to do bit shifts to increase speed. At the moment I am using a * 2 ^ n to do bit shifts.

    Also with the array, my problem is it needs to be an array of BYTEs. At the moment I have a word. And I am splitting it into a high and low byte. But would like to know if I could access the array of BYTEs a word at a time.

    I'm am using the array of bytes to get direct access to video memory in direct draw. But when I use a 16bit surface every pixel consists of 2 bytes and not one. And instead of passing the high byte and then the low byte. I'd like to pass the whole WORD to the array at a time.

    Hope this explains a little better.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hmm, maybe you could use copymemory to convert the byte array to a integer array?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Array of Bytes to Array of Integers

    I presume Integer is OK for what you want?

    I might be on the wrong track to help you, but in case this does give you a start, here is a real simple

    Code:
    ' in a module
    Option Explicit
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    In a form or wherever
    Code:
    Option Explicit
    Dim array1(0 To 7) As Byte
    Dim array2(0 To 3) As Integer
    Private Sub Form_Load()
      Dim c As Integer
      Debug.Print array2(0), array2(1)
      For c = 0 To 7
        array1(c) = c+1
        Debug.Print array1(c)
      Next
      CopyMemory array2(0), array1(0), 8
      ' should get 4 integers made up of the following lo/hi bytes:
      ' 1,2 = 257
      ' 3,4 = 1027
      ' 5,6 = 1541
      ' 7,8 = 2055
      Debug.Print array2(0), array2(1), array2(2), array2(3)
      
    End Sub
    Do you see how it works? CopyMemory is really quite fast for what you want to do.

    Regards
    Paul Lewis

  8. #8
    Guest
    Thanks, that's just what I needed. However I'm using it the other way around. By copying the array of integers to the array of Bytes. I was pleased to see that the high and low bytes get seperated automatically. Thanks again.

    btw. I was using this to do say plasma and fire effects directly in video memory with directdraw. but I've decided its not practical to do in 16bit. it seems allot of effort to approximate the correct colour intensities for the different pixels when you're not using a pallette. Do any of you use pixelformats higher than 8bits to do these effects. I could precalculate the 256 colour pallette into 16bit values and store them in a lookup table but that kinda defeats the point. Anyway, just thought I'd mention it.

    Thanks for the feedback




  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Sorry

    Sorry Dirk Kok, not me.

    I replied due to the small but sometimes useful tidbit of knowledge I had on CopyMemory. I am starting to get interested in graphics programming so I've started reading the Q&A in this forum.

    I find that one of the better ways to learn is to read Q&A that other more experienced users have posted. Oh, that plus a few books


    Cheers
    Paul Lewis

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