UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to declare a 6-byte Integer type?

#1 2008-12-22 04:42 PM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: How to declare a 6-byte Integer type?

Hello, i am using a 32-bit system and a 32-bit OS. I have to implement the IEEE 1588 V2 PTP std. Here, one of the datatypes mentioned is Unsigned Integer of 48 bits i.e. 6 bytes. i Know that Integer is of 4-bytes and long long int is of 8-bytes in a 32-bit machine. I can't afford to create a long long int and waste other 16 bits.
Now then, how do i declare(create) a 6-byte type Unsigned Integer? Please help me. Thanks in advance.

Offline

#2 2008-12-23 05:11 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How to declare a 6-byte Integer type?

Those 16 bits will be wasted one way or the other because of alignment.
Only if you expect to work with huge arrays of them it's worth trying to
save them. It's a speed versus space assessment.

If you don't use a long long then you end up using a structure or a
character array:

struct uint48 {
	unsigned long long v:48;
} __attribute__((packed));

or

struct uint48 {
	unsigned int i;
	unsigned short s;
} __attribute__((packed));

or

char uint48[6];

If you don't want padding then you need to tell the compiler that.

What is most convenient depends on your implementation, but I'd go for
the bitfield version and cast to long long when needed. The following code
seems to work as expected, even though it gives a bogus warning with my
gcc version:

#include <stdio.h>

struct uint48 {
	unsigned long long v:48;
} __attribute__((packed));

int main(void)
{
	struct uint48 x[100];

	x[0].v = 7;
	x[1].v = 1099511627776;
	x[2].v = x[0].v * x[1].v;
	printf("sizeof(x) = %u (600)\n", sizeof(x));
	printf("x[0] = %llu (7)\n", (unsigned long long)x[0].v);
	printf("x[1] = %llu (1099511627776)\n", (unsigned long long)x[1].v);
	printf("x[2] = %llu (7696581394432)\n", (unsigned long long)x[2].v);
	return 0;
}

Offline

#3 2008-12-23 08:54 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: How to declare a 6-byte Integer type?

Hello,
thanks a lot for the code sample. :)

Offline

#4 2008-12-24 06:02 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How to declare a 6-byte Integer type?

Don't forget to add a thorough test suite for your code, as it may not
always work as expected with all compiler versions and perhaps even
uncover compiler bugs in weird corner cases.

The test code have written isn't sufficient because the compiler optimises
it and calculates the values at compile time, which is different than at
runtime. You want to test all cases and operations.

Good luck!

Offline

#5 2008-12-24 08:39 AM

innqubus
Member
Registered: 2007-11-22
Posts: 29

Re: How to declare a 6-byte Integer type?

Thanks, i'll see to it once i add this code.

Offline

  • Index
  • » C
  • » How to declare a 6-byte Integer type?

Board footer

Powered by FluxBB