Search This Blog

Wednesday, June 04, 2008

Bitfield inside a Union

When coding Ace physical memory manager, I came to a situation where I have to use lot of bit operations on a integer variable. The code looked little ugly because of the explicit bit operations(left shift<<, and & …), so I changed the code to use C bitfields.

typedef struct struct_x
{
union
{
int all;
int x:1,y:1,z:30,
};
};

The above code wasn’t working; debugging further I found that the values for bitfields are not calculated correctly.

After some digging I found that, the bitfield hint is ignored by the compiler since it is directly under union. So the compiler treated the fields x,y,z as separate integer instead of considering as one.

I modified the code to pack the bitfields inside a structure and it worked correctly.

typedef struct struct_x
{
union
{
int all;
struct
{
int x:1, y:1, z:30,
}_;
};
};

And yes, just an underscore(_) is a valid identifier.

Configuring Cygwin to use proxy server

Today I was trying to setup ACPI-CA source tree in my computer using the following command

git clone http://www.acpica.org/repos/acpica.git

but it gave me error when connecting to the site. I knew it was problem with my cygwin network settings. Since I have a proxy server in between, connections internet was failing.

Googleing internet for how to configure cygwin to use proxy didn’t yield any result. Two of my friends with Linux knowledge advised me to set an environment variable (http_proxy) to connect. After exporting it, everything worked fine.

export http_proxy=http://username:password@host:port/