comp.lang.ada
 help / color / mirror / Atom feed
* Representing data differently
@ 2003-02-07 14:15 Daniel Allex
  2003-02-07 18:07 ` tmoran
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Daniel Allex @ 2003-02-07 14:15 UTC (permalink / raw)


In C I can represent the same data multiple ways using structs and
unions.  How and can I do this in Ada?  See example below:

//
// Example of C's representation clause
//

#include <iostream.h>

#define mt37_length 4

enum BOOL {FALSE, TRUE};

struct MT_37_header
{
    short
		:4,
		nw:6,
		mt:6;
    short
		:16;		// place holder
    short
		:16;		// place holder
    short
 		:16;		// place holder
};

struct MT_37_fields 
  {
    short
		:1,		// not used
		:1,		// spare
		surv:1,
		kill:1,
		:6,		// filler
		:6;		// filler
    short 	
		ctsl;
    short
		:1,		// not used
		:4,		// spare
		gaff:1,
		smid:5,		
		weapon_type:3,
		sif:1,
		dest:1;
    short
		ctsl_msl; 
  };

struct bool_fields
{
  short
	b0:1,b1:1,b2:1,b3:1,b4:1,b5:1,b6:1,b7:1,b8:1,
  	b9:1,b10:1,b11:1,b12:1,b13:1,b14:1,b15:1;
};

struct MT_37_bools
{
  struct bool_fields wd[mt37_length];
};

struct MT_37_words
{
  short wd0;
  short wd1;
  short wd2;
  short wd3;
};

struct MT_37_message 
{
  short msg[mt37_length];
};

union MT_37
{
  struct MT_37_header header;
  struct MT_37_message message;
  struct MT_37_fields fields;
  struct MT_37_words  words;
  struct MT_37_bools  bools;
};

typedef union MT_37 MT_37;

void main()
{
  MT_37 mt37;

  mt37.header.nw = 3;
  mt37.header.mt = 037;

  mt37.fields.surv = TRUE;
  mt37.fields.kill = TRUE;
  mt37.fields.ctsl = 022;
  mt37.fields.gaff = TRUE;
  mt37.fields.smid = 01;
  mt37.fields.weapon_type = 03;
  mt37.fields.sif = TRUE;
  mt37.fields.dest = FALSE;
  mt37.fields.ctsl_msl = 0102;

  for(int i=0; i<mt37_length; i++)
    cout<<oct<<(unsigned short)mt37.message.msg[i]<<endl;

  mt37.bools.wd[0].b15 = TRUE;  // generic boolean field 
  mt37.bools.wd[0].b13 = FALSE;  // generic boolean field 
  mt37.bools.wd[0].b12 = FALSE;  // generic boolean field 
  

  cout<<oct<<(unsigned short)mt37.words.wd0<<endl;
  cout<<oct<<(unsigned short)mt37.words.wd1<<endl;
  cout<<oct<<(unsigned short)mt37.words.wd2<<endl;
  cout<<oct<<(unsigned short)mt37.words.wd3<<endl;
  
  mt37.words.wd0 = 0;
  mt37.words.wd1 = 0;
  mt37.words.wd2 = 0;
  mt37.words.wd3 = 0;
  mt37.bools.wd[0].b15 = TRUE;  // generic boolean field 
  mt37.bools.wd[0].b11 = TRUE;  // generic boolean field 
  mt37.bools.wd[0].b7 = TRUE;  // generic boolean field 
  mt37.bools.wd[0].b3 = TRUE;  // generic boolean field 
  cout<<hex<<mt37.words.wd0<<endl;

}



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2003-02-23 21:59 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-07 14:15 Representing data differently Daniel Allex
2003-02-07 18:07 ` tmoran
2003-02-09  4:39   ` Craig Carey
2003-02-10 12:47     ` Colin Paul Gloster
2003-02-13 17:21       ` Craig Carey
2003-02-08  0:24 ` Wojtek Narczynski
2003-02-12 18:52 ` Martin Krischik
2003-02-22 19:09   ` Robert A Duff
2003-02-23 13:06     ` Martin Krischik
2003-02-23 21:09 ` Craig Carey
2003-02-23 21:59   ` tmoran

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox