Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I hate __int64!

I hate __int64!

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simon Steele
    wrote on last edited by
    #1

    Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this: #define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; } This doesn't work, nor does: return ((uint64_t)val * (uint64_t)1000); or return (uint64_t)((uint64_t)val * (uint64_t)1000); The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

    D L S 3 Replies Last reply
    0
    • S Simon Steele

      Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this: #define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; } This doesn't work, nor does: return ((uint64_t)val * (uint64_t)1000); or return (uint64_t)((uint64_t)val * (uint64_t)1000); The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      I get the desired results with:

      typedef unsigned __int64 uint64_t;

      unsigned long myval = 0xba5da800;

      uint64_t myfunction(unsigned long val)
      {
      return (uint64_t) val * (uint64_t) 0x38e;
      }

      void myotherfunction()
      {
      TRACE("%I64u\n", myfunction(myval));
      }

      1 Reply Last reply
      0
      • S Simon Steele

        Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this: #define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; } This doesn't work, nor does: return ((uint64_t)val * (uint64_t)1000); or return (uint64_t)((uint64_t)val * (uint64_t)1000); The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

        L Offline
        L Offline
        Len Holgate
        wrote on last edited by
        #3

        Check your tests? The second two should work fine. The first one fails because the values are multiplied as longs and then returned as a uint64. The multiplication as a long wraps. Casting one value to a uint64 will make the multiplication occur on uint64's and should give correct results. Your message mentions 0xba5da800 * 0x38E = 0x29678EB3000 at the top and your tests use 0xba5e3500 * 1000... The code below seeems to give the results you expect. typedef unsigned __int64 uint64_t; unsigned long myVal = 0xba5da800 ; uint64_t myfunction1(unsigned long val) { return val * 0x38E; } uint64_t myfunction2(unsigned long val) { return ((uint64_t)val * (uint64_t)0x38E); } uint64_t myfunction3(unsigned long val) { return (uint64_t)((uint64_t)val * (uint64_t)0x38E); } int main(int argc, char* argv[]) { uint64_t result1 = myfunction1(myVal); uint64_t result2 = myfunction2(myVal); uint64_t result3 = myfunction3(myVal); printf("myfunction1 - 0x%I64x\n", result1); printf("myfunction2 - 0x%I64x\n", result2); printf("myfunction3 - 0x%I64x\n", result3); return 0; } Len Holgate www.jetbyte.com The right code, right now.

        1 Reply Last reply
        0
        • S Simon Steele

          Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this: #define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; } This doesn't work, nor does: return ((uint64_t)val * (uint64_t)1000); or return (uint64_t)((uint64_t)val * (uint64_t)1000); The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

          S Offline
          S Offline
          Simon Steele
          wrote on last edited by
          #4

          Hi, thanks for saying that it works for you, it led me to the problem. The value being passed in was actually a long not an unsigned long, I didn't notice because it's actually part of a structure the definition of which has changed. "it does work, you fool!" posts are just as useful as solution advice so thanks to those who replied. I'll write a test harness next time before posting, my bad :) thanks again, simon. -- Simon Steele Programmers Notepad - http://www.pnotepad.org/

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups