Bit expansion by 2times,3times....10 times

H

hari

Hi all,

suppose a hex value is there(say 3B), this needs to be expanded.

Suppose if this 3B(0011 1011) needs to be expanded by 3 times, then
each bit should be expanded by 3 times(000000111111 111000111111). if
it needs to expanded by 4 times then(0000000011111111
111100001111111).hpow can I do this.I m tring to do this,but :(. I
need to do this expansion 10 times)

any suggestion will be great help.same query I have posted as
converting nibble to bytes and bytes to nibble,But I feel on expanding
by 3 times I m getting in to problems.

Regards
Hari
 
B

Barry Schwarz

Hi all,

suppose a hex value is there(say 3B), this needs to be expanded.

Suppose if this 3B(0011 1011) needs to be expanded by 3 times, then
each bit should be expanded by 3 times(000000111111 111000111111). if
it needs to expanded by 4 times then(0000000011111111
111100001111111).hpow can I do this.I m tring to do this,but :(. I
need to do this expansion 10 times)

any suggestion will be great help.same query I have posted as
converting nibble to bytes and bytes to nibble,But I feel on expanding
by 3 times I m getting in to problems.

The answers you received to your similar request four days ago are
equally applicable. If you didn't save them, you can probably find
them on google. Show us what you have done so far.


Remove del for email
 
B

Bartc

hari said:
Hi all,

suppose a hex value is there(say 3B), this needs to be expanded.

Suppose if this 3B(0011 1011) needs to be expanded by 3 times, then
each bit should be expanded by 3 times(000000111111 111000111111). if
it needs to expanded by 4 times then(0000000011111111
111100001111111).hpow can I do this.I m tring to do this,but :(. I
need to do this expansion 10 times)

any suggestion will be great help.same query I have posted as
converting nibble to bytes and bytes to nibble,But I feel on expanding
by 3 times I m getting in to problems.

What's the problem with doing it 10 times?

The following code duplicates bits in an integer value.

If this sort of code is no good then you must be more specific in your
requirements.

You mentioned something about printing in a previous post, sounded like
bit-image scaling. That doesn't sound difficult: to scale by 3 times,
duplicate each byte by 3 (as below), send the low 24-bits of the duplicated
value. And repeat to end of row. Then do the same thing for the next two
rows.

Bart


#include <stdio.h>

int duplbits(int,int,int);

int main(void)
{int x,y;

x=0x31;

y=duplbits(x,8,4);

//printf("x = %B\n",x);
//printf("y = %B\n",y);

printf("x = %X\n",x);
printf("y = %X\n",y);


}

/*duplicate least-significant <bits> of a, n times, return result */

int duplbits(int a,int bits,int n)
{
int b,amask,bmask;
int i;
int count;

if (bits<=0 || n<=0) return 0;

amask=1;
bmask=1;

b=0;

for (i=1; i<=bits; ++i)
{
count=n;
do
{ if (a & amask)
b |= (~0 & bmask);
bmask <<= 1;
}
while (--count);

amask <<= 1;
};

return b;

}
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top