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 


Read/display/format a single bit.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Rydian
Grandmaster Cheater Supreme
Reputation: 31

Joined: 17 Sep 2012
Posts: 1358

PostPosted: Wed Apr 29, 2015 1:03 am    Post subject: Read/display/format a single bit. Reply with quote

So there's readFloat, readInteger, readDouble, etc.

But there's no functions for reading 2-byte values, single-byte values, or single bits.

2-byte and single-byte can be gotten doing a band with 0xFFFF or 0xFF, but how would I go about getting the value of individual bits to display?

_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25291
Location: The netherlands

PostPosted: Wed Apr 29, 2015 2:53 am    Post subject: Reply with quote

Code:
 
function isBitSet(value,  bitnumber)
  return bAnd(value,  bShl(1,bitnumber)) ~= 0
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Wed Apr 29, 2015 5:04 am    Post subject: Reply with quote

made a heavy bitfield function:

Code:
function bitField(a,b)-- a,b < 64, bit number is zero based
  local function isInteger(p) return type(p)=='number' and math.floor(p) == p end
  assert(isInteger(a) and a >= 0 and a < 64, '1st arg must be integer between 0 and 63 inclusive')
  b = type(b)=='number' and math.floor(b)==b and b >= 0 and b < 64 and b or a -- default 1 bit bit-field (a,a)
  if a > b then a,b = b,a end -- made sure a <= b
  return function(v,Address)
    local Addr = Address or nil
    if Addr and readQword(Addr) then -- read value from address if address specificied
      v = readQword(Addr)
    else
      v = isInteger(v) and v or 0 -- default value 0 on initialize
    end
    local this = {}
    local Cache,Value =nil, v
    function this:get()
      if Cache == nil then
        Cache = 0
        local n,m = 1,2^a
        for i=a,b do
          if bAnd(Value,m)~=0 then Cache = Cache + n end
          n,m = 2 * n,2 *m
        end
      end
      return Cache
    end

    function this:set(p)
      assert(isInteger(p),'Integer needed')
      local n,m = 1,2^a
      for i=a,b do
        local bm,bn = bAnd(Value,m), bAnd(p,n)
        if bm~=bShl(bn,a) then
          Value = bn == 0 and Value - bm or Value + bShl(bn,a)
          Cache = nil -- reset cache
        end
        n,m = 2*n,2 *m
      end
      return self
    end

    function this:getValue() return Value end
    function this:setValue(v)
      assert(isInteger(v),'Integer needed')
      Cache,Value = nil,v
      return self
    end
    function this:read(addr)
      addr = addr or Addr
      this:setValue(readQword(addr))
      return self
    end
    function this:write(addr) -- caution if value at addr changed during modifying bit field
      addr = addr or Addr
      writeQword(addr,this:getValue())
      return self
    end
    return this
  end
end

-- test
local bf2_5 = bitField(2,5) -- bf2_5 is a function to create bitfield 2bit-5bit object
local bf = bf2_5()
print(bf:get())
bf = bf2_5(0xaf)
print(bf:get()) ---          10(1011)11 --> bf(2,5)= 1011 = 11
bf:set(7)       -- change to 10(0111)11 -> value = 0x9f = 159
print(bf:getValue())
bf:setValue(0xff)
print(bf:get()) ---          11(1111)11 --> bf(2,5)= 1111 = 15

autoAssemble('globalalloc(TESTZONE,256)')

writeInteger('TESTZONE',-1)
writeInteger('TESTZONE+04',16)
bf = bf2_5(nil,'TESTZONE')
local shex = function(n) return string.format('%02X',n) end
print('before write memory:',shex(bf:getValue()))             -- should be 0x10ffffffff, coz getValue return 64-bit integer
bf:read():set(0):write() -- read address again, change bit field, write it back to memory
print(' after write memory:',shex(readInteger('TESTZONE')))   -- should be 0xffffffc3

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