Continue to Site

# someone please explain me the logic in this vb code

Status
Not open for further replies.

#### techyfubky

##### Member level 1
'Shift bit to the right
dim tempval, amount as long
Dim SendBuff(0 To 255) As Byte
tempval = amount
For index = 1 To 24
tempval = tempval / 2
Next index

SendBuff(6) = tempval And &HFF

tempval = amount
For index = 1 To 16
tempval = tempval / 2
Next index

SendBuff(7) = tempval And &HFF

tempval = amount
For index = 1 To 8
tempval = tempval / 2
Next index

SendBuff(8) = tempval And &HFF
SendBuff(9) = amount And &HFF

The above code splits the Long value "amount " into bytes some lsb in sendbuff(9)..
Please explain me how this happens.....!

Also the following code

' RecvBuff(3)=234, (RecvBuff(2)=1 the values in the bytes
amount = RecvBuff(3)
amount = amount + (RecvBuff(2) * 256)
amount = amount + (RecvBuff(1) * 65536) '256 * 256
amount = amount + (RecvBuff(0) * 16777216) '256 * 256 * 256
tbValue.Text = CStr(amount)

' obtained result is 490' I really dont get the idea behind this logic...

#### AMK1971

##### Member level 3
It is equivalent to

SendBuff(6 ) = (amount / 16777216) And &HFF
SendBuff(7 ) = (amount / 65536) And &HFF
SendBuff(8 ) = (amount / 256) And &HFF
SendBuff(9 ) = amount And &HFF

#### techyfubky

##### Member level 1

Actual operation in which this code is a part of is in writing a numerical value into a block of size 16 byte.

I want to know how this manipulation affects in storing what value in which place..

Can someone show light on this issue please...!

#### AMK1971

##### Member level 3
This code is not affecting the order of bytes, but shifts it 6 places between Receive and send buffer.

i.e. RecvBuff(3) has the Least significant Byte which actually goes to SendBuff(9).

Status
Not open for further replies.