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

  • SEARCH
  • Home
  • 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 5989567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:07:12+00:00 2026-05-22T23:07:12+00:00

Hey all, I think this might be a bug, but it’s killing me. I

  • 0

Hey all, I think this might be a bug, but it’s killing me. I am using MySQL 5.1.41 on an Ubuntu Linux server. I’m trying to write a function to create a random signed BIGINT value. Because the precision of RAND() is too small to generate the full range of possible BIGINT values, I decided to try to combine four 32-bit words using bit operators.

I fired up MySQL Workbench, and tried the following to see if bit shift operators work correctly with negative numbers:

SELECT HEX((0x1ACE - 0x8000) << 0x10);

0x1ACE – 0x8000 is -25906, so if I shift 16 bits to the left, I should be multiplying by 65536, right? The answer I got back was 0xFFFFFFFF9ACE0000, which is the signed representation of -1697775616, or -25906 * 65536. Wunderbar, it works!!!

So my plan was to use that to generate the first 32-bit word of the random signed BIGINT, and use a simple loop to add three more 32-bit words to the value, shifting the bits over four bytes at a time. Excitedly, I started out by putting the following code in my function, using a hard-coded value to test my plan with:

DECLARE x BIGINT;
SET x = (0x1ACE - 0x8000) << 0x10;

If I set the value so that the value being shifted is positive, everything works fine. However, after performing this calculation with a shifted negative value (-25906 in this case), I kept getting that x was 0x7FFFFFFFFFFFFFFF, which is the maximum positive value of a signed 64-bit integer. I’m totally baffled. The exact same operation is generating a totally different result depending on whether it is in a SET operation in a function or a SELECT statement.

So I started messing around with whether x was signed or unsigned and things got really weird. I tried making x unsigned and tried the following:

DECLARE x BIGINT UNSIGNED;
SET x = (0x1ACE - 0x8000);

When I did that, I got x equal to zero. Not surprising, since x is unsigned and the result is negative. However, on a lark, I tried this:

DECLARE x BIGINT UNSIGNED;
SET x = (0x1ACE - 0x8000) << 0;

To my surprise, x was set to 0xFFFFFFFFFFFF9ACE!

Can someone please help? I’ve been working for hours on a function that does nothing more than efficiently generates a random signed BIGINT, I’m tired, and the more I look at this stuff, the more frustrated I get and the less sense I can make of it. Any help, either in explaining what’s going on here or advice in writing this function so that it consistently works now and, if this is a bug, in later version if it gets fixed, would be greatly appreciated!

  • 1 1 Answer
  • 1 View
  • 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-22T23:07:13+00:00Added an answer on May 22, 2026 at 11:07 pm

    Okay, I think I just figured it out. According to the documentation, the bit shift operators result in unsigned 64-bit integers. So when you try:

    DECLARE guid BIGINT;   -- signed BIGINT
    SET guid = -25924;     -- = 0xFFFFFFFFFFFF9ABC
    SET guid = guid << 0;  -- Result: 0x7FFFFFFFFFFFFFFF
    

    What is happening is that in order for guid << 0 to return an unsigned integer, it is trying to convert guid from a negative signed integer to an unsigned integer, resulting in 0x7FFFFFFFFFFFFFFF, then shifting it over zero places, which is an identity operation resulting in the same 0x7FFFFFFFFFFFFFFF.

    It appears that multiplication (*), however, works correctly on signed and unsigned numbers. I can achieve my desired result by the following:

    DECLARE guid BIGINT;        -- signed BIGINT
    SET guid = -25924;          -- = 0xFFFFFFFFFFFF9ABC
    SET guid = guid * 0x10000;  -- = Result: 0xFFFFFFFF9ABC0000 Woot! \o/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all- I have looked this up on here and Google but none of
I'm probably going about this all wrong, but hey. I am rendering a large
Hey all i am trying to turn an A/V reciever on and off this
Hey all I'm trying to make a 3d game in with 2d sprite characters.
Hey all, I want to change a value in a table (in a mysql
Hey all. Basically, I need (this) in my function to correspond to this selection:
Hey all. I've got a new app up and running on a new ubuntu
Hey guys, first off all sorry, i can't login using my yahoo provider. anyways
Hey all Im working on a game for iphone using cocos2d and sdk 4.1
Hey, I think the best way to ask this question is to show what

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.