Topic: 3 bytes integer


Author: Hong Ji <hji@gte.net>
Date: 1997/08/29
Raw View
Hi,

Is it possible to make an integer which is 3 bytes long (instead of 4
bytes long integer or 2 byte short integer)? If yes, how to do it?

Thanks.


Hong Ji
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Beman Dawes <beman@NOSPAMesva.net>
Date: 1997/08/29
Raw View
Hong Ji wrote:

> Is it possible to make an integer which is 3 bytes long (instead of 4
> bytes long integer or 2 byte short integer)? If yes, how to do it?

Sure.  I have used such an animal for many years.  First in C, later in
C++. It is just a storage class intended for external storage.  Thus it
is endian independent.  For doing computations, always convert to an
int32 first.

Here is the code, more or less. It assumes "typedef unsigned char byte",
"typedef whatever int32".

class bin3 {

 byte contents[3];  // [0] is high order

public:

 bin3() {} // A default constructor is required since
 // other constructors are present.  C-like semantics
 // mean no default initialization since speed matters.

// no copy constructor declared; default bitwise copy constructor
applies.
// no assignment operator declared; default bitwise assignment applies.
// no destructor declared; none is needed.

// constructors, convertors, and assignment-from-int32

 bin3( int32 value ) { *this = value; }  // constructor/convertor from
int32
 // in turn calls operator=( int32 )

 const bin3 &  operator = ( int32 value ) {  // assignment from int32

#ifdef LITTLE_ENDIAN
  contents[0] = ((byte*)&value)[2];
  contents[1] = ((byte*)&value)[1];
  contents[2] = ((byte*)&value)[0];
#else
  contents[0] = ((byte*)&value)[1];
  contents[1] = ((byte*)&value)[2];
  contents[2] = ((byte*)&value)[3];
#endif
  return *this;
  } // operator =

// convertor to int32

 operator int32() const {  // convert to int32
  int32 temp;
# ifdef LITTLE_ENDIAN
  ((byte*)&temp)[0] = contents[2];
  ((byte*)&temp)[1] = contents[1];
  ((byte*)&temp)[2] = contents[0];
  ((byte*)&temp)[3] = ( contents[0] & 0x80 ) ? (byte)0xFF : (byte)0x00;
# else
  ((byte*)&temp)[0] = ( contents[0] & 0x80 ) ? (byte)0xFF : (byte)0x00;
  ((byte*)&temp)[1] = contents[0];
  ((byte*)&temp)[2] = contents[1];
  ((byte*)&temp)[3] = contents[2];
# endif
  return temp;
  }  // operator int32()
 }; // bin3
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]