我在我正在编写的代码中发现了这一点,并认为这是我遇到的一些问题的原因.
在某处的标题中:
enum SpecificIndexes{
//snip
INVALID_INDEX = -1
};
Run Code Online (Sandbox Code Playgroud)
然后 - 初始化:
nextIndex = INVALID_INDEX;
Run Code Online (Sandbox Code Playgroud)
并使用
if(nextIndex != INVALID_INDEX)
{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
调试代码时,nextIndex中的值并没有完全显示(它们非常大),我发现它已声明:
unsigned int nextIndex;
Run Code Online (Sandbox Code Playgroud)
因此,对INVALID_INDEX的初始设置是使unsigned int下溢并将其设置为一个巨大的数字.我认为这是导致问题的原因,但是仔细观察,测试
if(nextIndex != INVALID_INDEX)
Run Code Online (Sandbox Code Playgroud)
行为正确,即,当nextIndex是"大+值"时,它从未执行if的主体.
它是否正确?这是怎么回事?枚举值是否被隐含地转换为与变量相同类型的unsigned int,因此以相同的方式包装?
干杯,
XAN
什么是C#的Convert.ToInt16(String)方法的C等价物?在我的情况下,我的字符串是一个char数组.谢谢
作为我CS课程的一部分,我已经获得了一些功能.其中一个函数接受一个指向无符号字符的指针,将一些数据写入文件(我必须使用这个函数,所以我不能只使用自己的目的构建函数,使用不同的BTW).我需要编写一个整数数组,其值使用此函数可以达到4095(仅使用无符号字符).
但是我认为unsigned char的最大值只有256,因为它长1个字节,我是对的吗?因此,我需要为每个整数使用4个无符号字符?但是,对于整数,转换似乎不适用于较大的值.有没有人知道如何最好地将整数数组转换为无符号字符?
我正在尝试使用pymongo在mongodb中插入一个64位无符号整数.整数是CRC64算法的输出.我试着跟随:
long(crc64(unicode(kw).encode('unicode-escape'))))
Run Code Online (Sandbox Code Playgroud)
如果我将其插入mongodb,它开始抱怨mongodb只支持64位整数.接下来我尝试将其转换为带符号的64位int,如下所示:
ctypes.c_int64(crc64(unicode(kw).encode('unicode-escape')))).value
Run Code Online (Sandbox Code Playgroud)
哪种工作,mongodb停止抱怨我的int的大小,但是当我看到mongodb中的数据时,我得到了这个:
{
"_id" : {
"floatApprox" : -5307924876159732000,
"top" : 3059119730,
"bottom" : 2651469802 },
"keyword" : "redacted",
"normal_hash" : {
"floatApprox" : -671156942315906300,
"top" : 4138701393,
"bottom" : 549001936
}
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?有没有办法将64位int作为一个int放入db(不关心它是有符号还是无符号.)
我有一个unsigned并希望将其转换为uint64_t(如果可能的话).
我怎么做?如果可能的话,我想避免依赖于未定义的行为.
谢谢!
在C#编程语言(涵盖C#4.0)(第4版),1.3类型和变量,第9页.
Hooray for byte是一个无符号类型!事实上,在Java中,一个字节被签名(并且没有无符号的等价物),这使得很多无意义的错误容易出错.很可能我们都应该比我们更多地使用uint,请注意:我确信许多开发人员在需要整数类型时默认达到int.框架设计者当然也属于这一类:为什么String.Length应该签名?
当我反编译String.Length ;
/// <summary>
/// Gets the number of characters in the current <see cref="T:System.String"/> object.
/// </summary>
///
/// <returns>
/// The number of characters in the current string.
/// </returns>
/// <filterpriority>1</filterpriority>
[__DynamicallyInvokable]
public int Length { [SecuritySafeCritical, __DynamicallyInvokable, MethodImpl(MethodImplOptions.InternalCall)] get; }
Run Code Online (Sandbox Code Playgroud)
也在MSDN中 ;
Length属性返回此实例中Char对象的数量,而不是Unicode字符数.原因是Unicode字符可能由多个Char表示.
它返回当前字符串中的字符对象数.为什么在已经存在类型的情况下String.Length返回?什么是的点和?Int32UInt32signed-unsigned byteString.Length
这是一段代码:
unsigned short a=-1;
unsigned char b=-1;
char c=-1;
unsigned int x=-1;
printf("%d %d %d %d",a,b,c,x);
Run Code Online (Sandbox Code Playgroud)
为什么输出是这样的:
65535 255 -1 -1
Run Code Online (Sandbox Code Playgroud)
?
有人可以分析一下吗?
码:
unsigned int i = 1<<31;
printf("%d\n", i);
Run Code Online (Sandbox Code Playgroud)
为什么输出是-2147483648负值?
更新的问题:
#include <stdio.h>
int main(int argc, char * argv[]) {
int i = 1<<31;
unsigned int j = 1<<31;
printf("%u, %u\n", i, j);
printf("%d, %d\n", i, j);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以上印刷品:
2147483648, 2147483648
-2147483648, -2147483648
Run Code Online (Sandbox Code Playgroud)
那么,这是否意味着,signed int和unsigned int具有相同的位值,不同之处在于将第31位转换为数值时如何处理?
最近我一直在努力学习C#,但是我无法理解某些东西.每个整数类型都有一个大小(有符号8位,无符号8位,有符号16位,无符号16位等).我很难理解尺寸究竟是什么,以及它们如何达到这个尺寸.8bit,16bit,32bit等是什么意思?签名和未签名也是如此.我不明白这些.如果有人可以引用我的链接解释位,签名和未签名,或甚至向我解释,那将是伟大的.谢谢
POSIX读取函数定义为ssize_t read(int fd, void *buf, size_t count);,将其buf参数定义为void*.
传入的实际缓冲区是chars还是unsigned chars 的数组是否重要?如果是这样,我应该使用哪一个?我用谷歌搜索并阅读了这个人,但是甚至没有提到缓冲区的类型,更不用说它的签名了.