FYI about wrapping 32-bit integers

This is a note for newbies, however, I have a question that came up writing this post that I'd appreciate any experts addressing. Please see the bottom of the post, "Code" Question.
I am not talking about wrapping a class around a primitive. I am talking about the low level issue of what happens internally to a 32-bit integer value as the bits wrap around either limit of the data type.
This is a FYI for noobs like me! Consider the following code:long bigValue = 53*52*51*50*49*48;
System.out.println(bigValue);Looks ok to us noobs, right? But the output this produces is -650,483,584 (I added the commas). When I did this I was puzzled for a bit, then I remembered that when you add to a value an amount that exceeds the upper limit of that type (subtract past the lower limit) the value can wrap. For example, a Java byte - an 8-bit type - has a range of -128 to 127. If I have a variable of type byte equal to 127 and add 1 to it, the result is -128! The bits inside wrapped around.
In my code sample, what is salient is that in Java an integer literal is always assumed to be an int - a 32-bit integer - which has a range of:-
-2,147,483,648 to 2,147,483,647. The resulting multiplication I was doing equals 16,529,000,000 which is out of range for an int, so it wrapped and gave me the negative value.
It didn't matter that bigValue was of type long. Java was looking first at the operations which contains integer literals; therefore it internally used an int to perform the multiplication, which caused the bit-wrapping. Fortunately, you can force Java to use a long value all the way through this computation by forcing the first literal to represent a long (a 64-bit value). Example:
long bigValue = 53L*52*51*50*49*48;
System.out.println(bigValue);
Notice the capital "L" after "53". That forced Java to read "53" as a long, and thereafter it continued to perform the multiplication steps on a long instead of an int, finally yielding the correct value to assign to bigValue.
...Just thought other newbies would appreciate the heads-up.
"Code" Question
When trying to write the second code snippet, the part "53L" would look like "53" in the preview. I could not figure out how to "escape" the "L" so it would show up. Anyone know how to do that?

kajbj and vanilla_lorax...
Thanks for the explanation and "fix".
Have y'all had trouble with the B,
I,
U, and code buttons? Every time I
click on them I get an "Error on page" in the leftof
my browser's status bar....Try clearing your browser's cached files. It might
help.Nope, didn't help.

Similar Messages

  • DNG SDK thinks 0xFFFFFFFF is invalid white level for 32 bit integers

    As discussed in another message the DNG SDK does not seem to be able to write floating point DNGs. Since I need higher dynamic range than a 16 bit sample I instead went for 32 bit integers.
    This works, and I manage to create a file but when I run dng_validate on it it says the white level is invalid. The white level I have of course set to 0xFFFFFFFF to make use of the full 32 bit range. Looking into the code in dng_ifd.cpp where this test is made it seems like the default max white level is set to 65535.0 and the file's white level is compared against that, regardless if the sample type is 16 or 32 bit. This means that I can only make use of 16 bits of the 32 bit integer which seems kind of strange. Looking into the DNG spec I don't see anything there that forbids using the full 32 bit range of 32 bit samples. So this looks like a bug to me.
    This is with version 1.4
    the created file can be opened in Lightroom 4, so the only problem seems to be that dng_validate does not think its valid.
    Message was edited by: torger76, removed clipping issue, that was a fault in my code.

    Hello Charles,
    I would be concerned too if my MacBook Prowas running slowly.  I found a couple of articles I recommend to help isolate and troubleshoot this issue.
    I recommend reviewing this article first for possible causes of the slowness:
    OS X Mavericks: If your Mac runs slowly
    http://support.apple.com/kb/PH13895
    You can further isolate the issue by determining if it is only happening in your user account or if it is happening system-wide:
    Isolating an issue by using another user account
    http://support.apple.com/kb/TS4053
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

  • OPC and 16 bit integers

    Hi everybody,
    I have a very time consuming problem with opc programming in labview: I need
    to write 16 bit integers with the labview opc client to the server - and I
    don't know how to do this because of the limitation of the labview Vi's to
    32 bit integers.
    Any suggestion is very welcome.
    Bernd Szyszka

    What is the limitation? LabVIEW has 8,16, and 32 bit integers - both signed
    and unsigned.
    "Dr. Bernd Szyszka" wrote:
    >Hi everybody,>>I have a very time consuming problem with opc programming
    in labview: I need>to write 16 bit integers with the labview opc client to
    the server - and I>don't know how to do this because of the limitation of
    the labview Vi's to>32 bit integers.>>Any suggestion is very welcome.>>Bernd
    Szyszka>>

  • Using 32 bit integers

    I need to use a 32bit integer in my app, but Java Card only supports 16 bit integers. What's the best way of handling this? I've been trying to do it with a 4 byte array but I'm having problems add/subtracting. I've made the following function to adding two byte array integers together
         public static byte [] intAdd(byte [] firstInt, byte[] secondInt) {
              if (firstInt.length != 4 || secondInt.length != 4) ISOException.throwIt(ISO7816.SW_WRONG_DATA);
              byte [] result = new byte[4];
              byte carry = (byte)0x00;
              for (int i=3; i>=0; i--) {
                   int tmp = firstInt[i] + secondInt[i] + carry;
                   byte[] tmpb = intToBytes(tmp);
                   result[i] = tmpb[3];
                   carry = tmpb[2];
              return result;
    It doesn't work properly because java treats each individual byte as a signed integer. Plus I think this is horribly inefficient.

    797530 wrote:
         public static byte [] intAdd(byte [] firstInt, byte[] secondInt)  {
              if (firstInt.length != 4 || secondInt.length != 4) ISOException.throwIt(ISO7816.SW_WRONG_DATA);
              byte [] result = new byte[4];
              byte carry = (byte)0x00;
              for (int i=3; i>=0; i--) {
                   int tmp = ((short)firstInt[i] & 0x00FF) + ((short)secondInt[i] & 0x00FF) + carry;
                   byte[] tmpb = intToBytes(tmp);
                   result[i] = tmpb[3];
                   carry = tmpb[2];
              return result;
    Please you code tags when posting code.
    In the code you have posted there is a memory leak and your card will run out of EEPROM eventually. You should only ever use the new keyword in your applet constructor or code that is only ever called from the applet constructor as there is (essentially) no garbage collector.

  • Join two 8 bit integers and send via Serial Port

    I am trying to join two 8 bit integers and send the result via the serial port.
    I have a constant of hex value A that I need to join with a value from hex 0 - F (this is based on incoming data)
    When I use the Join VI, and type cast to change from hex to string for Visa Write, I recieve hex 0A0F.
    I have been able to use the hex 0-F with a case structure and then wire the corresponding constant ex A0 - AF.
    This makes the program very cumbersome and labour intensive to change. I have 22 commands I have to respond to with the address of 0-F.
    Currently, I have a Case structure that is selected with Message ID, then a case that is selected with subtype and then a case for address.
    Therefore I have to create a constant inside of each address case for each message as the responses are different.
    Thanks for any help
    Robin

    Gambin,
    As I understand it, you want to take the two bytes, put them together,
    and output the result as an ASCII string on the serial port.  This
    is easy.  Simply convert each number to an ASCII string,
    concatonate the two characters together, and send the resulting string
    to the VISA write function.  That's it!  I have attached a VI
    (ver. 7.1) that takes two hex numbers as input, converts them to ASCII,
    concatonates the results, and outputs the 'command' string.  Fell
    free to modify this vi and use it as you see fit.  I have left
    extra terminals in case you want to add error input/output for data
    flow, or whatever.  Notice that the display for the concatonated
    string is in '/' Codes Display mode.  This is because 0A hex is
    the newline character in ASCII.  You should also check to make
    sure that your VISA serial settings are not setup so that the newline
    character is the termination character.  If it is, the second
    character may not be recognised.  Hope this helps.
    Roy
    Attachments:
    HextoCommand.vi ‏17 KB

  • How to create a picture indicator from an array of 32-bit integers

    My problem is simple. I have an array of signed 32-bit integers that represents image information. I want to display that information in a picture indicator on the front panel of my VI, but none of the picture indicators accept the array. What conversions or picture control do I need to use?
    thanks!

    Try Draw Unflattened Pixmap.vi.
    Lynn

  • Displaying signed 8 bit integers using a data plug-in

    Hi there,
    I'm currently using DIAdem 10.1 and I'm writing a data plug-in in order to read binary data. What I'm missing is a data type "eI8" to read and display 8 bit signed integers. Obviously, to get the data into a channel, I have to use the data type eByte, but how do I make DIAdem display it as a signed value in the data portal?
    Many thanks in advance!
    Regards,
    Stefan

    Hello Stefan,
    you can use the method:
    Value = Object.GetNextBinaryValue(eDataType)
    eDataType
    Aufzählung.
    Bestimmt den Datentyp des auszulesenden Werts.
    2 - eI16
    16-Bit-Integer.
    3 - eI32
    32-Bit-Integer.
    4 - eI64
    64-Bit-Integer.
    5 - eByte
    Byte.
    6 - eU16
    16-Bit-Unsigned-Integer.
    7 - eU32
    32-Bit-Unsigned-Integer.
    9 - eR32
    32-Bit-Real.
    10 - eR64
    64-Bit-Real.
    Regards,
    Rebecca Fox
    Applications Engineer
    National Instruments

  • Buffered Reader Question about wrapping around another Reader

    I'm reading about BufferedReaders and in the API the following paragraph reads:
    In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
    BufferedReader in = new BufferedReader(new FileReader("foo.in"));
    I was wondering if somebody could explain their reasoning in slightly less complicated terms...I can't really figure out what their reasoning is. Thanks

    If every time you wanted to read a character, you had to talk to the hard disk device, it would be very slow. This is because communicating with the disk is thousands of times slower than communicating with main memory.
    The buffered reader reads in chunks, buffers those data, and allows you to request them one character at a time.

  • How about a 64 bits firefox

    all about why there is not a transparent and unequivocal explanation answer by Mozilla about a Firefox 64 bits browser by Mozilla it self?!!!

    I agree with JaineyCakes. We install Firefox on all our clients' machines. We're not used to getting smart aleck answers from our vendors. (Or at least not the vendors we keep).
    You asked for a reason why you should developing a 64 bit version for Windows. Well here are our reasons:
    - We have multiple machines with 12 GB or more of RAM
    - We and our clients have been running 64 bit Windows since 2009 (at least 5 years).
    - Almost all our other applications that we depend on in our business are 64 bit (and have been for years).
    - Increasingly, our clients are running more applications concurrently, including more and more tabbed sessions within Firefox. Also the complexity of the apps they run within the browser continue to grow in complexity.
    - Our clients' tolerance for slow performance continues to go down every year.
    Not sure if any of this matters to Mozilla. But we used to consider Mozilla to be a technology leader. Not sure if you still do.

  • Help about converting songs (bit rate)

    Hi,just before begining I want to say that I did a search about the topic and found many things so what I am looking for is different opinions.
    I read that if I convert songs from one compress format to another, the songs will lose some quality no mather what's the compressed format and bit rate. Is that true? Like if I go from Mp3 to acc?
    Another thing that I want to be sure of is that AAC at 160kbps is audio cd quality. Is that true?
    Well thanks in advance

    Hi Maxime
    First: yes if you convert a .mp3 file to .acc it will lose some quality, because both formats are compressing the original data by using a alogrithm. Compressing an already compressed file = Data loss = Loss of quality. The todays compressing methods are really great and the loss won't be much, I guess. But you have to try out and listen to it to decide if it's still ok for you.
    The term "cd quality" is (miss)used on every second website. The thing is, songs on a CD are in a noncompressed format (like .wav) so it's not possible to get a .mp3 file, that reachs exactly the same quality. But an mp3 of 192kbps you get nice results and it should be ok for most of the users. ACC should be a bit smaller by the same quality level, but I don't use it. If you want real CD quality, you have to buy it or burn it without any compression.
    Thats not the ultimative truth but only my opinion.
    cheers
    P

  • Any clue about a 64 bit iMac ?

    Hi,
    I'm thinking -as well- about buying a new iMac. A friend who has been an iMac user since.... well... no clue, long ago, meant that end of the year the "new" iMacs 64 bit will come out.
    hmm... unfortunately, all based on speculations, i suppose.
    (http://www.macintouch.com/readerreports/macsonintel/topic3804.html)
    I know it is almost stupid to talk about this subject when the new iMacs Intel Duo just came out. But i do need to buy a new PC around the end of the year and was wondering if anybody has heard about this ?
    Anyways i suppose Mr. Jobs has something up his sleeves to draw the attention of all the millions of ppl looking to buy a new system after the new Windows Vista comes out around end of the year, right ?!
    Well, i know maybe this topic doesn't fit here, but i couldn't find another place where to put it.
    Cheers, cj.

    Okay bottom line is Apple wont be shipping 64-Bit computers until they can reconfigure it because that would totally go against Apples move to intel...remember the "performance per watt" by Steve Jobs? Well 64-Bit will totally go against what apple believes because 64-Bit Chips will take more power and it will heat up more than the regular 32-bit chips unless Apple and Intel figures out a solution to minimize the power consumption.
    Maybe we'll see a 64-Bit PowerMac that will run intel in the near future but I don't think any other mac line will go 64-Bit.
    As far as I know there the PowerMac G5 Quad that is 64-Bit powered but it's the IBM processors are still not fast enough to really push the 64-Bit to its core. Apple could have just used the Intel EM64T instead of their core duo but intel still has a lot to work on with their 64-Bit Chips. Imagine the iMac or any mac product running Intel EM64T, it would be a disaster. Right now the new core duo iMac aren't up to part and even intel admitted that their EM64T chips still needs tweaking.
    So we probably wont see any mac with 64-Bit chip any time soon..give apple and intel maybe 2-3 years before the next move since apple has just started the core duo transition and they're not even fully intel-ized yet so it's going to be a long wait if your waiting for 64-bit mac.
    The core duo are pretty fast but you have to understand, all the problems the consumers are experiencing isn't gonna be the whole time since it's apples softwares and such it's not necessarily the hardwares..if thats the case apple will have the patches and updates hopefully to solve these issues. As soon as the transition is complete, meaning all the mac apps out there are universal, then you start to see the performance apple has promised.
    I suggest waiting for the PowerMac line because it's the "PRO" of all the pro products apple offers so you can wait for that to go intel as well and thats my best bet if I were you, it would deliver such power you need for pretty much anything and maybe even better than the 64bit powered pc's out there since at this moment 64-Bit hasn't been perfected. As AMD and Intel stated, and even apple... "It's the future of computing" they didn't say "the future is now" so basically what I think they meant by "It's the future of computing" is that they are still working on it.
    20" iMac Core Duo 2GHz 2GB, PowerbookG4, PowermacG5 Quad   Mac OS X (10.4.5)  

  • I upgraded to Lightroom 5 about 2yrs ago,bit now my system is asking me to renew my membership to activate the develop module in Lightroom5. The upgrade was already paid for though.

    I upgraded to lightroom5 about 2yrs ago, but now my system prompts me to renew my membership to activate my develop module as the develop module has been de-activated

    If it asks for an activation to activate the develop, it must be a CC version installed. If you use a CC version you have to pay you for your selected plan like monthly or annually.
    If you should have the stand-alone version because you don't have/want a CC plan, then you probably have installed the wrong version - the CC instead of the stand-alone.
    Download the stand-alone version from here: Win, Mac

  • About 64 & 32 bit

    hey there, how do I know that my Intel-based iMac is using 32 or 64 OSX?

    Go to the Apple Menu in top left hand corner and choose About This Mac. Under Processor it lists which processor you have whether it be a Core Duo or Core 2 Duo or G4 or G5. G4 processors and earlier are 32bit. G5 processors are 64bit. And then like Kappy said Core 2 Duo or the Xeon processors in the Mac Pros are 64bit.
    George

  • Need help about byte-to-bit

    I wanna convert a byte to 8-bits and store them in a bit array. any API method I could use or how to write it by myself?
    8bits-to-byte method is needed as well
    Thanks!
    Message was edited by:
    xixiao

    There's java.util.BitSet.

  • I'm a bit confused about this 32 bit and 64 bit stuff

    Martin Evening's book says "If your computer hardware is 64 bit enabled and you are running a 64 bit operating system, you can run Lightroom as a 64 bit program."
    Well I'm currently running XP 64 Bit with 4 GB of RAM inside. How do I run Lightroom in 64 bit mode?
    He says Windows 7 users and Windows Vista users will want to buy the 64 bit version of Lightroom. I don't see any 64 bit versions of Lightroom. And for those of us on XP?

    Road Worn wrote:
    I bought the disc. I explored the disc and I did find the Setup32 and Setup64 on there so I will assume the installer installed the correct version for me.
    You can check in task manager, whether LR is running in 32 or 64-bit mode for you. AFAIK, the 32 bit processes should be marked *32 in WinXP also.
    What version of LR are you running? Since you installed from CD, you might have LR3.0 installed. In this case I would strongly suggest you download Version 3.4 from here and install it. There have been many bug fixes from 3.0 to 3.4.
    Beat

Maybe you are looking for

  • Inventory data load issue

    HI all,          We have 2 source system SAP4.7 and ECC6.0. I am using 3 data source BX, BF and UM. one year data we required from SAP4.7 and all data from ECC6.0. In SAP4.7 totally 7 years data available but we required only last one year. my doubt

  • PDFLPrintDoc functionality on non-English OS?

    We are using Datalogics PDF 8. 1 librrary in an exe. This has the functionality of printing a PDF region to a custom printer that generates a .EMF file. For this, we are setting few printer params using PDPrintParams and PDFLPrintUserParamsRec and th

  • Transfer Movies & TV shows to other Mac

    I have purchased both movies and TV shows on my Mac Pro. I would like to watch them on my big screen TV in the living room. To do so I would have to transfer the videos from my Mac Pro to my MacBook Pro so I can take my portable to my living room to

  • How can I get a mail delivery report

    Does anyone know how I can set mail to send me a delivery report when a message I have sent has been read? Thanks Rach

  • My iPad locked up while playing a game.  Now can't turn it off or do anything.

    Nt iPad locked up while playing a game.  Now I can't get it to shut down or do anything.  The buttons don't do anything.  Help!!