Right shifting by negative count is not only undefined, but maps from one-to-many in clang [duplicate]Undefined, unspecified and implementation-defined behaviorHow do I detect unsigned integer multiply overflow?Why doesn't C have unsigned floats?Fastest sort of fixed length 6 int arrayLeft shifting with a negative shift countModulo arithmetic with signed integers undefined behavior in c?Why is f(i = -1, i = -1) undefined behavior?What is gcc doing under a bitwise complement, and a negative left shift count when constrained by integer size?gcc and clang produce different outputs while left-shifting with unsigned valuesIs there a rule to spot out UB?Are there any 2-complement platform/compiler, where signed shift doesn't do the usual thing?

Python: next in for loop

Mathematical cryptic clues

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

What do you call a Matrix-like slowdown and camera movement effect?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Problem of parity - Can we draw a closed path made up of 20 line segments...

Can I ask the recruiters in my resume to put the reason why I am rejected?

Can I make popcorn with any corn?

Why doesn't H₄O²⁺ exist?

What are the differences between the usage of 'it' and 'they'?

Dragon forelimb placement

Test if tikzmark exists on same page

Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.

Is it important to consider tone, melody, and musical form while writing a song?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Why did Neo believe he could trust the machine when he asked for peace?

tikz: show 0 at the axis origin

Today is the Center

What would happen to a modern skyscraper if it rains micro blackholes?

Is it legal for company to use my work email to pretend I still work there?

How do I create uniquely male characters?

Minkowski space

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Is it unprofessional to ask if a job posting on GlassDoor is real?



Right shifting by negative count is not only undefined, but maps from one-to-many in clang [duplicate]


Undefined, unspecified and implementation-defined behaviorHow do I detect unsigned integer multiply overflow?Why doesn't C have unsigned floats?Fastest sort of fixed length 6 int arrayLeft shifting with a negative shift countModulo arithmetic with signed integers undefined behavior in c?Why is f(i = -1, i = -1) undefined behavior?What is gcc doing under a bitwise complement, and a negative left shift count when constrained by integer size?gcc and clang produce different outputs while left-shifting with unsigned valuesIs there a rule to spot out UB?Are there any 2-complement platform/compiler, where signed shift doesn't do the usual thing?






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








2
















This question already has an answer here:



  • Undefined, unspecified and implementation-defined behavior

    9 answers



When I compile this C code and run it a dozen times on my machine, I get a different 9 digit negative number each time. Another clang compiler on another machine yields 10 digit positive integers that are different. I am expecting a strange value because right shifting by a negative count is undefined, but I am surprised that the value is not a single, unique number. Instead, I am get multiple values with the same input. Why is this not a mathematical function?



#include <stdio.h>
int main(void)
printf("%d", 1 >> -1);
return 0;










share|improve this question













marked as duplicate by trentcl, Jonathan Leffler c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 4:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























    2
















    This question already has an answer here:



    • Undefined, unspecified and implementation-defined behavior

      9 answers



    When I compile this C code and run it a dozen times on my machine, I get a different 9 digit negative number each time. Another clang compiler on another machine yields 10 digit positive integers that are different. I am expecting a strange value because right shifting by a negative count is undefined, but I am surprised that the value is not a single, unique number. Instead, I am get multiple values with the same input. Why is this not a mathematical function?



    #include <stdio.h>
    int main(void)
    printf("%d", 1 >> -1);
    return 0;










    share|improve this question













    marked as duplicate by trentcl, Jonathan Leffler c
    Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 8 at 4:26


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      2












      2








      2









      This question already has an answer here:



      • Undefined, unspecified and implementation-defined behavior

        9 answers



      When I compile this C code and run it a dozen times on my machine, I get a different 9 digit negative number each time. Another clang compiler on another machine yields 10 digit positive integers that are different. I am expecting a strange value because right shifting by a negative count is undefined, but I am surprised that the value is not a single, unique number. Instead, I am get multiple values with the same input. Why is this not a mathematical function?



      #include <stdio.h>
      int main(void)
      printf("%d", 1 >> -1);
      return 0;










      share|improve this question















      This question already has an answer here:



      • Undefined, unspecified and implementation-defined behavior

        9 answers



      When I compile this C code and run it a dozen times on my machine, I get a different 9 digit negative number each time. Another clang compiler on another machine yields 10 digit positive integers that are different. I am expecting a strange value because right shifting by a negative count is undefined, but I am surprised that the value is not a single, unique number. Instead, I am get multiple values with the same input. Why is this not a mathematical function?



      #include <stdio.h>
      int main(void)
      printf("%d", 1 >> -1);
      return 0;





      This question already has an answer here:



      • Undefined, unspecified and implementation-defined behavior

        9 answers







      c one-to-many undefined-behavior






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 4:04









      EugeneEugene

      292




      292




      marked as duplicate by trentcl, Jonathan Leffler c
      Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 8 at 4:26


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by trentcl, Jonathan Leffler c
      Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 8 at 4:26


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes


















          5














          Because the behavior is not defined, the compiler (in a case of Clang, at least, see below) chooses not to put anything in the register where the argument would be passed to printf. This results in the register having whatever value was left there by the start-up code that prepares the C environment and calls main. This happens to be some address used during start-up, and it is randomized by the Address Space Layout Randomization used to impede attacks on software.



          Examination of the assembly generated by clang confirms no value is placed in the register, %esi, that would be used for this argument (using Apple LLVM 10.0.0 with clang-1000.11.45.5, building for its default target in macOS 10.14.3 with only the switch -O3).



          Other compilers may behave differently, of course, since the behavior is not defined by the C standard; this merely explains the observations reported by the OP in limited cases.






          share|improve this answer




















          • 1





            Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

            – Draconis
            Mar 8 at 4:20






          • 1





            @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

            – Bwebb
            Mar 8 at 4:21


















          2














          When you invoke undefined behavior, the compiler is allowed to do absolutely anything it wants. It's allowed to segfault, or print zero every time, or set your CPU on fire. Nothing really requires it to be deterministic or give the same result every time.



          In this case, Clang knows that undefined behavior happens, so it just stops caring. It doesn't bother to compile a right-shift instruction—because nothing obligates it to. It doesn't even bother to put any value in the register, and leaves whatever value happened to be in there beforehand, which is unpredictable and effectively random.






          share|improve this answer





























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            Because the behavior is not defined, the compiler (in a case of Clang, at least, see below) chooses not to put anything in the register where the argument would be passed to printf. This results in the register having whatever value was left there by the start-up code that prepares the C environment and calls main. This happens to be some address used during start-up, and it is randomized by the Address Space Layout Randomization used to impede attacks on software.



            Examination of the assembly generated by clang confirms no value is placed in the register, %esi, that would be used for this argument (using Apple LLVM 10.0.0 with clang-1000.11.45.5, building for its default target in macOS 10.14.3 with only the switch -O3).



            Other compilers may behave differently, of course, since the behavior is not defined by the C standard; this merely explains the observations reported by the OP in limited cases.






            share|improve this answer




















            • 1





              Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

              – Draconis
              Mar 8 at 4:20






            • 1





              @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

              – Bwebb
              Mar 8 at 4:21















            5














            Because the behavior is not defined, the compiler (in a case of Clang, at least, see below) chooses not to put anything in the register where the argument would be passed to printf. This results in the register having whatever value was left there by the start-up code that prepares the C environment and calls main. This happens to be some address used during start-up, and it is randomized by the Address Space Layout Randomization used to impede attacks on software.



            Examination of the assembly generated by clang confirms no value is placed in the register, %esi, that would be used for this argument (using Apple LLVM 10.0.0 with clang-1000.11.45.5, building for its default target in macOS 10.14.3 with only the switch -O3).



            Other compilers may behave differently, of course, since the behavior is not defined by the C standard; this merely explains the observations reported by the OP in limited cases.






            share|improve this answer




















            • 1





              Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

              – Draconis
              Mar 8 at 4:20






            • 1





              @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

              – Bwebb
              Mar 8 at 4:21













            5












            5








            5







            Because the behavior is not defined, the compiler (in a case of Clang, at least, see below) chooses not to put anything in the register where the argument would be passed to printf. This results in the register having whatever value was left there by the start-up code that prepares the C environment and calls main. This happens to be some address used during start-up, and it is randomized by the Address Space Layout Randomization used to impede attacks on software.



            Examination of the assembly generated by clang confirms no value is placed in the register, %esi, that would be used for this argument (using Apple LLVM 10.0.0 with clang-1000.11.45.5, building for its default target in macOS 10.14.3 with only the switch -O3).



            Other compilers may behave differently, of course, since the behavior is not defined by the C standard; this merely explains the observations reported by the OP in limited cases.






            share|improve this answer















            Because the behavior is not defined, the compiler (in a case of Clang, at least, see below) chooses not to put anything in the register where the argument would be passed to printf. This results in the register having whatever value was left there by the start-up code that prepares the C environment and calls main. This happens to be some address used during start-up, and it is randomized by the Address Space Layout Randomization used to impede attacks on software.



            Examination of the assembly generated by clang confirms no value is placed in the register, %esi, that would be used for this argument (using Apple LLVM 10.0.0 with clang-1000.11.45.5, building for its default target in macOS 10.14.3 with only the switch -O3).



            Other compilers may behave differently, of course, since the behavior is not defined by the C standard; this merely explains the observations reported by the OP in limited cases.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 8 at 4:18

























            answered Mar 8 at 4:12









            Eric PostpischilEric Postpischil

            80.3k890169




            80.3k890169







            • 1





              Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

              – Draconis
              Mar 8 at 4:20






            • 1





              @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

              – Bwebb
              Mar 8 at 4:21












            • 1





              Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

              – Draconis
              Mar 8 at 4:20






            • 1





              @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

              – Bwebb
              Mar 8 at 4:21







            1




            1





            Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

            – Draconis
            Mar 8 at 4:20





            Argh, finished examining the assembly output and came back to find your answer had gotten in first! +1 regardless, as this is entirely right.

            – Draconis
            Mar 8 at 4:20




            1




            1





            @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

            – Bwebb
            Mar 8 at 4:21





            @Draconis Ill thumbs up you bro, hate when that happens to me, also thumbed Eric.

            – Bwebb
            Mar 8 at 4:21













            2














            When you invoke undefined behavior, the compiler is allowed to do absolutely anything it wants. It's allowed to segfault, or print zero every time, or set your CPU on fire. Nothing really requires it to be deterministic or give the same result every time.



            In this case, Clang knows that undefined behavior happens, so it just stops caring. It doesn't bother to compile a right-shift instruction—because nothing obligates it to. It doesn't even bother to put any value in the register, and leaves whatever value happened to be in there beforehand, which is unpredictable and effectively random.






            share|improve this answer



























              2














              When you invoke undefined behavior, the compiler is allowed to do absolutely anything it wants. It's allowed to segfault, or print zero every time, or set your CPU on fire. Nothing really requires it to be deterministic or give the same result every time.



              In this case, Clang knows that undefined behavior happens, so it just stops caring. It doesn't bother to compile a right-shift instruction—because nothing obligates it to. It doesn't even bother to put any value in the register, and leaves whatever value happened to be in there beforehand, which is unpredictable and effectively random.






              share|improve this answer

























                2












                2








                2







                When you invoke undefined behavior, the compiler is allowed to do absolutely anything it wants. It's allowed to segfault, or print zero every time, or set your CPU on fire. Nothing really requires it to be deterministic or give the same result every time.



                In this case, Clang knows that undefined behavior happens, so it just stops caring. It doesn't bother to compile a right-shift instruction—because nothing obligates it to. It doesn't even bother to put any value in the register, and leaves whatever value happened to be in there beforehand, which is unpredictable and effectively random.






                share|improve this answer













                When you invoke undefined behavior, the compiler is allowed to do absolutely anything it wants. It's allowed to segfault, or print zero every time, or set your CPU on fire. Nothing really requires it to be deterministic or give the same result every time.



                In this case, Clang knows that undefined behavior happens, so it just stops caring. It doesn't bother to compile a right-shift instruction—because nothing obligates it to. It doesn't even bother to put any value in the register, and leaves whatever value happened to be in there beforehand, which is unpredictable and effectively random.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 8 at 4:19









                DraconisDraconis

                1,6391019




                1,6391019













                    Popular posts from this blog

                    Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                    Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                    Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved