March 28, 2024, 08:53:41 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


The Union command

Started by Andy, August 16, 2017, 11:25:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

August 16, 2017, 11:25:48 PM Last Edit: August 17, 2017, 12:03:17 AM by Andy
The Union command (one I have never used before, and never really noticed it in the help file) was the way to get a Uuint64 value into a hex string - this is the code:

You will not be able to run this yet, not until I post the library and include files.

$include "C:\\Include Files\\Registry\\registrylib.inc"

union dat
uint64 value
uint dword[2]
endunion

def num as dat

num.dword[0] = 0xFfffffff
num.dword[1] = 0xFfffffff

num.value = RegGetQWValue("HKEY_CURRENT_USER\\Software\\Test\\One","q2")

openconsole
print
print
print hex$(num.value)
print
print num.dword[0]
print num.dword[1]
print
print
print
do:until inkey$ <> ""
end


Now, I understand what Clint, Jalih and Fasecero are telling me about splitting up the Uint64 into two dwords (low and high bits), and from what I've read on the Internet a Union is a variable held in memory (in the same location) that can be of any valid variable type.

So in the above code, what is the purpose of these two lines - I presume we are setting the max values for both low and high bit values????


num.dword[0] = 0xFfffffff
num.dword[1] = 0xFfffffff


I'm simply asking for a little clarification on the command as the help file doesn't seem to tell me much about it.

It certainly works, attached is a sample of my RegExport function - note the qwords are correct (but I had to reverse each pair for it to be in the correct format for a .reg file).





Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

jalih

Hi Andy,

It was just to show you the full uint64 range works. It just assigns hi -and lo-dwords.

Andy

Thanks Jalih, I does demonstrate it works, but how?
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

with a union your saying that I want to create variables that sometimes contain one type of information and sometimes contain another type of information.
In the example the following was used:
union dat
uint64 value
uint dword[2]
endunion


The difference between the above and this
type dat
uint64 value
uint dword[2]
endtype

is that when I declare
dat myvar
using the TYPE declaration myvar consumes to pieces of memory at two different memory addresses; the first address points to myvar.value and the second points to myvar.dword[0] and a total of two unit64 or 4 dwords of memory are consume(which ever way you want to look at it)

when I declare
dat myvar
using the UNION declaration myvar consumes one piece of memory at only one memory address and the total amount of memory used is 1 unit64 0r 2 dwords(depending upon how you want to look at it)
when you use myvar.value you are telling the compiler to treat that address as a uint64 value and when you use myvar.dword[0] you're telling it to treat it as an int.

With unions there is nothing to keep you from doing this:
union dat
uint64 value
uint dword[2]
       string wow
endunion


I know I'm not very good at explaining but I hope that helps a little.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library