473,385 Members | 1,355 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 code to do left shit on 64-bit values

I use BC 5.02 (long is 32 bit) and I wonder if
there's any efficient code that would allow me
to left shift 64 bit values that I currently use
this way:

typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;

This struct can be changed if needed.

The least significant part can contain any value
in the 0..2^32 -1 range. The most significant
part; however, will usually be 0, but it may
cintain some smaller values, usually under 10,
though higher values (up to 2^16 -1) are possible
in theory.

The amount of shift will not be lnown in advance --
I will collect the above tuples into a 1 meg array,
figure out the minimum and maximum values, and
do the shifting based on that. Bits cannot fall into
the bitbucket.

TIA
May 27 '06 #1
19 2506
aurgathor wrote:

I use BC 5.02 (long is 32 bit) and I wonder if
there's any efficient code that would allow me
to left shift 64 bit values that I currently use
this way:

typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;


Create a mask for the high order bit. This is a separate problem,
to make it clean and portable.

Test the high order bit of the high portion. If set, you have an
overflow, and it is time to barf. If not, shift that portion left.

Test the high order bit of the low portion. If set, increment the
high order portion and reset the bit. Shift that portion left.
done.

This is probably better relegated to a system dependant assembly
language routine, if you can arrange the linkage, and if it is used
enough to be a bind.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

May 27 '06 #2
aurgathor wrote:

I use BC 5.02 (long is 32 bit) and I wonder if
there's any efficient code that would allow me
to left shift 64 bit values that I currently use
this way:

typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;

This struct can be changed if needed.

The least significant part can contain any value
in the 0..2^32 -1 range. The most significant
part; however, will usually be 0, but it may
cintain some smaller values, usually under 10,
though higher values (up to 2^16 -1) are possible
in theory.

The amount of shift will not be lnown in advance --
I will collect the above tuples into a 1 meg array,
figure out the minimum and maximum values, and
do the shifting based on that. Bits cannot fall into
the bitbucket.


msLong is 0x87654321
lsLong is 0x12345678

Left shifted 8:
msLong is 0x65432112
lsLong is 0x34567800

Left shifted 36 more:
msLong is 0x45678000
lsLong is 0x0

/* BEGIN new.c */

#include <stdio.h>

typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;

b64_struct leftb64(b64_struct value, int shift)
{
if (shift > 31) {
shift -= 31;
value = leftb64(value, 31);
value = leftb64(value, shift);
} else {
if (shift > 0) {
value.msLong <<= shift;
value.msLong |= value.lsLong >> 32 - shift;
value.lsLong <<= shift;
}
}
return value;
}

int main(void)
{
b64_struct value = {0x12345678, 0x87654321};

printf("msLong is 0x%lx\n", value.msLong);
printf("lsLong is 0x%lx\n", value.lsLong);
value = leftb64(value, 8);
puts("\nLeft shifted 8:");
printf("msLong is 0x%lx\n", value.msLong);
printf("lsLong is 0x%lx\n", value.lsLong);
puts("\nLeft shifted 36 more:");
value = leftb64(value, 36);
printf("msLong is 0x%lx\n", value.msLong);
printf("lsLong is 0x%lx\n", value.lsLong);
return 0;
}

/* END new.c */
--
pete
May 27 '06 #3
aurgathor wrote:
I use BC 5.02 (long is 32 bit) and I wonder if
there's any efficient code that would allow me
to left shift 64 bit values that I currently use
this way:

typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;


Step 0: See whether you can use `unsigned long long' (a
type introduced by C99, but supported as an extension even by
some C90 compilers when operated in lenient modes).

Step 1: Follow the KISS[*] principle, and use

b64_struct x = { ... };
int bits = ...;
x.msLong = (x.msLong << bits) | (x.lsLong >> (32 - bits));
x.lsLong <<= bits;
[*] "Keep It Simple, Stupid!"

Step 2: Try something else. Step 2 is only to be taken if
Steps 0 and 1 don't pan out. Some possible reasons they might not:

.... until and unless this proves to be inadequate. A few
potential reasons for inadequacy:

- Too slow (but you won't know this until you actually
measure it, in the context of the entire program)

- `long long' available only on some but not all of the
systems of interest

- On some systems `long' is wider than 32 bits (easy to
re-spell 32, but you need to decide what to do about
bits 64 and upwards)

- Can only shift by 0 <= bits < 32 (could wrap the above
in a loop if that's a problem)

Step 3: Re-read the subject line. Was it prompted by, er,
any extreme difficulty in getting the code out?

--
Eric Sosman
es*****@acm-dot-org.invalid
May 27 '06 #4
We'd really appreciate it if you'd keep that kind of language out
of the headers...
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
May 27 '06 #5

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
We'd really appreciate it if you'd keep that kind of language out
of the headers...


Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to
judgement here.
Rod Pemberton
May 27 '06 #6

Rod Pemberton wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
We'd really appreciate it if you'd keep that kind of language out
of the headers...


Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to
judgement here.


Just a quick hint here - try looking up the word "humor".

May 27 '06 #7

<en******@yahoo.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...

Rod Pemberton wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
We'd really appreciate it if you'd keep that kind of language out
of the headers...


Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to judgement here.


Just a quick hint here - try looking up the word "humor".


There was nothing humorous in what he said and there was nothing, such as a
smiley, to indicate that it was humor. I also assumed that since he didn't
post a response to the OP's question, he was offended. That seems to be the
usual case with Pfaff. I also assumed that since he was posting from a
university email, that his school is filtering, flaging, and reviewing his
email for foul language. Do you think my assumptions are invalid?
Rod Pemberton
May 27 '06 #8
Rod Pemberton wrote:
<en******@yahoo.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Rod Pemberton wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
We'd really appreciate it if you'd keep that kind of language out
of the headers...
Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to judgement here.

Just a quick hint here - try looking up the word "humor".


There was nothing humorous in what he said and there was nothing, such as a
smiley, to indicate that it was humor. I also assumed that since he didn't
post a response to the OP's question, he was offended. That seems to be the
usual case with Pfaff. I also assumed that since he was posting from a
university email, that his school is filtering, flaging, and reviewing his
email for foul language. Do you think my assumptions are invalid?
Rod Pemberton

Rod,

Ben Pfaff is well known here and is a friend. He is a graduate student
in a CS PhD program at Stanford University at Palo Alto. I bet he knows
how to spell 'flagging'.

We've known Ben for years. You have come to us only recently. I
personally don't care what assumptions you draw about Ben but you might
consider keeping the negative ones to yourself.

I find your posts to other groups, c.o.m.djgpp for example, helpful and
friendly. Here in comp.lang.c you are a PITA. What gives?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 27 '06 #9

"Joe Wright" <jo********@comcast.net> wrote in message
news:MO********************@comcast.com...
Rod Pemberton wrote:
<en******@yahoo.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Rod Pemberton wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
> We'd really appreciate it if you'd keep that kind of language out
> of the headers...
Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to
judgement here.
Just a quick hint here - try looking up the word "humor".


There was nothing humorous in what he said and there was nothing, such as a smiley, to indicate that it was humor. I also assumed that since he didn't post a response to the OP's question, he was offended. That seems to be the usual case with Pfaff. I also assumed that since he was posting from a
university email, that his school is filtering, flaging, and reviewing his email for foul language. Do you think my assumptions are invalid?

Ben Pfaff is well known here and is a friend. He is a graduate student
in a CS PhD program at Stanford University at Palo Alto. I bet he knows
how to spell 'flagging'.


I don't use a spell checker. You're likely to find occasional misspellings,
rare or alternate spellings, and maybe even a antonym or two in my posts.
We've known Ben for years. You have come to us only recently.
Recently? No. Whether my posts made it to Google or only a few select
servers is a different issue...
I personally don't care what assumptions you draw about Ben but you might
consider keeping the negative ones to yourself.
Did I make a negative comment about Pfaff? I don't see one. Nor, do I see
an untrue one. I see two possible non-negative assumptions. It appears to
me he rushed to judgement. There has been no further clarification from
him.
I find your posts to other groups, c.o.m.djgpp for example, helpful and
friendly. Here in comp.lang.c you are a PITA. What gives?


There are a large number of assholes, frauds, and charlatans here. I'm
dealing with them appropriately. However, in this thread, as far as I can
tell, I've done none of that.
Rod Pemberton
May 28 '06 #10
Joe Wright wrote:
Rod Pemberton

Rod,

Ben Pfaff is well known here and is a friend.

Joe, Rod Pemberton is well known here and is a troll.

Brian
May 28 '06 #11
Sorry, 't' got shifted and overwrote 'f'....

And thanks for all those who replied to my question.

aur

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
We'd really appreciate it if you'd keep that kind of language out
of the headers...
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org

May 28 '06 #12
Joe Wright wrote:
.... snip ...
Ben Pfaff is well known here and is a friend. He is a graduate student
in a CS PhD program at Stanford University at Palo Alto.


I was under the impression that Ben Pfaff was a faculty of Stanford.
Oh well...

May 28 '06 #13
On 2006-05-28, Default User <de***********@yahoo.com> wrote:
Joe Wright wrote:
> Rod Pemberton
>
>

Rod,

Ben Pfaff is well known here and is a friend.

Joe, Rod Pemberton is well known here and is a troll.

That is completely untrue. I'll let Rod post a decent defense,
but as far as I'm concerned, Default User is a troll.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 28 '06 #14
Andrew Poelstra wrote:
On 2006-05-28, Default User <de***********@yahoo.com> wrote:
Joe, Rod Pemberton is well known here and is a troll.

That is completely untrue. I'll let Rod post a decent defense,


It probably won't matter if he does, I've had him killfiled for months
now. As I'd do with any troll.
but as far as I'm concerned, Default User is a troll.


You're entitled to your opinion.

Brian

May 28 '06 #15
"santosh" <sa*********@gmail.com> writes:
Joe Wright wrote:
... snip ...
Ben Pfaff is well known here and is a friend. He is a graduate student
in a CS PhD program at Stanford University at Palo Alto.


I was under the impression that Ben Pfaff was a faculty of Stanford.


Definitely not.
--
Here's a tip: null pointers don't have to be *dull* pointers!
May 28 '06 #16
On 2006-05-28, Default User <de***********@yahoo.com> wrote:
Andrew Poelstra wrote:
but as far as I'm concerned, Default User is a troll.


You're entitled to your opinion.


I'd like to retract that statement. I looked over a lot of your
previous posts, and you appear to be a good poster. "Troll", certainly,
was far too strong a word. I'm sorry.

I still disagree that Rod Pemberton is a troll.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 28 '06 #17
Andrew Poelstra wrote:
On 2006-05-28, Default User <de***********@yahoo.com> wrote:
Andrew Poelstra wrote:
but as far as I'm concerned, Default User is a troll.
You're entitled to your opinion.


I'd like to retract that statement. I looked over a lot of your
previous posts, and you appear to be a good poster. "Troll",
certainly, was far too strong a word. I'm sorry.


Sure, not a problem.
I still disagree that Rod Pemberton is a troll.

I think you may want to apply the same review test to him. There's no
doubt that he knows a bit about programming and C, but his
"contributions" are generally of the type to stir up trouble.


Brian
May 28 '06 #18
Andrew Poelstra said:
On 2006-05-28, Default User <de***********@yahoo.com> wrote:
Andrew Poelstra wrote:
but as far as I'm concerned, Default User is a troll.
You're entitled to your opinion.


I'd like to retract that statement. I looked over a lot of your
previous posts, and you appear to be a good poster. "Troll", certainly,
was far too strong a word. I'm sorry.


Well done. You said something silly, then did your research, realised that
the research didn't bear out your claim, and then had the grace to admit it
and apologise. There is much hope for such people.
I still disagree that Rod Pemberton is a troll.


It is possible that "troll" is the wrong word for Mr Pemberton, but our
experience of him here in clc has not been a pleasant one. He has certainly
not endeared himself to the group. Many of the people you would probably
consider to be "good posters" have killfiled him. Even amongst those who
have not, I don't know of any who would be particularly keen to sing his
praises. If you want to understand why people here have a certain antipathy
to Mr Pemberton, you could do worse than to research his posting history in
this newsgroup. In my estimation, such research would explain to you why
people are labeling him "troll" (whether or not that label is, strictly
speaking, accurate - which in my view it probably is not).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 28 '06 #19

Rod Pemberton wrote:
<en******@yahoo.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...

Rod Pemberton wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
> We'd really appreciate it if you'd keep that kind of language out
> of the headers...

Given that :

1) he didn't use "that kind of language" in his message
2) that f & t use the same finger on QWERTY keyboards
3) it's a common "Freudian slip"

how could you honestly believe it was intentional? I think you rushed to judgement here.


Just a quick hint here - try looking up the word "humor".


There was nothing humorous in what he said and there was nothing, such as a
smiley, to indicate that it was humor. I also assumed that since he didn't
post a response to the OP's question, he was offended. That seems to be the
usual case with Pfaff. I also assumed that since he was posting from a
university email, that his school is filtering, flaging, and reviewing his
email for foul language. Do you think my assumptions are invalid?


I took his comments as understated irony, and adding a smiley
would definitely have taken something away from the humor value.

I don't mean to say your view is wrong, only that I find the
understated
irony view completely consistent with Ben's other postings.

May 28 '06 #20

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

Similar topics

14
by: Tina Li | last post by:
Hello, I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum recursion limit exceeded". Here is the pattern string: ...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
3
by: netsurfer | last post by:
Hi: I am working on a project and need assistance, and would really appreciate any feedback on it. First part I'm banging my head against the wall on is about the amounts of the Amount fields...
3
by: mitsura | last post by:
Hi, I have included a small listing. The test program opens a panel and show a bitmap. What I want is to when the mouse is over the bitmap panel, I want to trap the left mouse click. The...
7
by: ethanmys | last post by:
hello everyone. hours ago i found a topic about solving the problem: find out 2 int between certain rang, say 1---100, both the 2 int's sum and minus should be a int's square. i write this piece...
29
by: nessuno | last post by:
I can't find any discussion of this question in this NG. I'd like to implement some variable precision integer arithmetic in C, and do it efficiently. A problem arises with the divide/remainder...
3
by: Brian Wotherspoon | last post by:
I have a table with data that is refreshed regularly but I still need to store the old data. I have created a seperate table with a foreign key to the table and the date on which it was replaced. ...
0
by: tomwolfstein | last post by:
Hi. I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException" The decoder comes an an executable which I've turned into a .dll. This decoder has about...
3
by: Madhur | last post by:
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.....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.