What's the difference between (size_t)-1 and ~0?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















I've seen both (size_t)-1 and ~0 used to represent large numbers, or numbers with all their bits flipped.



Is there any difference between the two? If so, what is it?



I found this question: What is the difference between -1 and ~0, however it did not answer my question because I'm dealing with unsigned integers (such as size_t), as opposed to signed integers (such as int).










share|improve this question























  • Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

    – Jonathan Leffler
    3 hours ago











  • Should I edit that?

    – JL2210
    3 hours ago






  • 3





    Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

    – Jonathan Leffler
    3 hours ago




















6















I've seen both (size_t)-1 and ~0 used to represent large numbers, or numbers with all their bits flipped.



Is there any difference between the two? If so, what is it?



I found this question: What is the difference between -1 and ~0, however it did not answer my question because I'm dealing with unsigned integers (such as size_t), as opposed to signed integers (such as int).










share|improve this question























  • Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

    – Jonathan Leffler
    3 hours ago











  • Should I edit that?

    – JL2210
    3 hours ago






  • 3





    Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

    – Jonathan Leffler
    3 hours ago
















6












6








6








I've seen both (size_t)-1 and ~0 used to represent large numbers, or numbers with all their bits flipped.



Is there any difference between the two? If so, what is it?



I found this question: What is the difference between -1 and ~0, however it did not answer my question because I'm dealing with unsigned integers (such as size_t), as opposed to signed integers (such as int).










share|improve this question














I've seen both (size_t)-1 and ~0 used to represent large numbers, or numbers with all their bits flipped.



Is there any difference between the two? If so, what is it?



I found this question: What is the difference between -1 and ~0, however it did not answer my question because I'm dealing with unsigned integers (such as size_t), as opposed to signed integers (such as int).







c unsigned ones-complement






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









JL2210JL2210

622121




622121













  • Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

    – Jonathan Leffler
    3 hours ago











  • Should I edit that?

    – JL2210
    3 hours ago






  • 3





    Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

    – Jonathan Leffler
    3 hours ago





















  • Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

    – Jonathan Leffler
    3 hours ago











  • Should I edit that?

    – JL2210
    3 hours ago






  • 3





    Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

    – Jonathan Leffler
    3 hours ago



















Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

– Jonathan Leffler
3 hours ago





Note that ~0 is a signed quantity — you'd need ~0U to make it unsigned.

– Jonathan Leffler
3 hours ago













Should I edit that?

– JL2210
3 hours ago





Should I edit that?

– JL2210
3 hours ago




3




3





Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

– Jonathan Leffler
3 hours ago







Since you've got some answers which addresses ~0 rather than ~0U, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers.

– Jonathan Leffler
3 hours ago














3 Answers
3






active

oldest

votes


















5















What's the difference between (size_t)-1 and ~0?




Type and value differ.



(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.



~0 is often -1 and has the type of int.





Assigning both of those to a size_t will result in SIZE_MAX.



size_t a = (size_t)-1; 
size_t b = ~0;


In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Bhargav Rao
    2 hours ago



















4














(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).



~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).



Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.



If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.






share|improve this answer


























  • Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

    – JL2210
    3 hours ago











  • Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

    – TLW
    23 mins ago



















0














Note that the previous answers assume a 2s complement machine (very likely to be the case these days, but not guaranteed).



If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1s complement machine then -1 would have all bits but the LSB set.



In all of these cases (including the common 2s complement machine) ~0 has all bits set.





share
























  • Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

    – JL2210
    1 hour ago








  • 2





    @JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

    – TLW
    25 mins ago











  • Ah. I understand now. I'll upvote as soon as this is edited.

    – JL2210
    4 mins ago












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55678835%2fwhats-the-difference-between-size-t-1-and-0%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









5















What's the difference between (size_t)-1 and ~0?




Type and value differ.



(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.



~0 is often -1 and has the type of int.





Assigning both of those to a size_t will result in SIZE_MAX.



size_t a = (size_t)-1; 
size_t b = ~0;


In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Bhargav Rao
    2 hours ago
















5















What's the difference between (size_t)-1 and ~0?




Type and value differ.



(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.



~0 is often -1 and has the type of int.





Assigning both of those to a size_t will result in SIZE_MAX.



size_t a = (size_t)-1; 
size_t b = ~0;


In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.






share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Bhargav Rao
    2 hours ago














5












5








5








What's the difference between (size_t)-1 and ~0?




Type and value differ.



(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.



~0 is often -1 and has the type of int.





Assigning both of those to a size_t will result in SIZE_MAX.



size_t a = (size_t)-1; 
size_t b = ~0;


In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.






share|improve this answer
















What's the difference between (size_t)-1 and ~0?




Type and value differ.



(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.



~0 is often -1 and has the type of int.





Assigning both of those to a size_t will result in SIZE_MAX.



size_t a = (size_t)-1; 
size_t b = ~0;


In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 3 hours ago









chuxchux

85.3k874157




85.3k874157













  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Bhargav Rao
    2 hours ago



















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Bhargav Rao
    2 hours ago

















Comments are not for extended discussion; this conversation has been moved to chat.

– Bhargav Rao
2 hours ago





Comments are not for extended discussion; this conversation has been moved to chat.

– Bhargav Rao
2 hours ago













4














(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).



~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).



Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.



If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.






share|improve this answer


























  • Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

    – JL2210
    3 hours ago











  • Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

    – TLW
    23 mins ago
















4














(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).



~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).



Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.



If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.






share|improve this answer


























  • Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

    – JL2210
    3 hours ago











  • Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

    – TLW
    23 mins ago














4












4








4







(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).



~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).



Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.



If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.






share|improve this answer















(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).



~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).



Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.



If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.







share|improve this answer














share|improve this answer



share|improve this answer








edited 3 hours ago









Jonathan Leffler

575k956881041




575k956881041










answered 3 hours ago









Keith ThompsonKeith Thompson

195k26290484




195k26290484













  • Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

    – JL2210
    3 hours ago











  • Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

    – TLW
    23 mins ago



















  • Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

    – JL2210
    3 hours ago











  • Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

    – TLW
    23 mins ago

















Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

– JL2210
3 hours ago





Thanks for letting me know. I'll remember to define SIZE_MAX in my stdint.h implementation.

– JL2210
3 hours ago













Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

– TLW
23 mins ago





Re; ~0 versus -1 - some compilers warn on implicit wrapping in constant expressions.

– TLW
23 mins ago











0














Note that the previous answers assume a 2s complement machine (very likely to be the case these days, but not guaranteed).



If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1s complement machine then -1 would have all bits but the LSB set.



In all of these cases (including the common 2s complement machine) ~0 has all bits set.





share
























  • Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

    – JL2210
    1 hour ago








  • 2





    @JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

    – TLW
    25 mins ago











  • Ah. I understand now. I'll upvote as soon as this is edited.

    – JL2210
    4 mins ago
















0














Note that the previous answers assume a 2s complement machine (very likely to be the case these days, but not guaranteed).



If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1s complement machine then -1 would have all bits but the LSB set.



In all of these cases (including the common 2s complement machine) ~0 has all bits set.





share
























  • Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

    – JL2210
    1 hour ago








  • 2





    @JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

    – TLW
    25 mins ago











  • Ah. I understand now. I'll upvote as soon as this is edited.

    – JL2210
    4 mins ago














0












0








0







Note that the previous answers assume a 2s complement machine (very likely to be the case these days, but not guaranteed).



If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1s complement machine then -1 would have all bits but the LSB set.



In all of these cases (including the common 2s complement machine) ~0 has all bits set.





share













Note that the previous answers assume a 2s complement machine (very likely to be the case these days, but not guaranteed).



If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1s complement machine then -1 would have all bits but the LSB set.



In all of these cases (including the common 2s complement machine) ~0 has all bits set.






share











share


share










answered 1 hour ago









SoronelHaetirSoronelHaetir

7,1711514




7,1711514













  • Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

    – JL2210
    1 hour ago








  • 2





    @JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

    – TLW
    25 mins ago











  • Ah. I understand now. I'll upvote as soon as this is edited.

    – JL2210
    4 mins ago



















  • Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

    – JL2210
    1 hour ago








  • 2





    @JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

    – TLW
    25 mins ago











  • Ah. I understand now. I'll upvote as soon as this is edited.

    – JL2210
    4 mins ago

















Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

– JL2210
1 hour ago







Note that in the question I'm asking about unsigned integers, so there's no such thing as a "sign bit".

– JL2210
1 hour ago






2




2





@JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

– TLW
25 mins ago





@JL2210 - note that the intermediate values of both ~0 and -1 are signed, not unsigned.

– TLW
25 mins ago













Ah. I understand now. I'll upvote as soon as this is edited.

– JL2210
4 mins ago





Ah. I understand now. I'll upvote as soon as this is edited.

– JL2210
4 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55678835%2fwhats-the-difference-between-size-t-1-and-0%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Callistus I

Tabula Rosettana

How to label and detect the document text images