Questions tagged [unsigned]

An unsigned variable is a variable that can only represent non-negative numbers.

An unsigned variable is a variable that can only represent non-negative numbers, as opposed to a signed variable which can represent both positive and negative numbers.

1251 questions
20
votes
2 answers

Does the C++ standard require the maximum of unsigned integer numbers to be of the form 2^N-1?

For T so that std::is_integral::value && std::is_unsigned::value is true, does the C++ standard guarantee that : std::numeric_limits::max() == 2^(std::numeric_limits::digits)-1 in the mathematical sense? I am looking for a proof of that…
Vincent
  • 57,703
  • 61
  • 205
  • 388
20
votes
7 answers

How can I invert bits of an unsigned byte in Java?

I am trying to write a decoder for a very simple type of encryption. Numbers from 0-255 are entered via Scanner, the bits are inverted, and then converted to a character and printed. For example, the number 178 should convert to the letter "M".…
DavidKelly999
  • 301
  • 1
  • 2
  • 5
20
votes
4 answers

Why is −1 > sizeof(int)?

Consider the following code: template class StaticAssert; template<> class StaticAssert {}; StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error StaticAssert< (-1 > sizeof(int)) > xyz2; // OK Why is -1 > sizeof(int) true? Is it…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
20
votes
2 answers

Get the signed/unsigned variant of an integer template parameter without explicit traits

I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T, and the other as the unsigned variant of type T -- i.e. if T == int, then T_Unsigned == unsigned int.…
Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
20
votes
6 answers

Is wchar_t just a typedef of unsigned short?

for example, does: wchar_t x; translate to: unsigned short x;
Marlon
  • 19,924
  • 12
  • 70
  • 101
20
votes
3 answers

C++: Compiler warning for large unsigned int

I have following array, that I need to operate by hand on bitmaps. const unsigned int BITS[32] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, …
gruszczy
  • 40,948
  • 31
  • 128
  • 181
19
votes
4 answers

What is happening in "? :"? I have no idea about the return type

I think ((1 ? (int)1 : (unsigned int)2) > -1) results in 1 (true), but actually it is 0 (false) in Visual Studio 2017. I think the value of (1 ? (int)1 : (unsigned int)2) should be (int)1, because 1 ? is true, and 1 > -1 would be true. I don't know…
BellSnow
  • 209
  • 1
  • 5
19
votes
4 answers

C# equivalent of 64-bit unsigned long long in C++

I am building a DLL which will be used by C++ using COM. Please let me know what would be the C# equivalent of C++ 64-bit unsigned long long. Will it be ulong type in C# ? Please confirm. Thanks, Gagan
Gags
  • 827
  • 2
  • 13
  • 29
18
votes
7 answers

Proper Way To Initialize Unsigned Char*

What is the proper way to initialize unsigned char*? I am currently doing this: unsigned char* tempBuffer; tempBuffer = ""; Or should I be using memset(tempBuffer, 0, sizeof(tempBuffer)); ?
Brian
  • 249
  • 1
  • 3
  • 9
18
votes
3 answers

How computer distinguish an integer is signed or unsigned?

Two's complements is set to make it easier for computer to compute the substraction of two numbers. But how computer distinguish an integer is signed integer or unsigned integer? It's just 0 and 1 in its memory. For exmaple, 1111 1111 in the…
viperchaos
  • 395
  • 1
  • 4
  • 15
17
votes
1 answer

Is this a bug in std::gcd?

I've come across this behavior of std::gcd that I found unexpected: #include #include int main() { int a = -120; unsigned b = 10; //both a and b are representable in type C using C =…
perivesta
  • 3,417
  • 1
  • 10
  • 25
17
votes
2 answers

How do I print a short as an unsigned short in Java

I have an array of short whose values range between 0 and the maximum value of a short. I scale the data (to display it as TYPE_USHORT) so that the resulting short values range between 0 and 65535. I need to print some of the scaled values but…
Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34
17
votes
3 answers

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

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…
maerics
  • 151,642
  • 46
  • 269
  • 291
16
votes
3 answers

Why does std::abs return signed types

I'm getting warning for signed vs. unsigned comparison when I'm comparing a std::abs(int) against an unsigned. And indeed, std::abs returns signed values. Why was that choice made? It would have solved the problem of negative values whose…
akim
  • 8,255
  • 3
  • 44
  • 60
15
votes
3 answers

When should I use std_logic_vector and when should I use other data types?

I'm new to VHDL and am having trouble figuring out which data types are appropriate to use where. If I understand correctly, for synthesis, all top level entity ports should be declared either std_logic_vector or std_logic and never any other…
Emil Eriksson
  • 2,110
  • 1
  • 21
  • 31