AntonioVillanueva

swap function with generic parameters for Int KOTLIN

Nov 29th, 2021
927
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *swap function with generic parameters for Int  KOTLIN
  3.  * Antonio Villanueva Segura
  4.  */
  5.  
  6. fun main() {
  7.     var ushort:UShort=0xABCDU //UShort kotlin.Unit
  8.     var uint:UInt=0x1234U    //UInt kotlin.Unit
  9.     var int:Int=0x5678    //UInt kotlin.Unit
  10.     var short:Int=0x9ABC    //UInt kotlin.Unit    
  11.     println ( "%04x".format (swap (ushort.toInt())))
  12.     println ( "%04x".format (swap (uint.toInt())))
  13.     println ( "%04x".format (swap (int.toInt())))  
  14.     println ( "%04x".format (swap (short.toInt())))    
  15.  
  16. }
  17.  
  18. //Swap Fun
  19. fun <T>swap(w:T) :T{
  20.     var H:UShort=0x00U
  21.     var L:UShort=0x00U
  22.    
  23.     if ( (w is Int) ){ //Test Only Int
  24.         H=( w.toInt().shr(8) ).toUShort()
  25.         L=( w.toInt().shl(8) ).toUShort()
  26.         return ((H or L).toInt() ) as T
  27.     }
  28.  
  29.         return w //No changed error
  30. }
RAW Paste Data