What's the difference between (size_t)-1 and ~0? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhat is the difference between -1 and ~0What is the difference between #include <filename> and #include “filename”?Signed versus Unsigned IntegersWhy doesn't C have unsigned floats?What is the difference between const int*, const int * const, and int const *?size_t vs. uintptr_tDifference between malloc and calloc?Improve INSERT-per-second performance of SQLite?Difference between signed / unsigned charshould use size_t or ssize_tWhy is unsigned integer overflow defined behavior but signed integer overflow isn't?

Why does this iterative way of solving of equation work?

How to market an anarchic city as a tourism spot to people living in civilized areas?

Can the DM override racial traits?

Using "nakedly" instead of "with nothing on"

How to say that you spent the night with someone, you were only sleeping and nothing else?

How are presidential pardons supposed to be used?

Working around an AWS network ACL rule limit

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

What to do with post with dry rot?

Why can't wing-mounted spoilers be used to steepen approaches?

Was credit for the black hole image misattributed?

Why is "Captain Marvel" translated as male in Portugal?

Are my PIs rude or am I just being too sensitive?

How do I automatically answer y in bash script?

Can a 1st-level character have an ability score above 18?

How can I protect witches in combat who wear limited clothing?

How to rotate it perfectly?

Two different pronunciation of "понял"

Can the prologue be the backstory of your main character?

What did Darwin mean by 'squib' here?

How to add zeros to reach same number of decimal places in tables?

Mortgage adviser recommends a longer term than necessary combined with overpayments

How is simplicity better than precision and clarity in prose?

What kind of display is this?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat is the difference between -1 and ~0What is the difference between #include <filename> and #include “filename”?Signed versus Unsigned IntegersWhy doesn't C have unsigned floats?What is the difference between const int*, const int * const, and int const *?size_t vs. uintptr_tDifference between malloc and calloc?Improve INSERT-per-second performance of SQLite?Difference between signed / unsigned charshould use size_t or ssize_tWhy is unsigned integer overflow defined behavior but signed integer overflow isn't?



.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
    1 hour ago











  • Should I edit that?

    – JL2210
    1 hour 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
    1 hour 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
    1 hour ago











  • Should I edit that?

    – JL2210
    1 hour 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
    1 hour 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 1 hour ago









JL2210JL2210

618121




618121












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

    – Jonathan Leffler
    1 hour ago











  • Should I edit that?

    – JL2210
    1 hour 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
    1 hour ago


















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

    – Jonathan Leffler
    1 hour ago











  • Should I edit that?

    – JL2210
    1 hour 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
    1 hour ago

















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

– Jonathan Leffler
1 hour ago





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

– Jonathan Leffler
1 hour ago













Should I edit that?

– JL2210
1 hour ago





Should I edit that?

– JL2210
1 hour 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
1 hour 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
1 hour ago













2 Answers
2






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
    24 mins ago


















3














(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
    1 hour 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

























2 Answers
2






active

oldest

votes








2 Answers
2






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
    24 mins 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
    24 mins 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 1 hour ago

























answered 1 hour ago









chuxchux

85.3k874157




85.3k874157












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

    – Bhargav Rao
    24 mins ago

















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

    – Bhargav Rao
    24 mins ago
















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

– Bhargav Rao
24 mins ago





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

– Bhargav Rao
24 mins ago













3














(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
    1 hour ago















3














(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
    1 hour ago













3












3








3







(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 1 hour ago









Jonathan Leffler

575k956881041




575k956881041










answered 1 hour ago









Keith ThompsonKeith Thompson

195k26290484




195k26290484












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

    – JL2210
    1 hour ago

















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

    – JL2210
    1 hour ago
















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

– JL2210
1 hour ago





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

– JL2210
1 hour 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

Era Viking Índice Início da Era Viquingue | Cotidiano | Sociedade | Língua | Religião | A arte | As primeiras cidades | As viagens dos viquingues | Viquingues do Oeste e Leste | Fim da Era Viquingue | Fontes históricas | Referências Bibliografia | Ligações externas | Menu de navegação«Sverige då!»«Handel I vikingetid»«O que é Nórdico Antigo»Mito, magia e religião na volsunga saga Um olhar sobre a trajetória mítica do herói sigurd«Bonden var den verklige vikingen»«Vikingatiden»«Vikingatiden»«Vinland»«Guerreiras de Óðinn: As Valkyrjor na Mitologia Viking»1519-9053«Esculpindo símbolos e seres: A arte viking em pedras rúnicas»1679-9313Historia - Tema: VikingarnaAventura e Magia no Mundo das Sagas IslandesasEra Vikinge

What's the metal clinking sound at the end of credits in Avengers: Endgame?What makes Thanos so strong in Avengers: Endgame?Who is the character that appears at the end of Endgame?What happens to Mjolnir (Thor's hammer) at the end of Endgame?The People's Ages in Avengers: EndgameWhat did Nebula do in Avengers: Endgame?Messing with time in the Avengers: Endgame climaxAvengers: Endgame timelineWhat are the time-travel rules in Avengers Endgame?Why use this song in Avengers: Endgame Opening Logo Sequence?Peggy's age in Avengers Endgame

Are there legal definitions of ethnicities/races? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Legal definitions in the United StatesAre there truly legal limits on US interest rates?Are gender identity and sexual orientation federally protected?Why is there an apparent legal bias against digital services?What limits are there to the powers of individual judges in the United States legal system?Are women only scholarships legal under Irish / EU law?Is the term “race” defined by Public Law enacted by Congress of the United StatesIs there a legal definition of race in the US?Neighbors are spying for landlord on Renters is it legal?Are Protected Classes Bi-directional?