converting byte to nibble and converted nibble to bytes

H

hari

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.

What exactly need to do is as folowws

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?

Thanks
Hari
 
G

Gene

hi all,

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 ).

Well if this is homework, then stop reading.

There are lots of ways to do this. If n holds the nibble in its low
order bits, then you can for example say

b = ((((((n & 8) << 1) | (n & 4)) << 1) | (n & 2)) << 1) | (n &
1);
b |= (b << 1);

It's simple to use this to build a table, which probably gives a
faster code. For example,

static unsigned char tbl[] = {
0x00,0x03,0x0c,0x0f,0x30,0x33,0x3c,0x3f,
0xc0,0xc3,0xcc,0xcf,0xf0,0xf3,0xfc,0xff
};

The bytes you want are then just

tbl[n & 0xf)

and

tbl[(n >> 4) & 0xf]
 
A

Amandil

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.

"Splitting into nibbles" usually means 0x3B >> 4 (to get the upper
nibble) and 0x3B & 0xF (to get the lower nibble). What you describe
below is something totally different.
What exactly need to do is as folowws

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?
(
A brute force method would be to use a series of ifs:
unsigned short s = 0x3b;
uningned short high = 0, low = 0;
if (s & 0x80) high |= 0xC0; /* 0x80 is 1000 0000; 0xC0 is 1100
0000 */
if (s & 0x40) high |= 0x30; /* 0x40 is 0100 0000; 0x30 is 0011
0000 */
if (s & 0x20) high |= 0xC; /* 0x20 is 0010 0000; 0x0C is 0000
1100 */
if (s & 0x10) high |= 0x3; /* 0x10 is 0001 0000; 0x03 is 0000
0011 */
if (s & 0x8) low |= 0xC0; /* 0x08 is 0000 0100; 0xC0 is 1100
0000 */
if (s & 0x4) low |= 0x30; /* 0x04 is 0000 0100; 0x30 is 0011
0000 */
if (s & 0x2) low |= 0xC; /* 0x02 is 0000 0010; 0x0C is 0000
1100 */
if (s & 0x1) low |= 0x3; /* 0x01 is 0000 0001; 0x03 is 0000
0011 */

This can be put into a loop, testing against 0x1 and setting 0x3, then
shifting:
unsigned test = 0x1, set = 0x3; /* s, high, low have been
previously init */
for (i = 0; i <= 4; i++) {
if (s & test)
low |= set;
test <<= 1;
set <<= 2;
}
for (; i <= 8; i++) {
if (s & test)
high |= set;
test <<= 1;
set <<= 2;
}

Out of morbid curiosity, why do you need this code? I'd like to know
if this is a real-world application or a programming exercise.

-- Marty Amandil
 
N

Nick Keighley

google problems. I can see two other people have posted
but I cannot see their posts. Apologies if I am repeating
stuff.


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

bottom_nibble = byte & 0xf;
top_nibble = (byte >> 4) & 0xf;

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.

What exactly need to do is as folowws

 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?

well with only 16 possible nibble values why not use a lookup
table?

unsigned char nib2byt[] = {0x00, 0x03, 0xC0, 0xC3...};

expanded_byte = nib2byt [nibble];
 
H

hari

google problems. I can see two other people have posted
but I cannot see their posts. Apologies if I am repeating
stuff.

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

bottom_nibble = byte & 0xf;
top_nibble    = (byte >> 4) & 0xf;




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.
What exactly need to do is as folowws
 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?

well with only 16 possible nibble values why not use a lookup
table?

unsigned char nib2byt[] = {0x00, 0x03, 0xC0, 0xC3...};

expanded_byte = nib2byt [nibble];

I m not able to view the replies.Can you please give your suggestion
again.
Nick,
I need to make 1010 as 11001100 that is each bit should be rePlicated
two times
 
A

Amandil

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 & 0x8) 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
 
B

Barry Schwarz

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.

What exactly need to do is as folowws

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 ).
If I understand correctly, you want to take a byte (limited to systems
where with 8 bits) whose bits are abcdefgh and expand it to two bytes
aabbccdd eeffgghh.

I suggest you use the bit-wise "and" operator to determine if a
particular bit is 1 or 0. If you initialize your output bytes
judiciously, you only have to process the case where the input bit is
1 and use the bit-wise "or" operator to set the appropriate two output
bits to 1.

The best way to get help here will be to show your code and ask
specific questions about what is not working.


Remove del for email
 
G

Gene

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.

What exactly need to do is as folowws

 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?

Sorry I sent this a few minutes after you posted, but it seems lost in
the Google bit bucket.

This will do what you want. There are many other possibilities:

b = ((((((n & 8) << 1) | (n & 4)) << 1) | (n & 2)) << 1) | (n &
1);
b |= (b << 1);

But as has been pointed out, tables are probably quicker. You can use
the code above to get the table:

unsigned char tbl[] = {
0x00,0x03,0x0c,0x0f,0x30,0x33,0x3c,0x3f,
0xc0,0xc3,0xcc,0xcf,0xf0,0xf3,0xfc,0xff
};

Now the two bytes you want are

tbl[n & 0xf]

and

tbl[(n >> 4) & 0xf]
 
H

hari

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.
What exactly need to do is as folowws
 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?

Sorry I sent this a few minutes after you posted, but it seems lost in
the Google bit bucket.

This will do what you want.  There are many other possibilities:

    b  = ((((((n & 8) << 1) | (n & 4)) << 1) | (n & 2)) << 1) | (n &
1);
    b |= (b << 1);

But as has been pointed out, tables are probably quicker.  You can use
the code above to get the table:

    unsigned char tbl[] = {
      0x00,0x03,0x0c,0x0f,0x30,0x33,0x3c,0x3f,
      0xc0,0xc3,0xcc,0xcf,0xf0,0xf3,0xfc,0xff
    };

Now the two bytes you want are

tbl[n & 0xf]

and

tbl[(n >> 4) & 0xf]- Hide quoted text -

- Show quoted text -

Hi all,
Reason to expand this is
i m having a PRINT FILE whose values are in hex.(say 3B).When it is
send for printing it will be expanded as binary form as 0011 1011,
where binary value 1 reprsents black data and 0 represents white
data.if the data needs to be expanded by two times, then 3B should
become 0000 1111 1100 1111.This value is expansion of 3B(0011 1100).
If I need to expand 3B by 4 times then 0000 0000 1111 1111 1111 0000
1111 1111.So I need to expand like this.

Now currently the printing is happening for only 3B value,it should be
expanded as 0F CF.

To summarize .

values is ABCDEFGH(8 bits) then if it is expanded by 2 times then it
should be AABB CCDD EEFF GGHH.
If it is expanded by 3 times then , AAAB BBCC CDDD EEEF FFGG GHHH.

Hope the explantioon is clear.Can I do this in easy wy..
 
R

Richard Heathfield

hari said:

To summarize .

values is ABCDEFGH(8 bits) then if it is expanded by 2 times then it
should be AABB CCDD EEFF GGHH.
If it is expanded by 3 times then , AAAB BBCC CDDD EEEF FFGG GHHH.

Hope the explantioon is clear.

Yes, it is.
Can I do this in easy wy..

Apparently not. But it can be done in an easy way, yes.

#include <limits.h>
#include <stdio.h>

#define BYTE(x) ((x) / CHAR_BIT)
#define BIT(x) ((x) % CHAR_BIT)
#define SET_BIT(a, b) \
(a)[BYTE(b)] |= (1 << BIT(b))
#define CLEAR_BIT(a, b) \
(a)[BYTE(b)] &= ~(1 << BIT(b))
#define TEST_BIT(a, b) \
(!!(((a)[BYTE(b)]) & (1 << BIT(b))))

void doit(char x, int repeats)
{
unsigned char *p = (unsigned char *)&x;
size_t i = 0;
int r = 0;
while(i < sizeof x)
{
size_t b = 0;
while(b < CHAR_BIT)
{
for(r = 0; r < repeats; r++)
{
putchar('0' + TEST_BIT(p, b));
}
++b;
}
++i;
}
}

int main(void)
{
doit(6, 2); putchar('\n');
doit(42, 3); putchar('\n');
doit('Z', 4); putchar('\n');
return 0;
}
 
C

CBFalconer

hari said:
.... snip ...

Reason to expand this is i m having a PRINT FILE whose values
are in hex.(say 3B).When it is send for printing it will be
expanded as binary form as 0011 1011, where binary value 1
reprsents black data and 0 represents white data.if the data
needs to be expanded by two times, then 3B should become 0000
1111 1100 1111.This value is expansion of 3B(0011 1100). If I
need to expand 3B by 4 times then 0000 0000 1111 1111 1111 0000
1111 1111.So I need to expand like this.

Now currently the printing is happening for only 3B value,it
should be expanded as 0F CF.

Doesn't work. You also need to expand in the vertical direction.
 
H

hari

hari wrote:

... snip ...



Doesn't work.  You also need to expand in the vertical direction.

yes, I accept .it will increase only the width,to increase in height
ths same binary values should be repeated.But initially I need to do
the byte expansion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top