| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| AylinCE Grandmaster Cheater Supreme
 
  Reputation: 37 
 Joined: 16 Feb 2017
 Posts: 1527
 
 
 | 
			
				|  Posted: Fri Apr 18, 2025 6:00 am    Post subject: Lua on bit32.. (@DB, @atom0s) |   |  
				| 
 |  
				| I see that "bit32" is marked as the syntax in CE. 
 But when I want to use it, it gives "undefined, nil".
 Instead, I had to define "bit32" for "XOR".
 
 Probably because "bit32" was deprecated after Lua 5.2 and a 64 bit version was defined. (5.2+)
 
 My question: Which one do you find more useful?
 
 Of course, the intended use is also important.
 Here is a standalone example:
 
 
  	  | Code: |  	  | -- Defining bit32 XOR function manually local bit32 = {}
 
 function bit32.bxor(a, b)
 local result = 0
 local bit = 1
 while a > 0 or b > 0 do
 local a_bit = a % 2
 local b_bit = b % 2
 local xor_bit = (a_bit + b_bit) % 2
 result = result + xor_bit * bit
 a = math.floor(a / 2)
 b = math.floor(b / 2)
 bit = bit * 2
 end
 return result
 end
 
 -- XOR Encryption Function
 function xorEncryptDecrypt(input, key)
 local output = {}
 local out2 = ""
 for i = 1, #input do
 local char = string.byte(input, i)
 local encryptedChar = bit32.bxor(char, key)
 table.insert(output, string.char(encryptedChar))
 end
 out2 = table.concat(output)
 out2 = string.gsub(out2, "[\n\r]", " ")
 return out2
 end
 
 -- Encryption Key
 local key = 42
 
 -- Text to be Encrypted
 local originalText = "print('This is a test text.')"
 
 -- Encode
 local encryptedText = xorEncryptDecrypt(originalText, key)
 print("Encode Text:")
 print(encryptedText)
 -- Ahh.. It would be better to write the output to a ".lua" file. ;)
 
 -- Decode
 local decryptedText = xorEncryptDecrypt(encryptedText, key)
 print("Decode Text:")
 print(decryptedText)
 | 
 
 Or "64-bit" example:
 
 
  	  | Code: |  	  | -- 64-bit values local a = 0x12345678ABCDEF01  -- A 64-bit number
 local b = 0xF0F0F0F0F0F0F0F0  -- Another 64-bit number
 
 -- Bitwise "AND" operation
 local and_result = a & b
 print(string.format("AND result: 0x%X", and_result))
 
 -- Bitwise "OR" operation
 local or_result = a | b
 print(string.format("OR result: 0x%X", or_result))
 
 -- Bitwise "XOR" operation
 local xor_result = a ~ b
 print(string.format("XOR result: 0x%X", xor_result))
 
 -- Bitwise shifts (left and right)
 local shift_left = a << 4  -- Shift left by 4 bits
 local shift_right = b >> 4 -- Shift right by 4 bits
 print(string.format("Left shift: 0x%X", shift_left))
 print(string.format("Right shift: 0x%X", shift_right))
 | 
 
 Results:
 
  	  | Code: |  	  | AND result: 0x10305070A0C0E000 OR result: 0xF2F4F6F8FBFDFFF1
 XOR result: 0xE2C4A6885B3D1FF1
 Left shift: 0x2345678ABCDEF010
 Right shift: 0xF0F0F0F0F0F0F0F
 | 
 
 On the other hand, I can see that the required operation works without using "bit32":
 
  	  | Code: |  	  | function xorEncryptDecrypt(input, key) local output = {}
 for i = 1, #input do
 local char = string.byte(input, i)
 local encryptedChar = char ~ key
 table.insert(output, string.char(encryptedChar))
 end
 local singleLineOutput = table.concat(output)
 singleLineOutput = string.gsub(singleLineOutput, "[\n\r]", " ")
 return singleLineOutput
 end
 | 
 
 "bit32" is not defined in "celua.txt" but it is still marked in Lua Script.
 Why?
 _________________
 
 |  |  
		| Back to top |  |  
		|  |  
		| ParkourPenguin I post too much
 
  Reputation: 152 
 Joined: 06 Jul 2014
 Posts: 4706
 
 
 | 
			
				|  Posted: Fri Apr 18, 2025 11:04 am    Post subject: |   |  
				| 
 |  
				| Use Lua's builtin binary ops. e.g. xor: `~` 
 
 Lua 5.2's bit32 stdlib was deprecated in favor of Lua 5.3's new binary ops. 	  | AylinCE wrote: |  	  | "bit32" is not defined in "celua.txt" but it is still marked in Lua Script. Why?
 | 
 It still gets highlighted because DB didn't remove the code that highlighted "bit32". I don't think this has anything to do with backwards compatibility... perhaps it was simply forgotten.
 _________________
 
 I don't know where I'm going, but I'll figure it out when I get there. |  |  
		| Back to top |  |  
		|  |  
		| AylinCE Grandmaster Cheater Supreme
 
  Reputation: 37 
 Joined: 16 Feb 2017
 Posts: 1527
 
 
 | 
			
				|  Posted: Fri Apr 18, 2025 1:43 pm    Post subject: |   |  
				| 
 |  
				|  	  | ParkourPenguin wrote: |  	  | ... | 
 
 Probably as you said.
 I am evaluating applications to shorten some codes.
 
 Actually, he may have forgotten the emphasis because there is no need for "bit32".
 
 I am just wondering if there are other options.
   _________________
 
 |  |  
		| Back to top |  |  
		|  |  
		| atom0s Moderator
 
  Reputation: 205 
 Joined: 25 Jan 2006
 Posts: 8587
 Location: 127.0.0.1
 
 |  |  
		| Back to top |  |  
		|  |  
		| AylinCE Grandmaster Cheater Supreme
 
  Reputation: 37 
 Joined: 16 Feb 2017
 Posts: 1527
 
 
 | 
			
				|  Posted: Sat Apr 19, 2025 2:07 am    Post subject: |   |  
				| 
 |  
				| @atom0s, Thanks for the answer. 
 As far as I understand, you are using older versions of Lua and still recommending newer versions.
 
 On the other hand, what interested me was the integration of Lua with CE.
 I could not really adopt Lua outside of CE.
 
 Also, considering Lua in your case, I cannot calculate the layers. Because the level you are at (LuaJIT) reminds me of the following saying;
 
 "LuaJIT is like a superhero costume for Lua: Faster, more powerful and more impressive!"
 
 (I will not bother you unnecessarily by asking why it does not work with versions after 5.1.)
 
 Yes, I think the marked status of "bit32" is a forgotten or "if needed" detail to be revisited.
 Frankly, there is an alternative and I think it does not need to be defined.
 _________________
 
 |  |  
		| Back to top |  |  
		|  |  
		|  |  
  
	| 
 
 | You cannot post new topics in this forum You cannot reply to topics in this forum
 You cannot edit your posts in this forum
 You cannot delete your posts in this forum
 You cannot vote in polls in this forum
 You cannot attach files in this forum
 You can download files in this forum
 
 |  |