Connecting Tech Pros Worldwide Forums | Help | Site Map

efficient code to do left shit on 64-bit values

aurgathor
Guest
 
Posts: n/a
#1: May 27 '06
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



CBFalconer
Guest
 
Posts: n/a
#2: May 27 '06

re: efficient code to do left shit on 64-bit values


aurgathor wrote:[color=blue]
>
> 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;[/color]

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/>

pete
Guest
 
Posts: n/a
#3: May 27 '06

re: efficient code to do left shit on 64-bit values


aurgathor wrote:[color=blue]
>
> 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.[/color]

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
Eric Sosman
Guest
 
Posts: n/a
#4: May 27 '06

re: efficient code to do left shit on 64-bit values


aurgathor wrote:[color=blue]
> 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;[/color]

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
esosman@acm-dot-org.invalid


Ben Pfaff
Guest
 
Posts: n/a
#5: May 27 '06

re: efficient code to do left shit on 64-bit values


We'd really appreciate it if you'd keep that kind of language out
of the headers...
--
Ben Pfaff
email: blp@cs.stanford.edu
web: http://benpfaff.org
Rod Pemberton
Guest
 
Posts: n/a
#6: May 27 '06

re: efficient code to do left shit on 64-bit values



"Ben Pfaff" <blp@cs.stanford.edu> wrote in message
news:87verr4f1r.fsf@benpfaff.org...[color=blue]
> We'd really appreciate it if you'd keep that kind of language out
> of the headers...[/color]

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


ena8t8si@yahoo.com
Guest
 
Posts: n/a
#7: May 27 '06

re: efficient code to do left shit on 64-bit values



Rod Pemberton wrote:[color=blue]
> "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> news:87verr4f1r.fsf@benpfaff.org...[color=green]
> > We'd really appreciate it if you'd keep that kind of language out
> > of the headers...[/color]
>
> 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.[/color]

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

Rod Pemberton
Guest
 
Posts: n/a
#8: May 27 '06

re: efficient code to do left shit on 64-bit values



<ena8t8si@yahoo.com> wrote in message
news:1148763597.825442.89790@j33g2000cwa.googlegro ups.com...[color=blue]
>
> Rod Pemberton wrote:[color=green]
> > "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> > news:87verr4f1r.fsf@benpfaff.org...[color=darkred]
> > > We'd really appreciate it if you'd keep that kind of language out
> > > of the headers...[/color]
> >
> > 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[/color][/color]
to[color=blue][color=green]
> > judgement here.[/color]
>
> Just a quick hint here - try looking up the word "humor".
>[/color]

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


Joe Wright
Guest
 
Posts: n/a
#9: May 27 '06

re: efficient code to do left shit on 64-bit values


Rod Pemberton wrote:[color=blue]
> <ena8t8si@yahoo.com> wrote in message
> news:1148763597.825442.89790@j33g2000cwa.googlegro ups.com...[color=green]
>> Rod Pemberton wrote:[color=darkred]
>>> "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
>>> news:87verr4f1r.fsf@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[/color][/color]
> to[color=green][color=darkred]
>>> judgement here.[/color]
>> Just a quick hint here - try looking up the word "humor".
>>[/color]
>
> 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
>
>[/color]
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 ---
Rod Pemberton
Guest
 
Posts: n/a
#10: May 28 '06

re: efficient code to do left shit on 64-bit values



"Joe Wright" <joewwright@comcast.net> wrote in message
news:MOadnbxorcAgSuXZ4p2dnA@comcast.com...[color=blue]
> Rod Pemberton wrote:[color=green]
> > <ena8t8si@yahoo.com> wrote in message
> > news:1148763597.825442.89790@j33g2000cwa.googlegro ups.com...[color=darkred]
> >> Rod Pemberton wrote:
> >>> "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> >>> news:87verr4f1r.fsf@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[/color]
> > to[color=darkred]
> >>> judgement here.
> >> Just a quick hint here - try looking up the word "humor".
> >>[/color]
> >
> > There was nothing humorous in what he said and there was nothing, such[/color][/color]
as a[color=blue][color=green]
> > smiley, to indicate that it was humor. I also assumed that since he[/color][/color]
didn't[color=blue][color=green]
> > post a response to the OP's question, he was offended. That seems to be[/color][/color]
the[color=blue][color=green]
> > usual case with Pfaff. I also assumed that since he was posting from a
> > university email, that his school is filtering, flaging, and reviewing[/color][/color]
his[color=blue][color=green]
> > email for foul language. Do you think my assumptions are invalid?
> >
> >[/color]
> 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'.
>[/color]

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.
[color=blue]
> We've known Ben for years. You have come to us only recently.[/color]

Recently? No. Whether my posts made it to Google or only a few select
servers is a different issue...
[color=blue]
> I personally don't care what assumptions you draw about Ben but you might
> consider keeping the negative ones to yourself.[/color]

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.
[color=blue]
> 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?[/color]

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


Default User
Guest
 
Posts: n/a
#11: May 28 '06

re: efficient code to do left shit on 64-bit values


Joe Wright wrote:
[color=blue][color=green]
> > Rod Pemberton
> >
> >[/color]
> Rod,
>
> Ben Pfaff is well known here and is a friend.[/color]


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



Brian
aurgathor
Guest
 
Posts: n/a
#12: May 28 '06

re: efficient code to do left shit on 64-bit values


Sorry, 't' got shifted and overwrote 'f'....

And thanks for all those who replied to my question.

aur

"Ben Pfaff" <blp@cs.stanford.edu> wrote in message
news:87verr4f1r.fsf@benpfaff.org...[color=blue]
> We'd really appreciate it if you'd keep that kind of language out
> of the headers...
> --
> Ben Pfaff
> email: blp@cs.stanford.edu
> web: http://benpfaff.org[/color]


santosh
Guest
 
Posts: n/a
#13: May 28 '06

re: efficient code to do left shit on 64-bit values


Joe Wright wrote:
.... snip ...[color=blue]
> 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.[/color]

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

Andrew Poelstra
Guest
 
Posts: n/a
#14: May 28 '06

re: efficient code to do left shit on 64-bit values


On 2006-05-28, Default User <defaultuserbr@yahoo.com> wrote:[color=blue]
> Joe Wright wrote:
>[color=green][color=darkred]
>> > Rod Pemberton
>> >
>> >[/color]
>> Rod,
>>
>> Ben Pfaff is well known here and is a friend.[/color]
>
>
> Joe, Rod Pemberton is well known here and is a troll.
>
>[/color]
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.
Default User
Guest
 
Posts: n/a
#15: May 28 '06

re: efficient code to do left shit on 64-bit values


Andrew Poelstra wrote:
[color=blue]
> On 2006-05-28, Default User <defaultuserbr@yahoo.com> wrote:[/color]

[color=blue][color=green]
> > Joe, Rod Pemberton is well known here and is a troll.
> >
> >[/color]
> That is completely untrue. I'll let Rod post a decent defense,[/color]

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

You're entitled to your opinion.



Brian

Ben Pfaff
Guest
 
Posts: n/a
#16: May 28 '06

re: efficient code to do left shit on 64-bit values


"santosh" <santosh.k83@gmail.com> writes:
[color=blue]
> Joe Wright wrote:
> ... snip ...[color=green]
>> 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.[/color]
>
> I was under the impression that Ben Pfaff was a faculty of Stanford.[/color]

Definitely not.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Andrew Poelstra
Guest
 
Posts: n/a
#17: May 28 '06

re: efficient code to do left shit on 64-bit values


On 2006-05-28, Default User <defaultuserbr@yahoo.com> wrote:[color=blue]
> Andrew Poelstra wrote:[color=green]
>> but as far as I'm concerned, Default User is a troll.[/color]
>
> You're entitled to your opinion.[/color]

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.
Default User
Guest
 
Posts: n/a
#18: May 28 '06

re: efficient code to do left shit on 64-bit values


Andrew Poelstra wrote:
[color=blue]
> On 2006-05-28, Default User <defaultuserbr@yahoo.com> wrote:[color=green]
> > Andrew Poelstra wrote:[color=darkred]
> >> but as far as I'm concerned, Default User is a troll.[/color]
> >
> > You're entitled to your opinion.[/color]
>
> 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.[/color]

Sure, not a problem.
[color=blue]
> I still disagree that Rod Pemberton is a troll.[/color]


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
Richard Heathfield
Guest
 
Posts: n/a
#19: May 28 '06

re: efficient code to do left shit on 64-bit values


Andrew Poelstra said:
[color=blue]
> On 2006-05-28, Default User <defaultuserbr@yahoo.com> wrote:[color=green]
>> Andrew Poelstra wrote:[color=darkred]
>>> but as far as I'm concerned, Default User is a troll.[/color]
>>
>> You're entitled to your opinion.[/color]
>
> 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.[/color]

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.
[color=blue]
> I still disagree that Rod Pemberton is a troll.[/color]

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)
ena8t8si@yahoo.com
Guest
 
Posts: n/a
#20: May 28 '06

re: efficient code to do left shit on 64-bit values



Rod Pemberton wrote:[color=blue]
> <ena8t8si@yahoo.com> wrote in message
> news:1148763597.825442.89790@j33g2000cwa.googlegro ups.com...[color=green]
> >
> > Rod Pemberton wrote:[color=darkred]
> > > "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> > > news:87verr4f1r.fsf@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[/color][/color]
> to[color=green][color=darkred]
> > > judgement here.[/color]
> >
> > Just a quick hint here - try looking up the word "humor".
> >[/color]
>
> 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?[/color]

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.

Closed Thread