Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Count true + true + true = 3

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Sun Nov 01, 2020 2:28 am    Post subject: Count true + true + true = 3 Reply with quote

i like to count bit value in cheat engine
Code:

Count =
readInteger("Example.exe+ABC123") & (1 << 0)  +
readInteger("Example.exe+ABC124") & (1 << 0)  +
readInteger("Example.exe+ABC125") & (1 << 0) 
Print(Count)

and answer should be 3
how to do that?
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Sun Nov 01, 2020 3:32 am    Post subject: Reply with quote

May read https://en.wikipedia.org/wiki/Hamming_weight , or search popcount

tl;dr
Lua in wiki's last 64 bit popcount:
Code:
function popcount64(x)
  x = x - ((x >> 1) & 0x5555555555555555)
  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
  x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
  return (x * 0x0101010101010101) >> 56
end
--test
for i=0,15 do
  local j = (i<<16) | i --  x2
  local k = (i<<50) | (i<<40) | (i<<30) | (i<<20) | (i<<10) | i  -- x6
  print(i,popcount64(i),popcount64(j),popcount64(k))
end
--- ie.
local x = readInteger("Example.exe+ABC123")
print(x and popcount64(x) or 'not readable')


_________________
- Retarded.
Back to top
View user's profile Send private message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Sun Nov 01, 2020 3:51 am    Post subject: Reply with quote

thanks for the reply,
to be honest I do not understand any Lua script you write (sorry),
anyway this is the substitute code I write
Code:

Count = 0
if (readInteger("example.exe+123ABC") & (1 << 0) == 1) then
  Count = Count + 1
end
if (readInteger("example.exe+123ABD") & (1 << 0) == 1) then
  Count = Count + 1
end
if (readInteger("example.exe+123ABE") & (1 << 0) == 1) then
  Count = Count + 1
end
Print(Count)

it seems the code I write quite long and not so efficient (it using 3 "IF")
and I just want to make it a single line(like the code in my post)
thanks again for reply, sorry if i rude

EDIT : maybe the better question : how to convert Bit data type to numeral data type like byte or int
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Sun Nov 01, 2020 5:47 am    Post subject: Reply with quote

Look like you only need count the 1st bit of 3 consecutive bytes, may be this...

Code:

-- common memory read
local value = readInteger("example.exe+123ABc")-- read 4 bytes, no need to read each one by one
--  apply popcount64 with values masked by 0x010101
local count = value and popcount64(value & 0x010101)
-- or
-- count individual bit by shift and masks x3
local count = value and (value & 1) + ((value>>8) & 1) +((value>>16) & 1)


btw, (1 << 0) is just 1.

_________________
- Retarded.
Back to top
View user's profile Send private message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Sun Nov 01, 2020 6:07 am    Post subject: Reply with quote

thank you for the answer,
but I too stupid to understand what your code does,
I think i will stick with that "long and not so efficient" for now,

thank you again, and sorry for the inconvenient
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites