0

I am trying to part a int16_t variable inside a structure. This structure is persisted in disk and loaded back across reboot. the old structure is

struct details
{  
  int a;
  int16_t var1;
  int16_t b;
} details_t

The new one i changed is

struct details
{
  int a;
  int16_t var1:15;
  unit16_t var2:1;
  int16_t b;
}details_t;

The new changes are working fine across reboot.But is this correct way of doing?. what i want to achieve is to have a dual meaning of variable var1 based on var2 is set or cleared. since var2 stores binary values, I declared it as uint16_t. Is it legal to separate a variable and declare it as two different datatype (int16_t and uint16_t ). This is going to be the update in existing stack, which should work seamlessly after update. Also i couldn't use kernel functions like set_bit and clear_bit on these parted variables var1 and var2. My machine is little endian

user694733
  • 15,208
  • 2
  • 42
  • 68

2 Answers2

0

You want to keep the backwards compatibility. But unfortunately C standard gives implementations a lot of room on how they should handle the bit fields. var1 and var2 could end up as 2 16-bit variables, which you likely what you don't want.

I recommend using manual bit masking and shifting instead. Just be careful about the signed variable when using bit shifting. Use unsigned temporary variable if necessary.

user694733
  • 15,208
  • 2
  • 42
  • 68
0

But is this correct way of doing?

Bit-fields is almost certainly the incorrect way of doing anything, particularly if portability is desired. Please read this.

What happens if you declare int16_t var1:15; is not specified by the standard, there is no telling where the sign bit will end up, where the msb will go, or anything else. It is not even specified whether this bit field will be treated as signed or unsigned.

Is it legal to separate a variable and declare it as two different datatype

It will compile but the results will depend on system and compiler. Basically you just created a random goo of 16 bits, of which you cannot predict any given behavior, unless one specific compiler document says otherwise.

This is going to be the update in existing stack, which should work seamlessly after update.

Then don't do this. Chances are you'll break the program or at least introduce vulnerablities for future maintenance. Instead, forget you ever saw something called bit-fields and use bit-wise operators to manipulate the original int16_t.

How it makes sense to smuggle in a boolean flag in the middle of this data type, I have no idea, but that's up to your program to handle.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I can handle it with using bit wise operators on var1 , since var1 would need a maximum of 10 bits, the remaining bits I can use it to handle things. Thanks for the details. it helps me a lot to avoid screwing up later. – Vignesh K Viki Nov 10 '14 at 09:45
  • @VigneshKViki The strange thing here is that you have declared this variable as signed. If it is indeed a signed variable in two's complement, then you _are_ using all 16 bits. If it is not a signed variable, then why are you used a signed type? Picking the right type for the task is _very_ important in C. – Lundin Nov 10 '14 at 09:55
  • yes . i will set var1 into -1 to denote it as invalid.So i need it to be signed type. – Vignesh K Viki Nov 10 '14 at 10:01
  • @VigneshKViki So... then you _are_ using 16 bits and nothing of what you are trying to do here makes any sense. http://en.wikipedia.org/wiki/Two%27s_complement – Lundin Nov 10 '14 at 12:16
  • I don't understand what you are coming to say @Lundin. if var1 is +ve then i will check for 15th bit of var1 , depending on that bit is set or not i can mask the last 10 bits of var1 and use in different context.if the signed bit is set, i am not going to proceed at all. kindly explain what is wrong in this?. any example case would be appreciated. – Vignesh K Viki Nov 10 '14 at 12:58
  • @VigneshKViki I'm not following... what seems to be the problem then? Why can't you simply do `if(val < 0) { do_this(); } else { do_that(); }`. Why would you need to change the data structure? – Lundin Nov 10 '14 at 13:05