-1

Possible Duplicate:
How do you compare structs for equality in C?

In a macro I need the bitwise not of a variable or struct which is a parameter of this macro.

 #define THE_C_MACRO(var) (someFunction(~var == var##_inv))

That works fine with variables but with structs this doesn't work. Does somebody know a easy way to get the bitwise not of a struct in a macro?

Edit:

Sorry I was too fast with asking this question. I have to make sure that some variables are saved twice in the RAM - normal and inverse (because of some regulations). We built some macros which help us with normal variables.

I wanted to extend that also to structs. But there is also no easy way to compare two structs => that will not work so easily.

Community
  • 1
  • 1
Fabian
  • 1,224
  • 1
  • 11
  • 26
  • 2
    What do you mean with bitwise not of a Struct ? a bitwise not of each byte of the macro or a bitwise not of a field in the macro ? – BigMike Oct 20 '11 at 07:10
  • What is the purpose of this? Whatever you're trying to do, it's likely there's a better way to do it. – Keith Thompson Oct 20 '11 at 07:12
  • To those voting to close the question because it is an "exact duplicate", could you link to that question? – Smashery Oct 20 '11 at 07:21

1 Answers1

2

There is no direct way to get the 'bitwise not' of an entire structure in one operation. You would probably need to do a member-wise evaluation of the structure, unless you wrote a function analogous to memset():

void memnot(void *data, size_t length)
{
    unsigned char *src = (unsigned char *)data;
    while (length-- > 0)
        *src = ~*src;
}

Your macro would then need to be one of:

#define BITWISE_INVERT_STRUCT(x)     memnot(&(x), sizeof(x))
#define BITWISE_INVERT_STRUCTPTR(x)  memnot(x, sizeof(*(x)))

If this proves to be a bottleneck, you can consider optimizing. For example, if length is a multiple of 8 bytes, then use a loop on uint64_t; if it is a multiple of 4 bytes, then use a loop on uint32_t; if it is a multiple of 2 bytes, then use a loop on uint16_t; else use what is shown here. This makes the possibly radical assumption that if a structure is a multiple of 8 bytes long, then valid structure addresses will also be on a multiple of 8 bytes. This is not necessarily the case; a structure such as struct irksome { uint16_t a, b, c, d; }; only requires a 2-byte alignment even though it is 8 bytes long. You can do other, more careful/subtle analyses of the address and size and decide whether to do things byte-wise or in larger multiples.

The name memnot() is reserved to the implementation (C99 §7.26.11 - Future directions for <string.h>).

Function names that begin with str, mem, or wcs and a lowercase letter may be added to the declarations in the header.

You might prefer to use mem_not() or memNot() or something else as the function name.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • GCC has tricks such as the [`aligned` attribute](http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html) that might help get around alignment issues – detly Oct 20 '11 at 07:48
  • I don't have GCC. But i'm on a 16Bit machine and the compiler isn't really able to handle Byte variables or structs which don't have an even size. – Fabian Oct 20 '11 at 08:02