unsigned

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

北慕城南 提交于 2019-11-28 00:51:16
I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t , and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t . I can't find any clear reference for specified behavior when negating an unsigned integer. Is there any, and if so, what is it? Edit: Ok, so there are some good answers regarding arithmetic on unsigned types, but it's not clear that this is in fact such. When this

why is char's sign-ness not defined in C?

≯℡__Kan透↙ 提交于 2019-11-28 00:40:59
The C standard states: ISO/IEC 9899:1999, 6.2.5.15 (p. 49) The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char. And indeed gcc define that according to target platform. My question is, why does the standard do that? I can see nothing that can come out of ambiguous type definition, except of hideous and hard to spot bugs. More than so, in ANSI C (before C99), the only byte-sized type is char, so using char for math is

convert unsigned char* to String

不问归期 提交于 2019-11-27 20:41:16
问题 I am little poor in type casting. I have a string in xmlChar* (which is unsigned char*), I want to convert this unsigned char to a std::string type. xmlChar* name = "Some data"; I tried my best to type cast , but I couldn't a way to convert it. 回答1: std::string sName(reinterpret_cast<char*>(name)); reinterpret_cast<char*>(name) casts from unsigned char* to char* in an unsafe way but that's the one which should be used here. Then you call the ordinary constructor of std::string . You could

Signed vs. unsigned integers for lengths/counts

喜你入骨 提交于 2019-11-27 20:07:39
For representing a length or count variable, is it better to use signed or unsigned integers? It seems to me that C++ STL tends to prefer unsigned ( std::size_t , like in std::vector::size() , instead C# BCL tends to prefer signed integers (like in ICollection.Count . Considering that a length or a count are non-negative integers, my intuition would choose unsigned ; but I fail to understand why the .NET designers chose signed integers. What is the best approach? What are the pros and cons of each one? C++ uses unsigned values because they need the full range. On a 32-bit system, the language

得到的奇技淫巧

岁酱吖の 提交于 2019-11-27 18:56:03
1.read()快读函数 1 //适用于正整数 2 template 3 inline void read(T &ret) { 4 char c; ret=0; 5 while((c=getchar())'9'); 6 while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar(); 7 } 8 //// 9 //适用于正负整数 10 template 11 inline bool scan_d(T &ret) { 12 char c; int sgn; 13 if(c=getchar(),c==EOF) return 0; //EOF 14 while(c!='-'&&(c'9')) c=getchar(); 15 sgn=(c=='-')?-1:1; 16 ret=(c=='-')?0:(c-'0'); 17 while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); 18 ret*=sgn; 19 return 1; 20 } 21 //// 22 //适用于正负数,(int,long long,float,double) 23 template 24 bool scan_d(T &ret){ 25 char c; int sgn; T bit=0.1; 26 if(c=getchar(

LCD编程_画点线圆

。_饼干妹妹 提交于 2019-11-27 17:57:41
上篇博客中进行了lcd的简单测试,这篇博客将进行更加复杂的测试——画点、画线、画圆。画线和画圆是在画点的基础上实现的,因此本篇博客重点实现画点操作。 先抛出这样的一个问题,已知: (x,y)的坐标; bpp; xres; yres; 那么,如何在framebuffer中获得像素的地址呢? (x,y)像素的起始地址 = fb_base +(xres * bpp /8)* y +x * bpp/8 (xres * bpp /8)表示一行占据多少个字节,乘以y表示y行共占据多少个像素 2)在framebuffer.c中实现画点操作 1 #include "lcd.h" 2 3 /* 实现画点 */ 4 5 /* 获得LCD参数 */ 6 static unsigned int fb_base; 7 static int xres, yres, bpp; 8 9 void fb_get_lcd_params(void) 10 { 11 get_lcd_params(&fb_base, &xres, &yres, &bpp); 12 } 13 14 /* rgb: 0x00RRGGBB */ 15 unsigned short convert32bppto16bpp(unsigned int rgb) 16 { 17 int r = (rgb >> 16)& 0xff; /*将红色的值拿到*

“strlen(s1) - strlen(s2)” is never less than zero

梦想的初衷 提交于 2019-11-27 17:25:49
I am currently writing a C program that requires frequent comparisons of string lengths so I wrote the following helper function: int strlonger(char *s1, char *s2) { return strlen(s1) - strlen(s2) > 0; } I have noticed that the function returns true even when s1 has shorter length than s2 . Can someone please explain this strange behavior? What you've come across is some peculiar behavior that arises in C when handling expressions that contain both signed and unsigned quantities. When an operation is performed where one operand is signed and the other is unsigned, C will implicitly convert the

Unsigned keyword in C++

僤鯓⒐⒋嵵緔 提交于 2019-11-27 17:25:43
Does the unsigned keyword default to a specific data type in C++? I am trying to write a function for a class for the prototype: unsigned Rotate(unsigned object, int count) But I don't really get what unsigned means. Shouldn't it be like unsigned int or something? futureelite7 From the link above: Several of these types can be modified using the keywords signed, unsigned, short, and long. When one of these type modifiers is used by itself, a data type of int is assumed This means that you can assume the author is using ints. Integer Types: short -> signed short signed short unsigned short int

How does an adder perform unsigned integer subtraction?

泪湿孤枕 提交于 2019-11-27 15:46:37
Suppose that A and B are signed positive integers, then for A-B , it's calculated using A+2 's complement of B . For example, in a 4-bit binary system, for signed integers, we have 7-3=0111-0011=0111+1101=(1)0100 , the 1 in the bracket is the carry bit. According to the overflow rule for signed integer, we know there is no overflow and the result is therefore correct. However, for unsigned integers, what will happen if we calculate 7-3 ? If we use the same way we mentioned above: 7-3=0111-0011=0111+1101=(1)0100 then, according to the overflow rule for unsigned integers, there is an overflow

【MySQL】数据类型之数字相关 -- 2019-08-17 02:50:19

荒凉一梦 提交于 2019-11-27 14:24:04
原文: http://blog.gqylpy.com/gqy/247 " 目录 #. 数值类型 1. 数值范围验证 /. 有符号 /. 无符号 2. int类型显示长度验证 #. 浮点型 1. 验证 /. 建表 /. 精度 #. 日期类型 1. 验证 /. year /. date、time、datatime /. timetamp /. datetime 与 timestamp 区别 /. 注意事项 详见链接 MySQL常用数据类型概括: 1. 数字: 整型: tinyint int bigint 小数: float: 在位数比较短的情况下不精确 double: 在位数比较长的情况下不精确(如:0.000001230123123123 存成:0.000001230000) decimal: 精确,内部原理是以字符串形式去存(如果是使用小数,则推荐使用此方法) 2. 字符串: char(0): 简单粗暴,浪费空间,存取速度快(root 存成 root000000) varchar: 精确,节省空间,存取速度慢 sql优化:创建表时,定长的类型往前放(比如性别),变长的往后放(比如地址或描述信息). >255个字符,超了就把文件路径放到数据库中,比如图片,视频等找一个文件服务器,数据库中只存放路径或url. 3. 事件类型: datatime: 最常用,获取当前日期事件 4.