PDA

Click to See Complete Forum and Search --> : Bitwise Operations and Array Access


Sep 11th, 2000, 02:51 PM
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.

HarryW
Sep 12th, 2000, 11:33 AM
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 :)

Mad Compie
Sep 12th, 2000, 12:53 PM
Hohohooo! Yes you can shift in VB!

Examples:

C language:

int a;
int b;
b = a<<4; // which means b = a * (2^4)

VB equivalent:

Dim a As Integer;
Dim b As Integer;

b = a * (2^4)


So, to concatenate two bytes into ONE word, do the following:

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).

HarryW
Sep 12th, 2000, 08:08 PM
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.

Sep 13th, 2000, 01:14 AM
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.

kedaman
Sep 13th, 2000, 06:50 AM
hmm, maybe you could use copymemory to convert the byte array to a integer array?

PaulLewis
Sep 13th, 2000, 09:29 PM
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


' 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

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

Sep 14th, 2000, 06:48 AM
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

PaulLewis
Sep 14th, 2000, 02:20 PM
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