我试图将unsigned int与这样的signed char进行比较:
int main(){
unsigned int x = 9;
signed char y = -1;
x < y ? printf("s") : printf("g");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待o/p为"g".相反,它的"s".这里进行了什么样的转换?
我想重新解释一个unsigned long(实际上,a DWORD)作为一个signed long.我试过了:
DWORD x;
long y = reinterpret_cast<signed long>(x);
Run Code Online (Sandbox Code Playgroud)
但是,VC++ 2010 intellisense告诉我"无效的类型转换".为什么?我如何解决它?
我想将两个数字相乘,我知道我的数字总是正数,然后:
unsigned int mulPositiveNumbers(unsigned int a ,unsigned int b)
{
assert(a > 0);
assert(b > 0);
return (a*b);
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用断言告诉自己"给定的数字总是积极的".
但是当我跑步时:
int main()
{
unsigned int res = mulPositiveNumbers(-4,3);
// more stuff goes here
}
Run Code Online (Sandbox Code Playgroud)
代码不会失败,即使我使用的是负数.为什么?
在下面的代码中,我乘以0xffffffff2 unsigned int(32位)并将其存储在unsigned long long(64位)中.为什么我没有得到实际输出8589934588.相反,我得到了4294967294.提前致谢.输出:sizeof i = 4 Sizeof J = 8 2xi = 4294967292
/* Code starts here */
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
unsigned int i=4294967294;
unsigned long long j=i*2;
printf("Sizeof i=%d\n", sizeof(i));
printf("Sizeof J=%d\n", sizeof(j));
printf("2xi=%llu\n", j);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 所以这是代码:
int create_mask(unsigned b, unsigned e)
{
unsigned int mask=1;
if(b<e || b<0 || e<0)
{
printf("Wrong values, starting bit can't be smaller than ending.\n");
printf("Both got to be >= 0.\n");
exit(EXIT_FAILURE);
}
while(b>0)
{
printf("%u\n", b);
mask<<=1;
if(b>e)
mask|=1;
b--;
}
return ~mask; /* negates mask for later purpose that is clearing corresponding bits */
}
Run Code Online (Sandbox Code Playgroud)
函数为某些位操作创建掩码,但应该采用两个无符号整数b和e,两者都是非负数.问题是如何防止用户输入负数?当用(-1,0)调用函数时,它启动循环,并且错误地退出.
我希望or在 2 位图上执行简单的逻辑,但 Swift 认为这是错误的:
let u: UInt8 = 0b1
let i: Int = 0b10
i | u // Binary operator '|' cannot be applied to operands of type 'Int' and 'UInit8'
Run Code Online (Sandbox Code Playgroud)
有什么方法可以符合类型推断并且仍然有效吗?
我总是可以这样做i | Int(u) // 3,但我认为这并不是最佳选择。
我知道如果数字跟随U后缀,则将其视为无符号.但是为什么后续程序打印变量i的正确值,即使它是用负值初始化.(用gcc 4.9.2,4.8.2和4.7.1编译)
Program1.cpp
#include <iostream>
int main()
{
int i=-5U;
std::cout<<i; // prints -5 on gcc 4.9.2, 4.8.2, & 4.7.1
}
Run Code Online (Sandbox Code Playgroud)
Program2.cpp
#include <iostream>
int main()
{
auto i=-5U;
std::cout<<i; // prints large positive number as output
}
Run Code Online (Sandbox Code Playgroud)
但如果我使用auto关键字(类型演绎者新的C++ 0x功能),它会给我一个大的正数,如预期的那样.
请纠正我如果我理解不正确的事情.
在这个简单的示例中,我对使用有符号循环计数器和无符号循环计数器之间的差异感到非常惊讶:
double const* a;
__assume_aligned(a, 64);
double s = 0.0;
//for ( unsigned int i = 0; i < 1024*1024; i++ )
for ( int i = 0; i < 1024*1024; i++ )
{
s += a[i];
}
Run Code Online (Sandbox Code Playgroud)
在签名的情况下,生成了 icc 19.0.0(我显示了循环的展开部分):
..B1.2:
vaddpd zmm7, zmm7, ZMMWORD PTR [rdi+rax*8]
vaddpd zmm6, zmm6, ZMMWORD PTR [64+rdi+rax*8]
vaddpd zmm5, zmm5, ZMMWORD PTR [128+rdi+rax*8]
vaddpd zmm4, zmm4, ZMMWORD PTR [192+rdi+rax*8]
vaddpd zmm3, zmm3, ZMMWORD PTR [256+rdi+rax*8]
vaddpd zmm2, zmm2, ZMMWORD PTR [320+rdi+rax*8]
vaddpd zmm1, …Run Code Online (Sandbox Code Playgroud) optimization assembly icc compiler-optimization unsigned-integer
我见过浮点位黑客产生平方根,如此处所示快速浮点平方根,但此方法适用于浮点数。
是否有类似的方法可以在不循环 32 位无符号整数的情况下求整数平方根?我一直在网上搜索,但没有看到
(我的想法是纯二进制表示没有足够的信息来做到这一点,但由于它被限制为 32 位,我会猜测)
这是 Python 中的一个简单的线性同余生成器:
def prng(n):
# https://en.wikipedia.org/wiki/Lehmer_random_number_generator
while True:
n = n * 48271 % 0x7fffffff
yield n
g = prng(123)
for i in range(10**8):
next(g)
assert next(g) == 1062172093
Run Code Online (Sandbox Code Playgroud)
Python 2.7 比任何版本的 Python 3.x 都快。在 Linux 上生成 100M 项:
python2.7 g.py 14.60s user 0.65s system 99% cpu 15.260 total
python3.6 g.py 18.70s user 0.00s system 99% cpu 18.711 total
python3.7 g.py 18.10s user 0.04s system 99% cpu 18.193 total
python3.8 g.py 19.22s user 0.02s system 99% cpu …Run Code Online (Sandbox Code Playgroud) unsigned-integer ×10
c ×4
c++ ×3
assembly ×1
assert ×1
biginteger ×1
binary ×1
char ×1
cpython ×1
icc ×1
optimization ×1
performance ×1
python ×1
square-root ×1
swift ×1
unsigned ×1