-2

I have to write a function setbits(x,p,n,y) who returns x with the n bits that begin at position p set to the right most n bits of an unsigned char variable y (leaving other bits unchanged).

E.g. if x = 10101010 (170 decimal) and y = 10100111 (167 decimal) and n = 3 and p = 6 say then you need to strip off 3 bits of y (111) and put them in x at position 10xxx010 to get answer 10111010.

This function should print out the result in binary form.

The output should be like this :

x = 10101010 (binary)
y = 10100111 (binary)
setbits n = 3, p = 6 gives x = 10111010 (binary)

Hi I came across this problem in bit field.
How should I go about it?

Typedef struct {
    unsigned char x:8;
    unsigned char y:8;
} var;
palmplam
  • 707
  • 9
  • 20
garima
  • 5,154
  • 11
  • 46
  • 77
  • 1
    This isn't rent-a-coder, and tagging it homework doesn't mean we'll do it for you. Try first and come back with a more specific question. – Ed S. Feb 28 '11 at 09:36

3 Answers3

0

Bitfields are a specific C invention that have nothing to do with this. For instance, you could have

struct RGB16 {
   unsigned int R : 5;
   unsigned int G : 6;
   unsigned int B : 5;
};

which is a 16 bits structure, with 3 components that are each smaller than a byte. However, the C standard say nothing about their relative position. You can't assume that they're laid out in memory as RRRRRGGGGGGBBBBB. Furthermore, the bitfieldwidths are constant, and in your case the bits needed are variable. So, googling for "C bitfields" won't help you with this problem.

As for your problem, you should know that x & binary(11000011) zeroes out the middle 4 bits, and y | binary(00111100) sets the middle 4 bits to one.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • This question is not about setting 4 bits in a bit stream right? – sunmoon Feb 28 '11 at 09:42
  • Not precisely, no. But garima should be able to generalize this to his problem (setting `n` bits at position `p`, instead of 4 bits at position 6). – MSalters Feb 28 '11 at 09:48
0

char is not an allowed type for bit fields, they must be of type int. If you use char, you are invoking implementation-defined behaviour not covered by the C standard (see ISO 9899:1999 6.7.2.1 §4).

Thus your question is impossible to answer without knowing what system and compiler you are using.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • hey thnx a lot..even I had a doubt on this!!should I implement this without the bitfield then – garima Feb 28 '11 at 10:12
  • @garima It is *always* best to avoid bit fields, they are barely covered by the C standard at all. Any code involving bit fields will be completely unportable and unpredictable. It is better to allocate a chunk of bytes and then use the bit-wise operators to access individual bits. Bit-wise operators should yield exactly the same machine code if the compiler is somewhat decent. – Lundin Feb 28 '11 at 10:16
  • thnk Lundin!! this is helpful – garima Mar 01 '11 at 09:01
-1

t = y<<(8-n) k = t>>p; print x&k;

Assuming each bit stream is 8 bits.

sunmoon
  • 1,448
  • 1
  • 15
  • 27