Bitwise Operations on Memory larger than 64bits

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Bitwise Operations on Memory larger than 64bits

Post by swhite »

Hi

I have a need for using bitwise operators (And, Or, XOR) on values in excess of 128bits so I wondered if there were any tools in Purebasic for this type of thing. I am assuming if not that I could just do 64bits at a time and store the result in a memory buffer.

Thanks,
Simon
Simon White
dCipher Computing
User avatar
idle
Always Here
Always Here
Posts: 5098
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Bitwise Operations on Memory larger than 64bits

Post by idle »

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Bitwise Operations on Memory larger than 64bits

Post by mk-soft »

You can use structures ...

Code: Select all

;-TOP

Structure ArrayOfAny
  a.a[0]
  b.b[0]
  w.w[0]
  u.u[0]
  l.l[0]
  q.q[0]
EndStructure

Structure Var128
  StructureUnion
    q.q[2]
    splice.ArrayOfAny
  EndStructureUnion
EndStructure

Procedure And128(*Result.Var128, *Value1.Var128, *Value2.Var128)
  *Result\q[0] = *Value1\q[0] & *Value2\q[0]
  *Result\q[1] = *Value1\q[1] & *Value2\q[1]
EndProcedure

Procedure Or128(*Result.Var128, *Value1.Var128, *Value2.Var128)
  *Result\q[0] = *Value1\q[0] | *Value2\q[0]
  *Result\q[1] = *Value1\q[1] | *Value2\q[1]
EndProcedure

Procedure XOr128(*Result.Var128, *Value1.Var128, *Value2.Var128)
  *Result\q[0] = *Value1\q[0] ! *Value2\q[0]
  *Result\q[1] = *Value1\q[1] ! *Value2\q[1]
EndProcedure

Procedure.s Hex128(*Value.Var128)
  Protected r1.s
  r1 = RSet(Hex(*Value\q[1], #PB_Quad), 16, "0")
  r1 + RSet(Hex(*Value\q[0], #PB_Quad), 16, "0")
  ProcedureReturn r1
EndProcedure

  
Define v1.Var128
Define v2.Var128
Define r1.Var128

v1\q[0] = $FF00000001020304 ; Low Quad
v1\q[1] = $00FFFFFF10203040 ; High Quad

v2\q[0] = $F0F0F0F0F0F0F0F0
v2\q[1] = $F0F0F0F0F0F0F0F0

And128(r1, v1, v2)

Debug Hex128(v1)
Debug Hex128(v2)
Debug "And"
Debug Hex128(r1)

Debug "----"

Debug "Splice v1"
For i = 0 To 15
  Debug RSet(Hex(v1\splice\a[i]), 2, "0")
Next

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Bitwise Operations on Memory larger than 64bits

Post by swhite »

Thank-you that is quite useful.

Simon
Simon White
dCipher Computing
Post Reply