Beginning C question.

Gary Duzan gdtltr at freezer.it.udel.edu
Tue Jul 24 16:42:03 AEST 1990


In article <7703 at uudell.dell.com> jrh at mustang.dell.com (James Howard) writes:
=>In article <10997 at chaph.usc.edu>, wsze at nunki.usc.edu (Wally "The Whale"
=>Sze) writes:
=>> 
=>> A way I can see to solve this is to read two ints from disk and
=>> concantenate them. But how do you concantenate ints? Say I have
=>> a = 0x20df and b = 0x3244, and want to get a long int 0x20df3244.
=>> If anyone can help, or if you can think of a better way to do this,
=>> please let me know by mail. Thank you.
=>
=>This works on my system:
=>
=>main()
=>{
=>        short a=0x20df;
=>        short b=0x3244;
=>        int   c;
=>
=>c = (a<<16) + b;
=>printf("c = %x\n",c);
=>
=>exit(0);
=>}
=>
=>I used "short" because they're 16 bits on this machine, and int's are 32.
=>
   Doesn't this depend on the implementation of putw and endianness? Since
putw isn't standard (correct me if I'm wrong; it isn't in K&RII) it is 
difficult to say. I can easily see a case where c=a+(b<<16) might be right,
or where you might have to deal with each byte. I doubt if this could be
written portably. Of course, if portability isn't a concern, try Wally's
solution and see if it works.

                                        Gary Duzan
                                        Time  Lord
                                    Third Regeneration



--
                          gdtltr at freezer.it.udel.edu
   _o_                    --------------------------                      _o_
 [|o o|] If you can square, round, or cube a number, why not sphere it? [|o o|]
  |_O_|         "Don't listen to me; I never do." -- Doctor Who          |_O_|



More information about the Comp.lang.c mailing list