unsigned

Why doesn't GCC produce a warning when assigning a signed literal to an unsigned type?

一笑奈何 提交于 2019-12-18 19:06:06
问题 Several questions on this website reveal pitfalls when mixing signed and unsigned types and most compilers seem to do a good job about generating warnings of this type. However, GCC doesn't seem to care when assigning a signed constant to an unsigned type! Consider the following program: /* foo.c */ #include <stdio.h> int main(void) { unsigned int x=20, y=-30; if (x > y) { printf("%d > %d\n", x, y); } else { printf("%d <= %d\n", x, y); } return 0; } Compilation with GCC 4.2.1 as below

使用无锁队列(环形缓冲区)注意事项

只谈情不闲聊 提交于 2019-12-18 12:33:59
环形缓冲区是生产者和消费者模型中常用的数据结构。生产者将数据放入数组的尾端,而消费者从数组的另一端移走数据,当达到数组的尾部时,生产者绕回到数组的头部。如果只有一个生产者和一个消费者,那么就可以做到免锁访问环形缓冲区(Ring Buffer)。写入索引只允许生产者访问并修改,只要写入者在更新索引之前将新的值保存到缓冲区中,则读者将始终看到一致的数据结构。同理,读取索引也只允许消费者访问并修改。 环形缓冲区实现原理图 如图所示,当读者和写者指针相等时,表明缓冲区是空的,而只要写入指针在读取指针后面时,表明缓冲区已满。 清单 9. 2.6.10 环形缓冲区实现代码 /* * __kfifo_put - puts some data into the FIFO, no locking version * Note that with only one concurrent reader and one concurrent * writer, you don't need extra locking to use these functions. */ unsigned int __kfifo_put(struct kfifo *fifo, unsigned char *buffer, unsigned int len) { unsigned int l; len = min(len,

printf format for unsigned __int64 on Windows

旧巷老猫 提交于 2019-12-18 11:44:56
问题 I need to print a ULONGLONG value ( unsigned __int64 ). What format should i use in printf ? I found %llu in another question but they say it is for linux only. Thanks for your help. 回答1: Using Google to search for “Visual Studio printf unsigned __int64” produces this page as the first result, which says you can use the prefix I64 , so the format specifier would be %I64u . 回答2: %llu is the standard way to print unsigned long long , it's not just for Linux, it's actually in C99. So the problem

Is wchar_t just a typedef of unsigned short?

荒凉一梦 提交于 2019-12-18 07:29:33
问题 for example, does: wchar_t x; translate to: unsigned short x; 回答1: In short: in C may be in C++ no. Widely. C defines wchar_t as typedef but in Unix it is generally 4 bytes (so generally not short) and in Windows 2 so it may be short. Under C++ it is unique built-in type like char or int , so you can legally overload void foo(short x) and void foo(wchar_t x) 回答2: For anyone else who may come across this answer because function calls in your Visual Studio project won't link, despite both

unsigned becomes signed in if-statement comparisons?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:52:42
问题 I have searched this site for an answer and found many responses to unsigned/signed comparison but this problem is that only unsigned parameters are compared but still it works funny. The problem with the following code is that the first if -statment does not happen ("hello") where as the second ("world") does. This I have interpreted as the calculation that is done inside the if -statment generates a negative number but the exact same calculation done with the result saved to a variables

Convert unsigned byte to signed byte

核能气质少年 提交于 2019-12-18 04:44:13
问题 Is there an easy and elegant way to convert an unsigned byte value to a signed byte value in java? For example, if all I have is the int value 240 (in binary (24 bits + 11110000) = 32bits), how can I get the signed value for this int? 回答1: Java does not have unsigned values, except for char . Consider this snippet: byte val = (byte)255; System.out.println(String.valueOf(val)); The result will be -1, because the lowest 8 bits got copied over to the byte variable. 回答2: In Java all the primitive

How to cast or convert an unsigned int to int in C?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:55:26
问题 My apologies if the question seems weird. I'm debugging my code and this seems to be the problem, but I'm not sure. Thanks! 回答1: It depends on what you want the behaviour to be. An int cannot hold many of the values that an unsigned int can. You can cast as usual: int signedInt = (int) myUnsigned; but this will cause problems if the unsigned value is past the max int can hold. This means half of the possible unsigned values will result in erroneous behaviour unless you specifically watch out

Unsigned and Signed Values in C (Output)

情到浓时终转凉″ 提交于 2019-12-18 01:05:28
问题 signed int x = -5; unsigned int y = x; What is the value of y ? How is this so? 回答1: It depends on the maximum value of the unsigned int . Typically, a unsigned int is 32-bit long, so the UINT_MAX is 2 32 − 1. The C standard (§6.3.1.3/2) requires a signed → unsigned conversion be performed as Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range

最大的余数题解

老子叫甜甜 提交于 2019-12-17 17:42:02
其实本题的难度真心不高,但是可以完整说明数据分析、标程、随机数生成、对拍等部分。 题目链接 原题来自计蒜客的某次比赛。计蒜客对应的链接为 https://nanti.jisuanke.com/t/42227 。 或者我自己OJ的链接为 http://47.110.135.197/problem.php?id=5150 。 题面 给定一个正整数 n,请找出一个不大于 n 的正整数 p,使得 n 除以 p 的余数最大,并求出这个最大的余数。 输入 只有一行,包含一个正整数 n。 输出 只有一行,包含你的答案。 样例输入 5 样例输出 2 数据范围 一共 20 个测试数据 对于前 30% 的数据,1 ≤ n ≤10。 对于前 60% 的数据,1 ≤ n ≤10^6。 对于前 90% 的数据,1 ≤ n ≤ 10^18。 对于前 100% 的数据,1 ≤ n ≤ 10^1000。 题目分析 解题思路 求余数,而且要求余数最大。哪么必然意味着除数要最小,哪么除数必然为 1。也就是说,这题就是一个 2 的余数问题。进一步分析,我们可以知道如果被除数为奇数,哪么这个最大的余数为 n/2;如果被除数为偶数,哪么这个最大的余数为 n/2 - 1。等效于(n-1)/2。 数据范围分析 30%的数据,1 ≤ n ≤10。可以用int可以表示 n。 60% 的数据,1 ≤ n ≤10^6

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

痞子三分冷 提交于 2019-12-17 16:44:11
问题 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