unsigned

Difference between signed and unsigned on bitwise operations

☆樱花仙子☆ 提交于 2019-11-26 17:08:06
问题 Is there any difference between signed and unsigned variables on bitwise operations? For example,when dealing with unsigned numbers: AND 00000111, 00001101 will result 00000101. But what would happen when dealing with signed numbers? 回答1: Assuming 2's complement is used for signed numbers, operations that care about signedness (ie they are different for the signed and unsigned interpretation of a bitstring) are: division modulo right shift comparisons (except equality) double-width

what happens when i mix signed and unsigned types ?

匆匆过客 提交于 2019-11-26 16:16:01
问题 I'm studying the C++ language and i have some doubt about type conversion, could you explain me what happens in an expression like this : unsigned int u = 10; int a = -42; std::cout << u - a << std::endl; Here i know that the result will be 52 if i apply the rules when we have two mathematical operators.But i wonder what happens when the compiler to convert a to an unsigned value creates a temporary of unsigned type, what happens after ? The expression now should be 10 -4294967254. 回答1: In

What is the best way to work around the fact that ALL Java bytes are signed?

依然范特西╮ 提交于 2019-11-26 15:31:07
问题 In Java, there is no such thing as an unsigned byte. Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign. What's a good way to work around this? (Saying don't use Java is not an option) 回答1: When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the

Difference between size_t and unsigned int?

孤者浪人 提交于 2019-11-26 15:07:30
问题 I am so confused about size_t . I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only non-negative values. My first question is: if it is used to represent only non-negative values, why don't we use unsigned int instead of size_t ? My second question is: are size_t and unsigned int interchangeable or not? If not, then why? And can anyone give me a good example of size_t and briefly its workings? 回答1: if it is use to represent non

Declaring an unsigned int in Java

烈酒焚心 提交于 2019-11-26 15:02:25
Is there a way to declare an unsigned int in Java? Or the question may be framed as this as well: What is the Java equivalent of unsigned? Just to tell you the context I was looking at Java's implementation of String.hashcode() . I wanted to test the possibility of collision if the integer were 32 unsigned int. Simeon Visser Java does not have a datatype for unsigned integers . You can define a long instead of an int if you need to store large values. You can also use a signed integer as if it were unsigned. The benefit of two's complement representation is that most operations (such as

c++ vector size. why -1 is greater than zero

本小妞迷上赌 提交于 2019-11-26 14:47:17
问题 Please take a look at this simple program: #include <iostream> #include <vector> using namespace std; int main() { vector<int> a; std::cout << "vector size " << a.size() << std::endl; int b = -1; if (b < a.size()) std::cout << "Less"; else std::cout << "Greater"; return 0; } I'm confused by the fact that it outputs "Greater" despite it's obvious that -1 is less than 0. I understand that size method returns unsigned value but comparison is still applied to -1 and 0. So what's going on? can

What happens when I assign a negative value to an unsigned int? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 13:45:49
问题 Possible Duplicate: signed to unsigned conversion in C - is it always safe? Let's say I declare a variable of type unsigned int : unsigned int x = -1; Now -1 in two's complement (assuming 32 bit machine) is 0xFFFFFFFF. Now when I assigned this value to x, did the value 0x7FFFFFFF get assigned to x? If it were so, then printf ("%d",x); would have printed the decimal equivalent of 0x7FFFFFFF, right? But, clearly this isn't happening, as the value that gets printed is -1. What am I missing here?

【CH0805】防线

筅森魡賤 提交于 2019-11-26 13:06:18
AcWing (我写这道题的时候CH已经萎掉了,所以只有AcWing的链接) 分析 由于这题数据范围很大,我们无法直接统计每个位置的防具数量,于是考虑二分,在每次二分的过程中统计二分范围内的防具数量,记为 \(cnt\) 。然后因为偶数+偶数=偶数,而奇数+偶数=奇数,我们可以利用这个性质来判断是否符合条件,于是 \(check\) 函数直接返回 \(cnt \ \text{&} \ 1\) 即可。 注意要用 \(unsigned\) 。 代码 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define il inline #define re register #define maxn 200005 #define tie0 cin.tie(0),cout.tie(0) #define fastio ios::sync_with_stdio(false) #define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) {

C reverse bits in unsigned integer

不羁的心 提交于 2019-11-26 12:46:48
问题 I\'m converting an unsigned integer to binary using bitwise operators, and currently do integer & 1 to check if bit is 1 or 0 and output, then right shift by 1 to divide by 2. However the bits are returned in the wrong order (reverse), so I thought to reverse the bits order in the integer before beginning. Is there a simple way to do this? Example: So if I\'m given the unsigned int 10 = 1010 while (x not eq 0) if (x & 1) output a \'1\' else output a \'0\' right shift x by 1 this returns 0101

I/O内存

荒凉一梦 提交于 2019-11-26 12:43:34
IO内存:当外部寄存器或内存映射到内存空间时,即使用访问内存的方式对外部寄存器或者内存进行读写操作。 比如配置外设控制器的寄存器,该寄存器是挂载在地址总线上的。 I/O内存分配和映射 ——以上内容来自《LDD3》 相关的accessor函数 IO内存读函数: unsigned int ioread8(void *addr); unsigned int ioread16(void *addr); unsigned int ioread32(void *addr); IO内存写函数: unsigned int iowrite8(u8 value, void *addr); unsigned int iowrite16(u8 value, void *addr); unsigned int iowrite32(u8 value, void *addr); 以下这些函数读和写一系列值到给定的I/O内存区域,从给定的buf读或写count个值到addr,参数count表示要读写的数据个数,而不是字节数 void ioread8_rep(void *addr, void *buf, unsigned long count); void ioread16_rep(void *addr, void *buf, unsigned long count); void ioread32_rep(void