TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: DAVE KELLY
from: GEORGE WHITE
date: 1998-03-01 19:48:00
subject: bit fields

Hi Dave,
You asked:
DK>Given the code fragment below:
DK>(snip)____
DK>#include 
DK>/*On my compiler this produces an unsigned long interger*/
DK>#define SignalPacker(a, b, c, d)  ( (a) << 24 | (b) << 16 | (c) << 8 | (d) 
)
DK>main( argc, argv )
DK>int argc;
DK>char *argv[];
DK>{
DK>   unsigned long SignalToSend;
DK>   int a, b, c, d;
DK>   a = atoi(argv[1]);
DK>   b = atoi(argv[2]);
DK>   c = atoi(argv[3]);
DK>   d = atoi(argv[4]);
DK>   SignalToSend = SignalPacker( a, b, c, d );
DK>   printf("%lx\n", SignalToSend);
DK>   return 0;
DK>}
DK>(snip)____
DK>Is there a macro to uppack the signal once it is recieved by
DK>another program running in the background. Or  would I be better off
DK>to go to a function and store the information in a structure?
Not really appropriate to a macro in my opinion.
DK>struct UnpackedSignal {
DK>   int e, f, g, h;
DK>};
DK>UnPackSignal ( s )
DK>unsigned long int s;
DK>{
DK>   struct UnpackedSignal SigRecieved;
DK>   SigRecieved.e = ( (s) >> 24 );
DK>   SigRecieved.f = ( (s) >> 16 );
DK>   SigRecieved.g = ( (s) >>  8 );
DK>   SigRecieved.h = ( (s) );
DK>   return (&SigRecieved);
DK>}
DK>Or something to this effect.
Yes, but as you are only storeing 8 bits in each int, and for most of
us an int is either 16 or 32 bits long you need to make that:
  SigRecieved.e = ( (s) >> 24 ) & 0xff;
  SigRecieved.f = ( (s) >> 16 ) & 0xff;
  SigRecieved.g = ( (s) >>  8 ) & 0xff;
  SigRecieved.h = ( (s) ) & 0xff;
otherwise you may find SigRecieved.h contains a copy of the input data,
which may or may not be a problem depending on how the code uses it.
George
 * SLMR 2.1a * All Trademarks acknowledged (just in case ).
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.