how to express a byte value?

Just starting out? Need help? Post your questions and find answers here.
tseyfarth
User
User
Posts: 77
Joined: Sun Dec 05, 2010 8:05 pm

how to express a byte value?

Post by tseyfarth »

Hello all.
How do you properly express &H80000000 in PB?


Thank you
Tim
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: how to express a byte value?

Post 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)
BERESHEIT
tseyfarth
User
User
Posts: 77
Joined: Sun Dec 05, 2010 8:05 pm

Re: how to express a byte value?

Post 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
tseyfarth
User
User
Posts: 77
Joined: Sun Dec 05, 2010 8:05 pm

Re: how to express a byte value?

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: how to express a byte value?

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: how to express a byte value?

Post 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.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: how to express a byte value?

Post 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)
tseyfarth
User
User
Posts: 77
Joined: Sun Dec 05, 2010 8:05 pm

Re: how to express a byte value?

Post 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
Post Reply