如何判断一个32位的int可以容纳在一个16位的短 [英] How to tell if a 32 bit int can fit in a 16 bit short

查看:547
本文介绍了如何判断一个32位的int可以容纳在一个16位的短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有使用:

 ! 〜&安培; ^ | + LT;< >>

我需要找出psented为16位,补整数,如果32位有符号整数,可以重新$ P $。

我的第一个想法是到MSB 16位和LSB 16位分开,然后用一个面具和最后16位,所以如果其不为零,它不会是能够被重新presented,然后使用一些检查MSB位。

我需要写函数的一个例子是:fitsInShort(33000)= 0(不能重新presented)和fitsInShort(-32768)= 1(可重新presented)


解决方案

 布尔fits16(INT X)
{
    短Y = X;
    返回是== X;
}

只是开个玩笑:)这里是真正的答案,假设int是32位,短为16位和补重presantation:

编辑:请参阅正确答案的最后修改

 布尔fits16(INT X)
{
    / *屏蔽掉至少显著字* /
    INT Y = X&放大器; 0xFFFF0000地址;
    如果(X安培; 0x00008000){
        返回是== 0xFFFF0000地址;
    }其他{
        返回是== 0;
    }
}

如果没有if语句我beleive应该这样做:

 收益率(
    !((X安培;!为0xffff0000)||(X安培;!0x00008000))||
    !((X安培;为0xffff0000)||(X安培; 0x00008000))
);

编辑:奥利奇的权利。我总觉得,他们被允许。这里的最后一次尝试,并提供了解释:

我们需要的17最显著位X 是要么全部为一或全部为零。因此,让我们通过屏蔽等位出启动:

  int类型的= X&放大器; 0xffff8000; //我们需要一个是要么0xffff8000或00000000
INT B = A + 0x00008000; //如果== 0xffff8000那么B现在是00000000
                        //如果== 00000000,然后B现在0x00008000
                        //在任何其他情况下,B有不同的价值
INT C = B和; 0xffff7fff;它是否适合,如果它不//全零,别的东西
返回℃;

或者更简洁:

 收益率((X安培; 0xffff8000)+为0x8000)及0xffff7fff;

Using only:

! ~ & ^ | + << >>

I need to find out if a signed 32 bit integer can be represented as a 16 bit, two's complement integer.

My first thoughts were to separate the MSB 16 bits and the LSB 16 bits and then use a mask to and the last 16 bits so if its not zero, it wont be able to be represented and then use that number to check the MSB bits.

An example of the function I need to write is: fitsInShort(33000) = 0 (cant be represented) and fitsInShort(-32768) = 1 (can be represented)

解决方案

bool fits16(int x)
{
    short y = x;
    return y == x;
}

Just kidding :) Here's the real answer, assuming int is 32 bits and short is 16 bits and two's complement represantation:

Edit: Please see the last edit for the correct answer!

bool fits16(int x)
{
    /* Mask out the least significant word */
    int y = x & 0xffff0000;
    if (x & 0x00008000) {
        return y == 0xffff0000;
    } else {
        return y == 0;
    }
}

Without if statements i beleive that should do it:

return (
    !(!(x & 0xffff0000) || !(x & 0x00008000)) ||
    !((x & 0xffff0000) || (x & 0x00008000))
);

Edit: Oli's right. I somehow thought that they were allowed. Here's the last attempt, with explanation:

We need the 17 most significant bits of x to be either all ones or all zeroes. So let's start by masking other bits out:

int a = x & 0xffff8000; // we need a to be either 0xffff8000 or 0x00000000
int b = a + 0x00008000; // if a == 0xffff8000 then b is now 0x00000000
                        // if a == 0x00000000 then b is now 0x00008000
                        // in any other case b has a different value
int c = b & 0xffff7fff; // all zeroes if it fits, something else if it doesn't
return c;

Or more concisely:

return ((x & 0xffff8000) + 0x8000) & 0xffff7fff;

这篇关于如何判断一个32位的int可以容纳在一个16位的短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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