有效的代码做64位值左撇子 [英] efficient code to do left shit on 64-bit values

查看:53
本文介绍了有效的代码做64位值左撇子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BC 5.02(长是32位),我想知道是否

有任何有效的代码可以让我

左移64位值我目前以这种方式使用



typedef struct {

unsigned long lsLong;

unsigned long msLong;

} b64_struct;


如果需要,可以更改此结构。


最不重要part可以包含0..2 ^ 32 -1范围内的任何值

。最重要的

部分;然而,通常会为0,但它可能需要一些较小的值,通常在10以下,

虽然可能有更高的值(最多2 ^ 16 -1)<理论上是



提前的转移金额不会提前 -

我会将上面的元组收集到一个1兆阵列,

计算最小值和最大值,并且

根据该值进行移位。 Bits不能落入
bitbucket。


TIA

解决方案

aurgathor写道:


我使用BC 5.02(长是32位),我想知道是否有任何有效的代码可以让我左移我目前使用的64位值
这样:

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




为高位编码创建一个掩码。这是一个单独的问题,

使它干净便携。


测试高位的高位。如果设置,你有一个

溢出,现在是barf的时候了。如果没有,请将该部分移开。


测试低部分的高位。如果设置,则递增

高位部分并重置该位。转移那部分。

完成。


这可能更好地降级为系统相关的程序集

语言例程,如果你可以安排连接,如果使用

足以成为绑定。


-

如果你想通过groups.google.com发布跟进,不要使用

破解的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />


aurgathor写道:


我使用BC 5.02(长期是32位)我想知道是否有任何有效的代码可以让我使用当前使用的64位左移值
这样:
typedef struct {
unsigned long lsLong;
unsigned long msLong;
} b64_struct;

如果需要,可以更改此结构。
最不重要的部分可以包含0..2 ^ 32 -1范围内的任何值。最重要的部分;然而,通常会为0,但它可能会包含一些较小的值,通常低于10,
虽然理论上可以使用更高的值(最多2 ^ 16 -1)。 />
预先不会提到班次 -
我会将上面的元组收集到一个1兆的阵列中,
找出最小值和最大值,并且基于此进行转移。 Bits不能落入bitbucket。




msLong is 0x87654321

lsLong is 0x12345678


左移8:

msLong是0x65432112

lsLong是0x34567800


左移36多:

msLong是0x45678000

lsLong是0x0

/ * BEGIN new.c * /


#include< stdio.h>


typedef struct {

unsigned long lsLong;

unsigned long msLong;

} b64_struct;


b64_struct leftb64(b64_struct值,int shift)

{

if(shift> ; 31){

shift - = 31;

value = leftb64(value,31);

value = leftb64(value,shift) ;

}否则{

if(shift> 0){

value.msLong<< = shift;

value.msLong | = value.lsLong>> 32 - 班次;

value.lsLong<< = shift;

}

}

返回值;

}


int main(无效)

{

b64_struct value = {0x12345678, 0x87654321};


printf(" msLong是0x%lx \ n",value.msLong);

printf(" lsLong是0x% lx \ n",value.lsLong);

value = leftb64(value,8);

puts(" \ nLeft shift 8:");

printf(" msLong is 0x%lx \ n",value.msLong);

printf(" lsLong is 0x%lx \ n",value .lsLong);

put(\ nLeft转移36次:);

value = leftb64(value,36);

printf(" msLong是0x%lx \ n",value.msLong);

printf(" lsLong是0x%lx \ n",value.lsLong);

返回0;

}


/ * END new.c * /

-

pete


aurgathor写道:

我使用BC 5.02(长是32位),我想知道是否有任何有效的代码可以让我使用我当前使用的64位左移值
这样:

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




第0步:看看你是否可以使用`unsigned long long''(由C99引入的一种
类型,但是支持作为扩展,即使是一些C90编译器也是如此当在宽松模式下操作时)。


步骤1:遵循KISS [*]原则,并使用


b64_struct x = {.. 。$;

int bits = ...;

x.msLong =(x.msLong<<位)| (x.lsLong>>(32位));

x.lsLong<< = bits;

[*]"保持简单,愚蠢!


第2步:尝试别的东西。第2步只有在

时才会采取步骤0和1不要成功。他们可能没有的一些可能的原因:


....直到并且除非这被证明是不充分的。一些潜在的不足之处:


- 太慢了(但是直到你实际上你还不知道这个... b $ b测量它,在整个程序的上下文中)


- long long仅在部分但不是全部的

系统中可用


- 在某些系统上,long比32位更宽(很容易

重新拼写32,但你需要决定如何处理

位64及以上)


- 只能移位0< = bits< 32(如果这是一个问题,可以将上面的

包在一个循环中)


步骤3:重新阅读主题行。是否提示,呃,

获取代码的任何极端困难?


-

Eric Sosman
es*****@acm-dot-org.inva 盖子


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

解决方案

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


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


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


这篇关于有效的代码做64位值左撇子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆