如何判断 32 位 int 是否适合 16 位 Short

发布于 2024-12-03 10:11:41 字数 284 浏览 1 评论 0原文

仅使用:

! ~ & ^ | + << >>

我需要找出有符号的 32 位整数是否可以表示为 16 位二进制补码整数。

我的第一个想法是将 MSB 16 位和 LSB 16 位分开,然后使用掩码来处理最后 16 位,这样如果它不为零,则无法表示,然后使用该数字来检查 MSB 位。

我需要编写的函数示例是:fitsInShort(33000) = 0(无法表示)和fitsInShort(-32768) = 1(可以表示)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

这样的小城市 2024-12-10 10:11:41
bool fits16(int x)
{
    short y = x;
    return y == x;
}

只是开玩笑:)这是真正的答案,假设 int 是 32 位,short 是 16 位,并且二进制补码表示:

编辑:请参阅最后的编辑以获得正确的答案!

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

没有 if 语句我相信应该做吧:

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

编辑:奥利是对的。我不知何故认为他们是被允许的。这是最后一次尝试,并附有解释:

我们需要 x 的 17 个最高有效位全为 1 或全为 0。因此,让我们从屏蔽其他位开始:

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;

或更简洁地说:

return ((x & 0xffff8000) + 0x8000) & 0xffff7fff;
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;
荒岛晴空 2024-12-10 10:11:41

如果 32 位数字在 [-32768,+32767] 范围内,则 17 个 msb 将全部相同。

这是一种仅使用您的操作来判断 3 位数字是全 1 还是全 0 的蹩脚方法(我假设您不允许使用条件控制结构,因为它们需要隐式逻辑操作):

int allOnes3(int x)
{
    return ((x >> 0) & (x >> 1) & (x >> 2)) & 1;
}

int allTheSame3(int x)
{
    return allOnes3(x) | allOnes3(~x);
}

我会离开你扩展/改进这个概念。

If a 32-bit number is in the range [-32768,+32767], then the 17 msbs will all be the same.

Here's a crappy way of telling if a 3-bit number is all ones or all zeros using only your operations (I'm assuming that you're not allowed conditional control structures, because they require implicit logical operations):

int allOnes3(int x)
{
    return ((x >> 0) & (x >> 1) & (x >> 2)) & 1;
}

int allTheSame3(int x)
{
    return allOnes3(x) | allOnes3(~x);
}

I'll leave you to extend/improve this concept.

花桑 2024-12-10 10:11:41

这是一个没有强制转换、if 语句并且仅使用您要求的运算符的解决方案:

#define fitsInShort(x) !(((((x) & 0xffff8000) >> 15) + 1) & 0x1fffe)

Here's a solution without casting, if-statements and using only the operators you asked for:

#define fitsInShort(x) !(((((x) & 0xffff8000) >> 15) + 1) & 0x1fffe)
倾`听者〃 2024-12-10 10:11:41
short fitsInShort(int x)
{
    int positiveShortRange = (int) ((short) 0xffff / (short) 2);
    int negativeShortRange = (int) ((short) 0xffff / (short) 2) + 1;

    if(x > negativeShortRange && x < positiveShortRange)
        return (short) x;
    else
        return (short) 0;
}
short fitsInShort(int x)
{
    int positiveShortRange = (int) ((short) 0xffff / (short) 2);
    int negativeShortRange = (int) ((short) 0xffff / (short) 2) + 1;

    if(x > negativeShortRange && x < positiveShortRange)
        return (short) x;
    else
        return (short) 0;
}
叶落知秋 2024-12-10 10:11:41
if (!(integer_32 & 0x8000000))
{
   /* if +ve number */
  if (integer_32 & 0xffff8000)
    /* cannot fit */
  else 
    /* can fit */
}
else if (integer_32 & 0x80000000)
{
  /* if -ve number */
  if ( ~((integer_32 & 0xffff8000) | 0x00007fff))
    /* cannot fit */
  else
    /* can fit */
}

First if 首先通过检查有符号位来检查 +ve 数字。如果 +ve ,则检查第 15 位到第 31 位是否为 0,如果为 0,则它不能适合 short,否则可以。

如果位 15 至 31 全部设置(2 的补码方法表示),则负数在范围内。

因此,第二个如果它是一个-ve数,则位15到31被屏蔽,并且设置剩余的低位(0到14)。如果这是0xffffffff,那么只有一个的补码将为0,这表明位15到31都已设置,因此它可以适合(else部分),否则它不适合(if 条件)。

if (!(integer_32 & 0x8000000))
{
   /* if +ve number */
  if (integer_32 & 0xffff8000)
    /* cannot fit */
  else 
    /* can fit */
}
else if (integer_32 & 0x80000000)
{
  /* if -ve number */
  if ( ~((integer_32 & 0xffff8000) | 0x00007fff))
    /* cannot fit */
  else
    /* can fit */
}

First if Checks for +ve number first by checking the signed bit. If +ve , then it checks if the bit 15 to bit 31 are 0, if 0, then it cannot fit into short, else it can.

The negative number is withing range if bit 15 to 31 are all set (2's complement method representation).

Therefore The second if it is a -ve number, then the bit 15 to 31 are masked out and the remaining lower bits (0 to 14) are set. If this is 0xffffffff then only the one's complement will be 0, which indicates the bit 15 to 31 are all set, therefore it can fit (the else part), otherwise it cannot fit (the if condition).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文