npos : Unsigned but negative ?

npos is a value of type size_t which is of type unsigned int.

but since npost is unsigned how can it bears the value of -1 ?

Plz help...thank you !
It can't. npos have the maximum value that the type can hold.
-1 and 4294967297 are the same for 32-bit integers.
Here is an example of a 1 bit integer. On many compilers you can call this a char:

0000 0000.

Now the bit furthest to the left is what is most interesting to us. In an unsigned char this represents the sign bit. In a signed char this is the most significant bit.

Example:
1000 0011.

As an unsigned char this is equal to 0x83 (in hex) (131 in dec)
As a signed char this is equal to - 0x03 (in hex) (-3 in dec)

The "unsigned" therefore doesn't change the bits themselves, just how the compiler sees them. You can cast it however you want.
Stewbond wrote:
[...] a 1 bit integer. On many compilers you can call this a char
I think you mean 1 byte? ;)
Of course it's 1 byte.

But this is 8-bit windows right ?

ABout 32-bits,I knew that 1 is for the sign,7 is for the mantissa and the rest for the factorial part.
ABout 32-bits,I knew that 1 is for the sign,7 is for the mantissa and the rest for the factorial part.


I'm pretty sure that's only for floating point types like float or double.
For unsigned integers, all the bits go to the number.
For signed integers, all the bits except the first which is the sign.
(Just like Stewbond explained.)
Actually, they're stored in two's compliment. If an entire bit were wasted for the sign, we could not have -128 in signed chars.
If you subtract 1 from an unsigned integer at zero, it "wraps round" and takes on the highest possible value. So a convenient way to set an unsigned integer to its highest value is to set it to -1.
Of course, you need to cast it to do so. I prefer to just set it to 0 first and then do --var; as this eliminates the need to cast. ;)
@ hentaiw: is this it?
1
2
3
size_t sz = -1; // actually gets huge unsigned value

printf("%d", sz); // oops, should've been "%u" 

Oh nice ! I didn't know that. How unsigned is implemented anyway ? int and unsigned int have the same capacity , but how can unsigned double the range and not includes the negative part.

You may give me the articles,not need to explain.But the articles plz be easy.
how can unsigned double the range and not includes the negative part
You answered your own question, there. You see, 'signed' means it has a sign,positive or negative, and 'unsigned' means it doesn't, so it is assumed to be positive. Signed types take the upper half of the positive range and use it instead for the negative range. Unsigned types are the originals, not the other way around.
Because unsigned don't use the first byte as sign indicator,but for number indicator like other 7 ?

But still didn't know how they can assign a negative to an unsigned.
You can't. I guess when you do this:
unsigned x = -1;
the compiler must implicitly see this:
unsigned x = unsigned(-1);
which, of course, works.
unsigned(-1);

But that ain't the same thing ?? and unsigned with a negative value ??!?!?!?
No, when you cast it it becomes unsigned. You need to understand wrap-around.
Because iomanip's setbase(2) doesn't seem to work, I wrote this horrible thing.
Don't use on pointers, don't use on constants... should work on objects.
This is also C++11 code, mostly. Flames welcome as long as they're also instructive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <map>
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>

bool test_little_endian()
{
    uint16_t u = 0xABCD;

    if (reinterpret_cast<char *> (&u)[0] == '\xCD' &&
        reinterpret_cast<char *> (&u)[1] == '\xAB')
        return true;

    return false;
}

void show_binary(const char *pc, size_t sz)
{
    std::map<char, std::string> nibbles {
        {'\x0', "0000"},
        {'\x1', "0001"},
        {'\x2', "0010"},
        {'\x3', "0011"},
        {'\x4', "0100"},
        {'\x5', "0101"},
        {'\x6', "0110"},
        {'\x7', "0111"},
        {'\x8', "1000"},
        {'\x9', "1001"},
        {'\xA', "1010"},
        {'\xB', "1011"},
        {'\xC', "1100"},
        {'\xD', "1101"},
        {'\xE', "1110"},
        {'\xF', "1111"},
    };

    while (sz--)
    {
        std::cout << nibbles[(*pc & 0xF0) >> 4] << nibbles[*pc & 0xF] << ' ';
        ++pc;
    }

    std::cout << std::endl;
}

#define SHOW_BINARY(v) \
    do { \
        std::cout << v << '\t'; \
        show_binary(reinterpret_cast<const char *> (&v), sizeof v); \
    } while (0)

int main()
{
    int i1   = -3;
    int i2   = -1;
    char c1  = 'Y';
    char c2  = '\x70';
    char a[] = "Hello";
    float f  = 1000.43f;

    if (test_little_endian())
        std::cout << "You seem to be using a Little Endian machine, so bytes\n"
            " will appear as in reversed order (read bytes from right to left).\n" << std::endl;

    SHOW_BINARY(i1);
    SHOW_BINARY(i2);
    SHOW_BINARY(c1);
    SHOW_BINARY(c2);
    SHOW_BINARY(a);
    SHOW_BINARY(f);

    system("PAUSE"); // you may remove this if you're elite
    return EXIT_SUCCESS;
}
The little endian test is useful for allowing compatibility between files saved on different machines, but what does this have to do with this thread?
test_little_endian() isn't the important part... but on Little Endian machines, the bytes will appear in reversed order.

I hope the program helps OP understand how numbers and other data are stored in memory.
I myself was wrong about how signed integers are stored in modern computers.

http://en.wikipedia.org/wiki/Signed_number_representations
Topic archived. No new replies allowed.