×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

各位老大,现有个作业题,小的不会, 烦请大家帮个忙,脑袋想破也想不出来,请专家们赐教。谢谢了先!

*Consider an ANSI compliant compiler on a machine using 2's
complement arithmetic for integers. The task is
to figure out whether a variable used for integers is signed or not.

One suggested solution is to write a macro like the following:

#define IS_UNSIGNED( var ) \
( (var) >= 0 && ~(var) >= 0 )

(a) Will this macro work for all possible integer values or are there
any exceptions? (If you find any exceptions,
you are encouraged to find a solution that will get rid of those
exceptions.)

(b) If the variable is a type instead of a variable, how would the
macro above be rewritten?

#define IS_UNSIGNED( TYPE ) \

(c) Is it possible to write a function to do this job instead of a
macro? If yes, show how the the macro shown
above can be rewritten as a function. If it cannot be cast as a
function, explain why not. [Either write below,
the IsVarUnsigned( ... ) function or give your reasoning.]
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 各位老大,现有个作业题,小的不会, 烦请大家帮个忙,脑袋想破也想不出来,请专家们赐教。谢谢了先!
    *Consider an ANSI compliant compiler on a machine using 2's
    complement arithmetic for integers. The task is
    to figure out whether a variable used for integers is signed or not.

    One suggested solution is to write a macro like the following:

    #define IS_UNSIGNED( var ) \
    ( (var) >= 0 && ~(var) >= 0 )

    (a) Will this macro work for all possible integer values or are there
    any exceptions? (If you find any exceptions,
    you are encouraged to find a solution that will get rid of those
    exceptions.)

    (b) If the variable is a type instead of a variable, how would the
    macro above be rewritten?

    #define IS_UNSIGNED( TYPE ) \

    (c) Is it possible to write a function to do this job instead of a
    macro? If yes, show how the the macro shown
    above can be rewritten as a function. If it cannot be cast as a
    function, explain why not. [Either write below,
    the IsVarUnsigned( ... ) function or give your reasoning.]
    • me try:)
      1.#define ...
      当你用int定义一个数值1时,出错,原因是以上的定义只能支持你以unsigned int定义的形式定义一个 var。
      改进方法:

      #define ISUNSIGNED(var) ( (var > 0x7fffffff)? (var& 0x80000000) : 1 ) //假设是32位系统,你要进一步改进的话,那就用sys/types.h里面的MAX..等把具体的数值替换了。

      2。type的问题,不懂,我想不出来任何IS_UNSIGNED(unsigned int)这样的东西,将type 作为string处理时可以的,但是不满足你这里的需求。
      • 谢谢。继续顶。
        • 有什么奖励?
        • 你确认你的type部分是这样描述的?
          • 没错的,往type cast 上考虑。
            • type cast怎么cast一个type呢?typeid?,C里面有对应的东西吗?
              • parameter is the type
                • 呵呵,多谢轮胎:)#define IS_UNSIGNEDINT( type) ( (typeid(type) == typeid( unsigned int) ) 如何?
                  • 只见过typeof,没见过typeid.
                    • C++
              • such as (type)(-1)
              • 有符号和没有符号的类型.用来转换数字,结果应该是不同的.
                • 这样也行:)
                  • 不过好像没有value嘛,怎么(type)..呀?
                    • please think. I am tired.
        • 扒皮,2出来了,不可以用function替换,因为type需要在compile的时候处理。你准备怎么意思一下呀?:)
          • 你的第一个还不如原来题里面那个呢。
            • 指点一二?,谢谢:)
              • int i=1; IS_SUNSIGNED(i)的结果是什么?
                • 1<ox7FFFFFFFU, so 是1,true了。
                  • int ! 不是unsigned int.
                    • 明白你的意思了,不过我认为这里是要判断值是不是unsigned int,而不是类型定义的东西,或许我错了:)您有何高见?
                      • int i =-1, ISUNSIGNED(i) 呢?
                      • 不明白你的那个宏到底是用来干吗的。
                        • #define IS_UNSIGNED(var) ( ((var)>=0 && ~(var)>=0 ) || ( (var <= 0) &&~(var)<=0 ) )? 如何?
                          • 你先把这几个数挨个试一遍再贴:int i=0, 1, -1, unsigned int i=1, 0, ~0.
                            • 多了两=,请教这里需要判断unsigned int非法定义吗? 比如unsigned int i=-1是否合法?
                              • 不需要,1、如果非法compiler 会complain;2、unsigned int i=-1,不非法,是合法的付值语句,连警告都不是。
                                • 谢谢,那么原来的定义我没有看出来又什么问题,您高见如何?另外除了typeid和typeof以外,还有什么解决的办法?
    • 2 done. how to pay me?
      #include <stdio.h>

      int main()
      {
      if ((unsigned int)(-1) > 2)
      printf ("dd\n");
      return 0;
      }
      • 有符号和没有符号的类型.用来转换数字,结果是不同的.
        • 虚心请教你的2的macro怎么定义呢?
          • #define IS_UNSIGN(a) ((a)(-1) > 2)
            • 厉害厉害,我还做不到信手拈来:)
    • 我再来:),
      1。没有什么问题。
      2。#define IS_UNSIGNED(type) ( typeid(unsigned int) == typeid( type) )

      test code
      ...
      #include <typeinfo>
      using namespace std;

      #define ULONG unsigned long

      ...

      if( IS_UNSIGNED(ULONG) )
      .....

      不能定义对应的function, 因为function不能在compiler的时候执行。
      • 不建议你用typeid,因为 1、这个题在C++出现之前就有了;2、这很明显是个C的作业,typeid是个C++ keyword, C compiler 不认。
        • :),谢谢,老兄功力深厚呀:),佩服。