User Tools

Site Tools


simulating_20a_20union

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

simulating_20a_20union [2018/03/31 13:19]
127.0.0.1 external edit
simulating_20a_20union [2018/04/17 18:48] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, March 2007//\\ \\  BBC BASIC doesn'​t have **unions**, but it is possible to simulate them using structures. The trick is to create two (or more) structures, which should be the same size, then manipulate the internal pointers such that both structures refer to the //same// data.\\ \\  Here is a simple example of creating a **union** between a 32-bit integer value and four individual 8-bit values:\\ \\  //by Richard Russell, March 2007//\\ \\  BBC BASIC doesn'​t have **unions**, but it is possible to simulate them using structures. The trick is to create two (or more) structures, which should be the same size, then manipulate the internal pointers such that both structures refer to the //same// data.\\ \\  Here is a simple example of creating a **union** between a 32-bit integer value and four individual 8-bit values:\\ \\ 
 +<code bb4w>
         DIM s1{v%}         DIM s1{v%}
         DIM s2{b0&,​b1&,​b2&,​b3&​}         DIM s2{b0&,​b1&,​b2&,​b3&​}
Line 9: Line 10:
  
         PRINT ~s2.b0& s2.b1& s2.b2& s2.b3&         PRINT ~s2.b0& s2.b1& s2.b2& s2.b3&
 +</​code>​
 Here the structure **s1{}** contains the 32-bit integer value **s1.v%** and the structure **s2{}** contains the four 8-bit values **s2.b0&​**,​ **s2.b1&​**,​ **s2.b2&​** and **s2.b3&​**. The third line is a bit of '​magic'​ which sets **s2{}** to point to the same data as **s1{}**.\\ \\  Hence you can write and read the data using either of the two formats. In the example above the data is written as a single 32-bit value and read as four 8-bit values.\\ \\  If you prefer to hide the non-obvious '​magic'​ code away from public gaze, you can put it in a procedure (which could even be contained in a library) as follows:\\ \\  Here the structure **s1{}** contains the 32-bit integer value **s1.v%** and the structure **s2{}** contains the four 8-bit values **s2.b0&​**,​ **s2.b1&​**,​ **s2.b2&​** and **s2.b3&​**. The third line is a bit of '​magic'​ which sets **s2{}** to point to the same data as **s1{}**.\\ \\  Hence you can write and read the data using either of the two formats. In the example above the data is written as a single 32-bit value and read as four 8-bit values.\\ \\  If you prefer to hide the non-obvious '​magic'​ code away from public gaze, you can put it in a procedure (which could even be contained in a library) as follows:\\ \\ 
 +<code bb4w>
         DIM s1{v%}         DIM s1{v%}
         DIM s2{b0&,​b1&,​b2&,​b3&​}         DIM s2{b0&,​b1&,​b2&,​b3&​}
Line 22: Line 25:
         !(^b{}+4) = a{}         !(^b{}+4) = a{}
         ENDPROC         ENDPROC
 +</​code>​
 Note that, unlike languages which have true unions, this technique will not save memory. The memory originally allocated to the structure **s2{}** is wasted, but that doesn'​t matter if it is a LOCAL structure since the memory will be released on exit from the procedure or function in which it was declared. Note that, unlike languages which have true unions, this technique will not save memory. The memory originally allocated to the structure **s2{}** is wasted, but that doesn'​t matter if it is a LOCAL structure since the memory will be released on exit from the procedure or function in which it was declared.
simulating_20a_20union.txt ยท Last modified: 2018/04/17 18:48 by tbest3112