On Mar 18, 9:08 pm, hari <(E-Mail Removed)> wrote:

> hi all,

>

> Im writing a code in which the bytes,needed to be splitted in to

> nibbles.each nibble needs to be made as byte. for example:

> consider 3B as byte, on splitting this I will get 3 and B.The binary

> value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
Most people would understand "separating into nibbles" as asking for

0x3B >> 4

to get the upper nibble and

0x3B & 0xF

to isolate the lower nibble. What you are asking for is something

totally different.

> What exactly need to do is as follows

>

> 3B----> byte

> 0011 1011----->binary value for 3B

>

> I need to expand(make it as bytes) 3 and B as follows )

>

> 3 ->0011 as 00001111

> B->1011 as 11001111.

>

> logic is all the bit position values should be filled with adjacent

> bits(that is for eg 1010 is the value ,it should be made as

> 11001100 ).

>

> How can I do this?

>
I tried to post this yesterday, I don't know why it didn't work.

One method is to use a series of ifs:

unsigned short u = 0x3B;

unsigned short high = 0, low = 0;

if (u & 0x80) high |= 0xC0; /* 0x80 = 1000 0000B, 0xC0 = 1100

0000B */

if (u & 0x40) high |= 0x30; /* 0x40 = 0100 0000B, 0x30 = 0011

0000B */

if (u & 0x20) high |= 0xC; /* 0x20 = 0010 0000B, 0x0C = 0000

1100B */

if (u & 0x10) high |= 0x3; /* 0x10 = 0001 0000B, 0x03 = 0000

0011B */

if (u & 0x

low |= 0xC0; /* 0x08 = 0000 1000B, 0xC0 = 1100

0000B */

if (u & 0x4) low |= 0x30; /* 0x04 = 0000 0100B, 0x30 = 0011

0000B */

if (u & 0x2) low |= 0xC; /* 0x02 = 0000 0010B, 0x0C = 0000

1100B */

if (u & 0x1) low |= 0x3; /* 0x01 = 0000 0001B, 0x03 = 0000

0011B */

Alternatively, you could use a loop:

unsigned short u = 0x3B;

unsigned short high = 0, low = 0;

unsigned short test = 0x1, set = 0x3;

int i;

for (i = 0; i < 4; i++) {

if (u & test)

low |= set;

test <<= 1; /* Shift left and assign */

set <<= 2;

}

set = 0x3;

for ( ; i < 8; i++) {

if (u & test)

high |= set;

test <<= 1;

set <= 2;

}

Out of morbid curiosity, why do you want this? Some kind of

programming exercise? Or is it actually a real-world application?

-- Marty Amandil