December 04, 2020, 11:33:45 AM

Author Topic: [bmx] Integers inside longer integers by Hezkore [ 1+ years ago ]  (Read 382 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Integers inside longer integers
Author : Hezkore
Posted : 1+ years ago

Description : A simple way to store 2 bytes in a short, 4 bytes in an int or 2 shorts in an int.

Code :
Code: BlitzMax
  1. '=Usage===================================================================
  2. 'Storage variables
  3. Local byte_myShort:Short
  4. Local byte_myInt:Int
  5. Local short_myInt:Int
  6.  
  7. '--------------------------------
  8. 'Store bytes in our short
  9. storeByte_short(byte_myShort, 26, 0)
  10. storeByte_short(byte_myShort, 205, 1)
  11.  
  12. 'Show stored bytes from our short
  13. Print("(Byte)Short: " + Right(Bin(byte_myShort), SizeOf(byte_myShort) * 8))
  14. Print(getByte_short(byte_myShort, 0))
  15. Print(getByte_short(byte_myShort, 1))
  16. Print()
  17. Delay(1000)
  18.  
  19. '--------------------------------
  20. 'Store bytes in our int
  21. storeByte_int(byte_myInt, 3, 0)
  22. storeByte_int(byte_myInt, 26, 1)
  23. storeByte_int(byte_myInt, 128, 2)
  24. storeByte_int(byte_myInt, 205, 3)
  25.  
  26. 'Show stored bytes from our int
  27. Print("(Byte)Int: " + Right(Bin(byte_myInt), SizeOf(byte_myInt) * 8))
  28. Print(getByte_int(byte_myInt, 0))
  29. Print(getByte_int(byte_myInt, 1))
  30. Print(getByte_int(byte_myInt, 2))
  31. Print(getByte_int(byte_myInt, 3))
  32. Print()
  33. Delay(1000)
  34.  
  35. '--------------------------------
  36. 'Store shorts in our int
  37. storeShort_int(short_myInt, 256, 0)
  38. storeShort_int(short_myInt, 65535, 1)
  39.  
  40. 'Show stored shorts from our int
  41. Print("(Short)Int: " + Right(Bin(short_myInt), SizeOf(short_myInt) * 8))
  42. Print(getShort_int(short_myInt, 0))
  43. Print(getShort_int(short_myInt, 1))
  44. Print()
  45. Delay(1000)
  46.  
  47. End
  48. '=Functions===============================================================
  49. Function storeByte_int(pntr:Int Var, value:Byte, pos:Byte)              'Holds 4 Bytes
  50.         pntr:+(value Shl ((pos Mod SizeOf(pntr)) Shl 3))
  51. End Function
  52. Function getByte_int:Byte(pntr:Int Var, pos:Byte)                               'Holds 4 Bytes
  53.         Return(pntr Shr ((pos Mod SizeOf(pntr)) Shl 3))
  54. End Function
  55.  
  56. Function storeByte_short(pntr:Short Var, value:Byte, pos:Byte)  'Holds 2 Bytes
  57.         pntr:+(value Shl ((pos Mod SizeOf(pntr)) Shl 3))
  58. End Function
  59. Function getByte_short:Byte(pntr:Short Var, pos:Byte)                   'Holds 2 Bytes
  60.         Return(pntr Shr ((pos Mod SizeOf(pntr)) Shl 3))
  61. End Function
  62.  
  63. Function storeShort_int(pntr:Int Var, value:Short, pos:Byte)    'Holds 2 Shorts
  64.         pntr:+(value Shl ((pos Mod SizeOf(pntr)) Shl 4))
  65. End Function
  66. Function getShort_int:Short(pntr:Int Var, pos:Byte)                             'Holds 2 Shorts
  67.         Return(pntr Shr ((pos Mod SizeOf(pntr)) Shl 4))
  68. End Function


Comments :


virtlands(Posted 1+ years ago)

 I like this "Integers inside Integers" code.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal