Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:09:56+00:00 2026-05-18T09:09:56+00:00

I have problem when try to shift bit for a negative number with Perl.

  • 0

I have problem when try to shift bit for a negative number with Perl.
For example:

printf("%d", (-10) >> 2);

This method print: 1073741821

With same condition, when I try to do it with Javascript:

document.write('test: ' + (-10 >> 2) + '<br>');

The output is -3

I would like to understand the different between Perl and Javascripts. Does Perl have problem with shifting bit for a negative number?

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:09:56+00:00Added an answer on May 18, 2026 at 9:09 am

    Perl uses unsigned binary right shift. If Perl runs on a 32-bit architecture, you get the following:

    -10 == -0xa == 0xfffffff6
    0xfffffff6 >> 2 == 0x3ffffffd == 1073741821
    

    Unsigned binary right shift means that the highest (rigtmost) bits of the result become 0.

    JavaScript uses sign-extending binary right shift. JavaScript shifts always treat both the input and the output as 32 bit integers. So you get:

    -10 == -0xa == 0xfffffff6
    -3 == -0x3 == 0xfffffffd
    0xfffffff6 >> 2 == 0xfffffffd == -3
    

    Sign-extending binary right shift means that the highest (rigtmost) bits of the result get copied from the highest bit of the original value.

    As Tim Henigan said in his answer, it’s possible to get sign-extending binary right shift from Perl, by specifying use integer;. Example:

    { use integer;
      printf "%d 0x%x\n", (-10) >> 2, (-10) >> 2;  #: -3 0xfffffffd
    }
    printf "%d 0x%x\n", (-10) >> 2, (-10) >> 2;  #: 1073741821 0x3ffffffd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been puzzling over a problem this morning with LinqToSQL. I'll try and
I have this problem: When I try to implement Ayende's complex searching found at:
guys! I have problem with my .net application. When I try to print in
I have problem when I try insert some data to Informix TEXT column via
I have a very strange problem, when I try to var_dump (or print_r )
I'll try to explain my problem the best I can, here goes: I have
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have been trying to register 3 hotkeys. I followed this example (or this
we have a funny problem with try catch and std::runtime_error. Can someone explain to
i have problem with maven. When i try to compile test classes ( mvn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.