• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX:SE Bitmask between C++ and XML

Vitus

Resource contributor
Messages
1,480
Country
newzealand
Good evening,

I have a large number of boolean values I wish to communicate between my C++ gauge and XML. Instead of declaring a Lvar for each of the different states, I was wondering if there's a more efficient way to go about this. The thing that comes to mind is using a bitmask to store data in an integer variable. But the question then would be how to unpack this in the XML gauge. Also, what would be the limit of such a system? How many values could I store in an integer/enum value in XML?

Cheers,
Vitus
 
Your variable & 1 if{ .. you have bit 1 set ... }
Your variable & 2 if{ .. you have bit 2 set ... }
Your variable & 4 if{ .. you have bit 3 set ... }
Your variable & 8 if{ .. you have bit 4 set ... }
...
...

You'd have 32 tests for one variable, sounds like complicated way to do it though :oops:
 
It can be easily done just by using doubles in C++, considering LVars are all doubles.

For example you can take 1000000 and add values like they were bits : 1000001 or 1000011 etc.

double base = 1000000;
double export;

if(condA)
export = base + 1;

if(condB)
export = base + 10;

if(condC)
export = base + 100;

etc

finally assign an LVAR with export value

Then in XML
(LVar) sp0
l0 10 % 10 * int 1 == if{ } els{ } <!-- For rightmost digit --> 100000 1 -->
l0 100 % 10 * int 1 == if{ } els{ } <!-- tenths digit -->10000 1 0 -->
l0 1000 % 10 * int 1 == if{ } els{ } <!-- hundreths digit -->10001 0 0 -->

etc

IMO more simple that messing with real bitmask conversion.

Tom
 
Just to clarify: I need nine variables each holding a number of ON/OFF states, some as little as five, some need to hold up to an estimated 30 boolean values. Hence Tom's approach wouldn't be feasible, unless I further split up the variables with more than 10 states. On the other hand a 32 bit integer can hold 32 boolean states, which should be enough. I've never developed such a bitmask system before and so was hoping I could get some input before diving into it.
So in my C++ gauge I'd need to do something like this:
C++:
if (some_state_1 == true)
    glorious_l_var |= 1
if (some_state_2 == true)
    glorious_l_var |= 2
if (some_state_3 == true)
    glorious_l_var |= 4
if (some_state_4 == true)
    glorious_l_var |= 8
    
//etc. etc.

and within my XML I can do this?
Code:
(L:GLORIOUS LVAR,enum) 1 & if{ do_whatever_1 } els{ don't do whatever_1 }
(L:GLORIOUS LVAR,enum) 2 & if{ do_whatever_2 } els{ don't do whatever_2 }
(L:GLORIOUS LVAR,enum) 4 & if{ do_whatever_3 } els{ don't do whatever_3 }
(L:GLORIOUS LVAR,enum) 8 & if{ do_whatever_4 } els{ don't do whatever_4 }
 
I don't know about the C++ side but the XML looks ok - I've never tried it though.

Edit:
Ok, I have now. This works;
Ignore the lube oil pressure, I'm using an old gauge.

XML:
<?xml version="1.0" encoding="utf-8"?>
<Gauge Name="ADF_Settings" Version="1.0">
<Image Name="TestGauge_Background.bmp" Luminous="Yes" ImageSizes="320,240"/>

<!-- Maximum of 7 Entries -->

<!-- General Test Gauge  -->

<Element>
  <Select>
    <Value>5 (&gt;L:BunchOfBits,enum)</Value>
  </Select>
</Element>

<Element>
  <Select>
    <Value>
      (L:BunchOfBits,enum) 1 &amp; if{ 1 (&gt;L:TestVar_Bit0,enum) } els{ 0 (&gt;L:TestVar_Bit0,enum) }
      (L:BunchOfBits,enum) 2 &amp; if{ 1 (&gt;L:TestVar_Bit1,enum) } els{ 0 (&gt;L:TestVar_Bit1,enum) }
      (L:BunchOfBits,enum) 4 &amp; if{ 1 (&gt;L:TestVar_Bit2,enum) } els{ 0 (&gt;L:TestVar_Bit2,enum) }
      (L:BunchOfBits,enum) 8 &amp; if{ 1 (&gt;L:TestVar_Bit4,enum) } els{ 0 (&gt;L:TestVar_Bit4,enum) }
    </Value>
  </Select>
</Element>


<Element>
  <Position X="40" Y="20"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>Test Var Bit_0:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="20"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:TestVar_Bit0,enum) )%!d!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="50"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>Test Var Bit_1:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="50"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:TestVar_Bit1,enum) )%!d!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="80"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>Test Var Bit_2:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="80"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:TestVar_Bit2,enum) )%!d!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="110"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>Test Var Bit_4:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="110"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:TestVar_Bit4,enum) )%!d!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="140"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>A:Oil Press:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="140"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (A:ENG OIL PRESSURE:1,psi) )%!2.2f!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="170"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>L:Oil Press:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="170"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:ENG OIL PRESSURE:1,psi) )%!2.2f!</String>
  </Text>
</Element>

<Element>
  <Position X="40" Y="200"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>BunchOfBits:</String>
  </Text>
</Element>

<Element>
  <Position X="200" Y="200"/>
  <Text X="288" Y="24" Bright="Yes" Length="32" Font="Arial" Color="#00C000" HorizontalAlign="left">
    <String>%( (L:BunchOfBits,enum) )%!d!</String>
  </Text>
</Element>

</Gauge>

Main-1_01Jun19 at 15-56-26.JPG
 
Last edited:
Brilliant! Thanks for your help. I'll try myself and check if the C++ side works.
 
Yep. I put it all together and it works.
I use this to highlight components in the "Maintenance Manual" of my aircraft desk if they're broken. The function on the C++ side looks like this:
C++:
void AircraftDesk::highlightComponent(int component)
{
    highlight |= (int)pow(2,component);    //I use pow() to get into the bit-space of the variable. I have a simple numbering system for the rest of the code to address various components.
    set_named_variable_value(highlight_id,highlight);
}

This was easier than I thought....
 
The irony of this thread is that it is unlikely to have improved performance at all. Bitwise operations can be more CPU cycles than a simple not zero check (boolean). Not sure you actually did yourself a favor, to be honest.
 
In this case, it's not really that important. It's part of my "Aircraft desk" and thus only pops up when the plane is on the ground and maintenance being performed. The C++ part is only called once after the mechanic found issues. The variable is used to highlight the various components in the maintenance manual (which is mostly XML) like this:
0QwgTxy.jpg
 
Back
Top