Page 1 of 1

how to express a byte value?

Posted: Tue Jan 04, 2011 4:54 am
by tseyfarth
Hello all.
How do you properly express &H80000000 in PB?


Thank you
Tim

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 5:19 am
by netmaestro
You could cast it to a type large enough to hold it without the sign affecting the result:

Code: Select all

num.q = $80000000
Debug num
Or, if all you want to do is display the correct value, another option:

Code: Select all

num.l = $80000000
Debug StrU(num, #PB_Long)

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 5:25 am
by tseyfarth
But I need to do some math with it.

I guess PB is not real strong on bit/byte manipulation? I need to work with these from time to time when dealing with external uC based units. Again, am real green on PB so probably just me....

Any ideas?
Thank you!
Tim

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 6:39 am
by tseyfarth
These are the functions I am trying to port from VB6

Code: Select all

Procedure.i LShiftLong(Value.i, Shift.w); As Long
Define ReturnValue.i
;Define Si.w
    MakeOnBits()
     If (Value & (2 | (31 - Shift))) :Goto OverFlow :EndIf
    ReturnValue = ((Value & OnBits(31 - Shift)) * (2 | Shift))
    ProcedureReturn  ReturnValue

OverFlow:
  ;LShiftLong = ((Value And OnBits(31 - (Shift + 1))) * (2 ^ (Shift))) Or &H80000000
    ReturnValue = ((Value & OnBits(31 - (Shift + 1))) * (2 ^ (Shift))) | &H80000000
    ProcedureReturn ReturnValue
EndProcedure

Procedure.i RShiftLong( Value.i,Shift.i); As Long
Define ReturnValue.i
 Define hi.i
    MakeOnBits()
    If Value & &H80000000
       hi = &H40000000
     EndIf
  
    ReturnValue.i = (Value & &H7FFFFFFE) \ (2 | Shift)
    ReturnValue.i = (RShiftLong | (hi \ (2 | (Shift - 1))))
    ProcedureReturn ReturnValue.i
EndProcedure

Procedure MakeOnBits()
Define ReturnValue.i
Define j.i
Define v.i

    For j = 0 To 30
      v = v + (2 | j)
      OnBits(j) = v
    Next j
    OnBits(j) = v + &H80000000
EndProcedure

Maybe there is a better way of doing this in PB that I am not aware of.

Tim

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 8:54 am
by Demivec
tseyfarth wrote:These are the functions I am trying to port from VB6
Any idea what the functions are supposed to do? That is the real key to porting them.

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 11:02 am
by Trond
tseyfarth wrote:Hello all.
How do you properly express &H80000000 in PB?


Thank you
Tim
how to express a byte value?
$80000000 is not a byte value, it can't fit in a byte. But you can put it in a long, integer or quad variable.

Your functions are named lshift and rshift, I'm guessing they shift bits right and left. This is built-in in PB. Take a look in the help file for arithmetic shift left and right. If the functions happen to rotate rather than shift, you can easily do that with inline assembly.

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 2:52 pm
by Baldrick
Just a little add on to what Trond has said above about bitshifting. be careful when you are bitshifting with large numbers such as you have indicated with the mask value from your original post as PB does not have any unsigned long & potentially will give you a headache trying to work out where things go wrong when you shift a negative number to the right mainly. ( Just use a 64 bit type such as a quad instead to avoid this, which of course depends I guess on exactly what it is you are trying to achieve )

Code: Select all

a.l=$80000000
Debug a
Debug Bin(a)
a>>31
Debug a
Debug Bin(a)
b.q=$80000000
Debug b
Debug Bin(b)
b>>31
Debug b
Debug Bin(b)

Re: how to express a byte value?

Posted: Tue Jan 04, 2011 5:56 pm
by tseyfarth
I don't recall the purpose :( But I do know that the numbers are always positive.

Will look for and try the bit shifting built in.

TY
Tim