<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/</id><updated>2020-03-30T09:49:22.239000Z</updated><subtitle>Recent posts to Bit-packing in unions of custom structures</subtitle><entry><title>Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/?limit=25#483b/682d" rel="alternate"/><published>2020-03-30T09:49:22.239000Z</published><updated>2020-03-30T09:49:22.239000Z</updated><author><name>pekkallio</name><uri>https://forge.codesys.com/u/pekkallio/</uri></author><id>https://forge.codesys.comca7d766896d7e462e76d2cf8e135fd2faae7278c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Yes, I'm aware of the problem (see my first message). I would like to avoid using functions here, hence I was asking if there is any way to overcome this byte alignment.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/?limit=25#483b" rel="alternate"/><published>2020-03-24T16:44:31.293000Z</published><updated>2020-03-24T16:44:31.293000Z</updated><author><name>Comingback4u</name><uri>https://forge.codesys.com/u/comingback4u/</uri></author><id>https://forge.codesys.com8854209bb828f881b90467fb8a7772e497f5c851</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I believe your issue is that the structure size is still a byte. I'm not sure if there is a better way but you might want to look into doing something like this (Instead of a union):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;wTest&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SHL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;st7bit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;st3bit&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;111&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/?limit=25#b25c" rel="alternate"/><published>2020-03-23T13:32:29.233000Z</published><updated>2020-03-23T13:32:29.233000Z</updated><author><name>pekkallio</name><uri>https://forge.codesys.com/u/pekkallio/</uri></author><id>https://forge.codesys.comfcf64f61993e4545a3374560d91ed5cb27a7b66c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
here's ST_3bit_7bit:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;TYPE&lt;/span&gt; &lt;span class="nt"&gt;ST_3bit_7bit&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nt"&gt;STRUCT&lt;/span&gt;
    &lt;span class="nt"&gt;st3bit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;ST_3bit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;st7bit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;ST_7bit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_STRUCT&lt;/span&gt;
&lt;span class="nt"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In pic1 results is as expected.&lt;/p&gt;
&lt;p&gt;&lt;img src="./attachment/pic1.png" width="50%"/&gt;&lt;/p&gt;
&lt;p&gt;In pic2 I would expect value 15.&lt;/p&gt;
&lt;p&gt;&lt;img src="./attachment/pic2.png" width="50%"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/?limit=100#76e0" rel="alternate"/><published>2020-03-23T13:00:37.368000Z</published><updated>2020-03-23T13:00:37.368000Z</updated><author><name>dFx</name><uri>https://forge.codesys.com/u/dfx/</uri></author><id>https://forge.codesys.comfba5aebb5231139e8164f87b4d28d7543b75ef71</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Please provide ST_3bit_7bit code (copy/paste issue)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Bit-packing in unions of custom structures</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/88a8ef27da/?limit=25#8822" rel="alternate"/><published>2020-03-19T12:16:06.224000Z</published><updated>2020-03-19T12:16:06.224000Z</updated><author><name>pekkallio</name><uri>https://forge.codesys.com/u/pekkallio/</uri></author><id>https://forge.codesys.com654e71e25b141d3611002242a121d7876204bcf6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;We need to use custom structures of BITS (not BOOLs) in our projects. For example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;TYPE&lt;/span&gt; &lt;span class="nt"&gt;ST_3bit&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nt"&gt;STRUCT&lt;/span&gt;
    &lt;span class="nt"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;x2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;x3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_STRUCT&lt;/span&gt;
&lt;span class="nt"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;...and ST_7bit in the same way. This is &lt;strong&gt;mandatory&lt;/strong&gt; due to requirements of certain devices.&lt;/p&gt;
&lt;p&gt;I combine 3bit and 7bit with a further structure:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;TYPE&lt;/span&gt; &lt;span class="nt"&gt;ST_3bit&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nt"&gt;STRUCT&lt;/span&gt;
    &lt;span class="nt"&gt;x1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;x2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;x3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BIT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_STRUCT&lt;/span&gt;
&lt;span class="nt"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then, I do a union of the combined structure with WORD.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;TYPE&lt;/span&gt; &lt;span class="nt"&gt;U_Test&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nt"&gt;UNION&lt;/span&gt;
    &lt;span class="nt"&gt;st3_7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;ST_3bit_7bit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;wTest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;WORD&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_UNION&lt;/span&gt;
&lt;span class="nt"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This doesn't work as I would expect. st3bit is copied to WORD correctly but st7bit is byte-aligned to the &lt;strong&gt;higher byte&lt;/strong&gt; of the WORD.&lt;/p&gt;
&lt;p&gt;Example: uUnionTest.st3_7.st3bit.x1 TRUE --&amp;gt; wTest is 256.&lt;/p&gt;
&lt;p&gt;Is there any way I could pack the bits and omit the byte-alignment?&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>