English 中文(简体)
核对C类[封闭式]超量流动
原标题:Bitwise overflow checking in c [closed]
Closed. This question needs debugging details. It is not currently accepting answers.

I am trying to write two functions that will check/prevent overflow in c (using only ! ~ | & ^ +) but cant get it. The first is will a certain twos compliment/signed int will fit in a certatin amount of bits: fitsB(int x, int n) where is the int and n is the size of bits to use. Also a function that will check to see if two ints will not overflow when added together: overflowInt(int x, int y). I can get it if they are unsigned ints but the negatives just make things harder for me. Anyone know how to?

也没有投放物,ts矿总有32处。

最佳回答
/* 
 * addOK - Determine if can compute x+y without overflow
 *   Example: addOK(0x80000000,0x80000000) = 0,
 *            addOK(0x80000000,0x70000000) = 1, 
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 20
 *   Rating: 3
 */
 int addOK(int x, int y) {
  // Find the sign bit in each word
  //if a and b have different signs, you cannot get overflow.
  //if they are the same, check that a is different from c and b is different from c, 
  // if they are the same, then there was no overflow.
  int z=x+y;
  int a=x>>31;
  int b=y>>31;
  int c=z>>31;
  return !!(a^b)|(!(a^c)&!(b^c));
}
问题回答

x将安装在纳米轨道上;2^(n-1)。

外流问题需要更多信息。 如果你指派给他们很长一段时间(或翻了一番),两点就不会超支。

采用上述例子(Adam Shiemke),你可以找到最高(正值)价值和最低值(负值),以达到最高(正值)数值2^(n-1)(如Adam s)和最低(正值)数值,以达到最高/正数。 最低值减去2^(n-1),以获得最低值x =>-(2^(n-1)); (说明“和”;=“不”;最低范围)。 例如,N = 4 bits, 2^(4-1) - 1 = 2^3 -1 = 7 so x <= 7 and x >=-8 = ((2^(4-1))。

假设初步投入不会超过32倍的四分之四(在这种条件下自然会出现错误),而现在使用的比照数则不到32(因为你增加了1个,增加了32个,如果你有32个比值,就会流出,详见下文解释)。

如果增加值最大,则确定增量是否会超支; 通过使用algebra,我们可以获取;=最高价值——x。 然后,你可以比较过去的价值,如果它不能满足条件,增加的数额就会超支。 例如,如果X是最高值,那么 y;=0,那么,那么,这种数值必须低于或等于零,或者增加将超过。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签