473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Efficient shifting of a flat buffer

I would like to know the best efficient way to shift a flat buffer by
say 4bits.

for exaple if the flat buffer is

0x62 0x48 0x23 ....

then the result should be

0x06 0x24 0x82 0x3.....

byte by byte shifting somehow looked very inefficient.
Something like
int main()
{
unsigned char sample1[50];
unsigned char sample2[50];
int i;

for(i=0; i < 50; i++)
{
sample1[i] = rand(250);
}

for(i=0 ; i < 50; i++)
{
printf("%02x \n",sample1[i]);
}

printf(" output after shifting\n");
sample2[0] = sample1[0]>>4;
for(i=1; i <= 50; i++)
{
sample2[i] = (sample1[i-1] << 4) | (sample1[i]>>4);
printf("%02x \n",sample2[i]);
}

return 0;
}

Is there any other best way to do this.
Mar 3 '08 #1
3 5527
On Mon, 3 Mar 2008 01:16:31 -0800 (PST), Madhur <ma********@gmail.com>
wrote:
>I would like to know the best efficient way to shift a flat buffer by
say 4bits.

for exaple if the flat buffer is

0x62 0x48 0x23 ....

then the result should be

0x06 0x24 0x82 0x3.....

byte by byte shifting somehow looked very inefficient.
Something like
int main()
{
unsigned char sample1[50];
unsigned char sample2[50];
int i;

for(i=0; i < 50; i++)
{
sample1[i] = rand(250);
}

for(i=0 ; i < 50; i++)
{
printf("%02x \n",sample1[i]);
}

printf(" output after shifting\n");
sample2[0] = sample1[0]>>4;
for(i=1; i <= 50; i++)
{
sample2[i] = (sample1[i-1] << 4) | (sample1[i]>>4);
printf("%02x \n",sample2[i]);
}

return 0;
}

Is there any other best way to do this.
reducing the number or reads:

unsigned char last=0;
for (int i=0,i<sizeof(sample);++i) {
unsigned char next=sample1[i];
sample2[i]= (last<<4)|(next>>4);
last=next;
}

But you should profile to be sure it is better

Zara
Mar 3 '08 #2

"Madhur" <ma********@gmail.comwrote in message
news:d7**********************************@s8g2000p rg.googlegroups.com...
>I would like to know the best efficient way to shift a flat buffer by
say 4bits.

for exaple if the flat buffer is

0x62 0x48 0x23 ....

then the result should be

0x06 0x24 0x82 0x3.....

byte by byte shifting somehow looked very inefficient.
....
If the hex digits were strung all together, then the above does a right
shift. Your code however seems to do a left shift.

One solution would be to shift 32 or 64 bits at a time (depends on your
machine), but the way you've defined your shift means this results in very
peculiar shift pattern.

If you explained the reason for the shift further then perhaps we could help
better.

--
Bart
Mar 3 '08 #3
Madhur <ma********@gmail.comwrote:
# I would like to know the best efficient way to shift a flat buffer by
# say 4bits.
#
# for exaple if the flat buffer is
#
# 0x62 0x48 0x23 ....
#
# then the result should be
#
# 0x06 0x24 0x82 0x3.....
#
# byte by byte shifting somehow looked very inefficient.

Unless the hardware provides bit vectors, you're going to be
doing aload/shift/mask/or/store loop. A byte at a time is least
likely efficient, but most portable. Depending on alignment and
endianness you can sometimes do it a word at a time, but that
will only work on specific kinds of hardware.

Also some hardware provides a rotate instruction implemented
with a <<< or >>operator. The two shifts can be replaced
with one rotate if available.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Mar 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
31
by: metiu uitem | last post by:
Say you have a flat list: How do you efficiently get , , ] I was thinking of something along the lines of: for (key,number) in list: print key, number
7
by: Kay Schluehr | last post by:
I want to manipulate a deeply nested list in a generic way at a determined place. Given a sequence of constant objects a1, a2, ..., aN and a variable x. Now construct a list from them recursively:...
1
by: pedagani | last post by:
Dear comp.lang.c++, I'm interested in knowing the general techniques used to handle large binary files (>10GB) efficiently such as tweaking with filebuf , etc. Reading chunk by chunk seems to be...
5
by: deppeler | last post by:
Can someone look at this for me: I am trying to set up a script to edit an item in a flat file DB but I don't seem to be getting the data to the Photoedit script. It seems to be reading the 1st line...
6
by: Madhur | last post by:
I am having the following problem of bit shifting. My program runs on a little endian machine. Consider that if I have the following data represented in big endian... 0x12345678 the little...
1
by: Jon Harrop | last post by:
What would be the most efficient matrix representation for linear algebra on .NET? I recently benchmarked a 2D array, a flat array and an array of arrays and got the following performance...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.