oggplay 发表于 2014-5-22 11:31:55

发个技术交流贴活跃一下气氛!!!!!!!!!

本帖最后由 oggplay 于 2014-5-22 12:27 编辑

假设我们将一个w位的字节中的字节从0(最低位)到w/8-1位(最高位)编号。写出下面C函数的代码,它会返回一个无符号值,其中参数x的字节i被替换成字节b:

unsigned put_byte (unsigned x,unsigned char b,int i);

以下的一些示例,说明了这个函数该如何工作:
replace_byte ( 0x12345678 , 0xAB, 2) -->0x12AB5678
replace_byte ( 0x12345678 , 0xAB, 0) -->0x123456AB
特意限制了你能使用的编程结构,你的代码必须遵守下面规则:
*假设:
1、整数用补码形式表示。
2、有符号数的右移是算数右移。
3、数据类型int是w位长的。对于某些题目,会给定w的值,但是在其他情况下,只要w是8的整数倍,你的代码就应该能工作。你可以用表达式sizeof(int)<<3来计算w。
(4、考虑机器之间的移植,如大、小端机器,16位和32位或64位。)
*禁止使用:
1、条件语句(if或者?:)、循环、分支语句、函数调用和宏调用。
2、除法、模运算和乘法。
3、相对比较运算符(<、>、<=、和>=)。
4、强制类型转换,无论是显式还是隐式的。
*允许的运算:
1、所有位级运算和逻辑运算。
2、加减法
3、左、右移,但是位移的数量只能在0和w-1之间。
4、整型常数INT_MIN和INT_MAX。
5、相等(==)和不相等(!=)测试。


AepKill 发表于 2014-5-22 11:31:56

#include <stdio.h>
unsigned put_byte (unsigned x,unsigned char b,int i)
{
        unsigned mask=0xFF;
        mask=mask<<(i<<3);
        mask=~mask;
        x&=mask;
        x|=b<<(i<<3);
        return x;
}
int main(void)
{
      unsigned a=0x12345678;
      printf("%x\n",put_byte(a,0xAb,0));
      printf("%x\n",put_byte(a,0xAb,2));
      return 0;
}
vs系列asm中可以使用局部变量。

oggplay 发表于 2014-5-22 20:00:42

没人会??只能自己明天公布答案了:sweat:

向往青莲 发表于 2014-5-22 23:19:57

我是来膜拜楼主的。。。

牡丹花下死做鬼 发表于 2014-5-23 18:36:04

我同楼上一样 膜拜~~~

AepKill 发表于 2014-5-23 19:37:56

#include <stdio.h>
unsigned put_byte (unsigned x,unsigned char b,int i)
{
        unsigned char *p=(unsigned char*)&x;
        *(p+i)=b;
        return x;
}
int main(void)
{
        unsigned a=0x12345678;
        printf("%x\n",put_byte(a,0xAb,0));
        printf("%x",put_byte(a,0xAb,2));
        return 0;
}

oggplay 发表于 2014-5-23 20:45:24

本帖最后由 oggplay 于 2014-5-23 20:47 编辑

AepKill 发表于 2014-5-23 19:37 static/image/common/back.gif
#include
unsigned put_byte (unsigned x,unsigned char b,int i)
{

没仔细看题 0分 你违反了禁止使用的第4条

AepKill 发表于 2014-5-23 20:57:22

#include <stdio.h>
unsigned put_byte (unsigned x,unsigned char b,int i)
{
                _asm
                {
                        lea esi,x
                        add esi,i
                        lea edi,b
                        mov ah,b
                        mov ,ah
                        sub esi,i
                        mov eax,
                }
}
int main(void)
{
      unsigned a=0x12345678;
      printf("%x\n",put_byte(a,0xAb,0));
      printf("%x",put_byte(a,0xAb,2));
      return 0;
}

oggplay 发表于 2014-5-23 21:04:41

本帖最后由 oggplay 于 2014-5-23 21:20 编辑

AepKill 发表于 2014-5-23 20:57 static/image/common/back.gif
#include
unsigned put_byte (unsigned x,unsigned char b,int i)
{

嘻嘻,编译通不过,请用C语言编写函数代码就可以了,不需要内联汇编;另一点局部变量是不能出现在asm里边的,asm里只能用全局变量。

elvo 发表于 2014-5-23 23:29:37

额。。。。看题都有些看不懂。。。膜拜一下大神。。然后看了5楼的程序,估计知道了题意,程序如下:
麻烦楼主大神检测一下。。。#include <stdio.h>
unsigned put_byte (unsigned x,unsigned char b,int i)
{
                unsigned tmp=x;
                x=x>>(i+1)*8;
                x=x<<8;
                x=x+b;
                x=x<<i*8;
                tmp=tmp<<(4-i)*8-1;
                tmp=tmp>>(4-i)*8-1;
      x=x+tmp;
      return x;
}
int main(void)
{
      unsigned a=0x12345678;
      
      printf("%x\n",put_byte(a,0xAb,0));
      printf("%x\n",put_byte(a,0xAb,2));
      return 0;
}

oggplay 发表于 2014-5-24 08:28:39

本帖最后由 oggplay 于 2014-5-24 08:30 编辑

elvo 发表于 2014-5-23 23:29 static/image/common/back.gif
额。。。。看题都有些看不懂。。。膜拜一下大神。。然后看了5楼的程序,估计知道了题意,程序如下:
麻烦楼 ...
存在bug.. 看到编译器警告了么?
5分满分,只能给1分.:titter:

oggplay 发表于 2014-5-24 08:33:52

本帖最后由 oggplay 于 2014-5-24 09:35 编辑

AepKill 发表于 2014-5-23 23:36 static/image/common/back.gif
#include
unsigned put_byte (unsigned x,unsigned char b,int i)
{

不错,接近满分,你的函数可以简化一下:
unsigned replace_byte(unsigned x, unsigned char b, int i)
{
return ( b << (i << 3) ) | ( x & ~(0xff << (i << 3)) );
}



仰望天上的光 发表于 2014-5-24 10:30:04

oggplay 发表于 2014-5-24 08:33 static/image/common/back.gif
不错,接近满分,你的函数可以简化一下:
unsigned replace_byte(unsigned x, unsigned char b, int i)
...
不是说隐式类型转换也不能用吗?
你的return ( b << (i << 3) ) | ( x & ~(0xff << (i << 3)) );
中b << (i << 3)是unsigned char 类型的, x & ~(0xff << (i << 3)是unsigned int类型的,
它们按位或的时候,unsigned char 类型会先隐式转换为unsigned int类型,然后再执行按位运算。
也就是说,你的这个答案违背了的你第4条规定。

oggplay 发表于 2014-5-24 11:23:55

本帖最后由 oggplay 于 2014-5-24 11:25 编辑

仰望天上的光 发表于 2014-5-24 10:30 static/image/common/back.gif
不是说隐式类型转换也不能用吗?
你的return ( b
哈哈,这都被你看出来了,原稿书籍翻译错误,我就没改。将第4条隐式去掉。:big

Mikel 发表于 2014-6-20 10:54:48

:sad我还是太菜了,回去敲代码去了。
页: [1]
查看完整版本: 发个技术交流贴活跃一下气氛!!!!!!!!!