Binary Numeric Promotion

From the JLS - When an operator applies binary numeric promotion to a pair of operands, and these are both characters, both operands are converted to type int.
So given
byte b = 'b' + 'a';I see a compiler error as I would expect. The operands would be converted to type int and the result could not be assigned to a byte without a cast.
However, the following all compile and run successfully..
byte b = 'b' - 'a';
char c = 'b' - 'a';
char ch = 'b' + 'a';But how? Aren't the characters 'b' and 'a' converted to type int and doesn't this preclude direct assignment to byte or char without an explict cast? What is the difference?

Thanks for your posts jverd. You've helped me see
that I was overlooking the obvious. That is, the
actual value of 'b' + 'a' is outside the upperrange
of byte which is 128. 127, actually. :-)LOL. Yes indeed.

Similar Messages

  • 5.6.1 Unary Numeric Promotion

    can anyone explain why 5.6.1 is written so differently to 5.6.2? is this an equivalent simpler formulation of 5.6.1, or have i missed something?
    ...type:
    * If the operand is of a reference type, unboxing conversion is performed. Then:
    * If the operand is of compile-time type byte, short, or char, unary numeric promotion...
    here's 5.6.1 and 5.6.2 from adding_generics-2_4ea:
    5.6.1 Unary Numeric Promotion
    Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type:
    * If the operand is of compile-time type Byte, Short, Character, or Integer it is subjected to unboxing conversion. The result is then promoted to a value of type int by a widening conversion (?5.1.2) or an identity conversion.
    * Otherwise, if the operand is of compile-time type Long, Float, or Double it is subjected to unboxing conversion.
    * Otherwise, if the operand is of compile-time type byte, short, or char, unary numeric promotion promotes it to a value of type int by a widening conversion (?5.1.2).
    * Otherwise, a unary numeric operand remains as is and is not converted.
    In any case, value set conversion (?5.1.8) is then applied.
    5.6.2 Binary Numeric Promotion
    When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (?5.1.2) to convert operands as necessary:
    * If any of the operands is of a reference type, unboxing conversion is performed. Then:
    * If either operand is of type double, the other is converted to double.
    * Otherwise, if either operand is of type float, the other is converted to float.
    * Otherwise, if either operand is of type long, the other is converted to long.
    * Otherwise, both operands are converted to type int.
    After the type conversion, if any, value set conversion (?5.1.8) is applied to each operand.

    5.6.1 operates on two types, 50602 operates on one type.

  • Problem with bits operation

    Hi
    I have the next code that is throwing the
    recorreBits.java:8: possible loss of precision
    found   : int
    required: byte
      byte c =        a & b;
                             ^
    1 errorand the code is
    public class recorreBits {
         public static void main(String args[])
         byte a = 0x20;
         byte b = 0x10;
         byte c = a & b;
         System.out.println(c);
              }I'm using byte don't know whyt it found's a int. Thanks
    Edited by: Ikim on Oct 6, 2009 1:14 PM

    [JLS 15.22.1 Integer Bitwise Operators|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.1] says:
    When both operands of an operator &, ^, or | are of a type that is convertible (§5.1.8) to a primitive integral type, binary numeric promotion is first performed on the operands (§5.6.2). The type of the bitwise operator expression is the promoted type of the operands.
    So, since byte is a primitive integral type, binary numeric promotion occurs, and both are converted to int, and the result is then int. You can't assign an int value to a byte variable, unless it's a compile-time constant, so you have to cast.
    byte c = (byte)(a & b);Edited by: jverd on Oct 6, 2009 1:19 PM

  • Byte.MAX_VALUE, Integer.MAX_VALUE

    hi,
    just curious..
    byte b = (byte)Byte.MAX_VALUE+1 //does not compile
    short s = (short)Short.MAX_VALUE+1 // does not compile
    int i = (int)Integer.MAX_VALUE+1 //compiles
    can someone tell me why this happens?
    thanks,
    shishir

    thanks.
    any idea why adding 1 in int doesn't promot it to
    long?[url http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.2]JLS 15.182 Additive Operators (+ and -) for Numeric Types says: Binary numeric promotion is performed on the operands (�5.6.2). The type of an additive expression on numeric operands is the promoted type of its operands.
    And [url http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983]5.6.2 Binary Numeric Promotion says: ...Otherwise, both operands are converted to type int.
    So any two integer types yield an int result, unless at least one of them is a long. byte + byte gives int, so the result is an int. int + int (e.g., Integer.MAX_VALUE + 1) yields an int, which just silently wraps around through Integer.MIN_VALUE (0x80000000).

  • Binary String to numerical binary array

    Hello,
    I initially created binary strings from numerical numbers using the
    "%03b" command on the 'Format into String' block. I then concatenated
    the various strings into one string that is now composed of 16bits.
    However, I need to convert this string that I created back to a binary
    numerical array for further processing.
    Can you help me with this?
    Thanks,
    Rajesh.

    triniboy wrote:
    I initially created binary strings from numerical numbers using the
    "%03b" command on the 'Format into String' block. I then concatenated
    the various strings into one string that is now composed of 16bits.
    In general, you are out of luck, because the number of characters for each value when formatted to binary with "%03b", will be variable. Any number greater than 7 will use more than 3 digits. (since you use "03" instead of 3", smaller numbers will have three digits with possible leading zeroes for padding).
    After you concatenated all the formatted strings, you loose all boundary information unless ALL numbers are less than 8, but in this case they result would be a multiple of 3 and would not add up to 16 bits, right?
    My best suggestion would be to NOT create a binary formatted string in this way, because it is a one-way operation and cannot be undone because you loose information in the process.
    Back to square one!
    Maybe we can backup a few steps and you can explain to us what you really want to do with your original numbers. Why would you even consider formatting them this way? Do you need to save them to a file for later retrieval, for example?
    LabVIEW Champion . Do more with less code and in less time .

  • Show 8 bit binary data on graph

    Hello..
    I have an 8bit binary numeric control and I want it to be shown on graph so that it would look like a digital bit pattern just like image below.
    Please Help..

    Try something like the attached draft (LabVIEW 8.2).
    convert number to boolean array
    convert boolean array to (0,1) array
    graph (0,1) array.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    GraphBits.vi ‏13 KB
    GraphBits.png ‏8 KB

  • Binary Indicator for Digital Output Device

    This probably simple question to most of people but there is always a first time to do a new thing/thing that we never have done.
    I have NI 9472 which is a digital sourcing device, I have no problem to control it using LABView to output the result (line 00000000).  I also would like to have some indication on the computer screen using LED array.  I am able to create an binary array indicator but fail to connect the decimal input to the binary array.
    Please help/provide some hints, it is appreciated.
    Solved!
    Go to Solution.

    I'm with Dennis.  Just use a numeric indicator.  Set the representation to U8.  Right-click on it and select Visible->Radix.  Right-click again and choose Display Format.  Choose the Advanced editing mode and put in %08b.  The result will give you an indicator like this.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    binary numeric.png ‏2 KB

  • Date/time in LR 2.1 -- still not what I expect

    Date/time of images that I scanned and input the dates outside of Lightroom are now working better, but still not what I would call correct. If I use the Library Filter, the date selection is correct under "Date".
    This is the date that Lightroom calls "Date Time Original" in the metadata panel available on the right side. But when I select a folder and ask LR to sort based on "Capture Time", it sorts on what it calls "Date Time" not what it calls "Date Time Original". What I put in Date Time is the scan date, usually called the "Date Time Digitized". (I care when they are digitized because that indicates the quality of the scanner.) This shows up as "Date Time" in the metadata panel.
    So why is the metadata search based on "Date Time Original" and the sorting by "Date Time Digitized". Surely most people want a sort on "Date Time Original" (which is the same as LR's "Date Time" in a digital camera) not some other date. If they think some people really want the other date, then it should be a choice under sort.
    The sort field is called "Capture Time". OK, I agree, I did scan the files on that date. So in that sense it is the "capture time" but MY opinion is I "captured" them on film on the "Date Time Original" date and that is always the date I want them sorted by. The other date is just for reference in my case.
    If "Capture Time" is really what the designers of LR think I want, why is that not the date in the library filter?
    Judy

    Holy macaroni! I think I got it right this time. But from now on I'm using Integer.toBinaryString(int) ...or my own special brand of sluggish code he he. Thanks for the input though. Really appreciate it.
       public String toBinary(int innTall) { // i.e 123
            while(innTall > 0) {
                rest = innTall%2;
                innTall = innTall / 2; //i.e 61-30-15-7-3-1-0
                if(rest > 0)
                    tall = 1;
                else
                    tall = 0;
                result = result.concat(result.valueOf(tall));
            return (reverse(result));//returnerer den omvendte rekkef�lgen av bin�rtall
        //internal method for flipping a string of binary numers
        private String reverse(String sekvens) {
            char tegn[] = new char[sekvens.length()];
            while (sekvens.length() != index ) {
                tegn[index] = sekvens.charAt((sekvens.length()-1) - index);
                System.out.println(tegn[index]);
                index++;
            index = 0; //reset index
            String newString = new String(tegn);
            return newString; //toString er synderen
    }

  • MS6147 out of order

    I have bought several MS6147 Ver1:1 motherboards about 1 year ago and several are now out of order.
    I have notice that in five of those, there are three bumped capacitors (EC4, EC5 and EC6), which I have replaced but PCB still doesn't work.
    With capacitors replaced there are now 157 Ohms between both terminals. (Previously it was shortcircuit).
    Now, with those capacitors replaced, fan runs for about 15 seconds and it stop again. Nothing appears on screen.
    I have checked CPU's and memory cards in another board and they are OK.  
    Anyone has similar problems or knows how to fix this problem?.
    Thanks in advanced.
    Manolo

    John,
    I am not sure what you mean by am I "in folder view". On the left side panel I can see folders and all the folders are in the correct order.
    What do you mean by create a new folder of images and repeat?
    I always create a new folder where the folder name is the date (for example 10 Feb 07 is 070210. And all the images in the folder are 070210-1, 070210-2,070210-3 etc. After renaming the files I place them in the folder and then import into LR.
    I checked the metadata for the images and the original date and time are still there and correct. As you would expect from untouched raws and jpegs.
    Here is a small extraction of the data from one folder. Sort by capture time is selected. But despite this the files remain to be displayed in what seems to be a strange binary numerical order by filename - not even in true numerical order. Even when I attempt to sort by File name the images are ordered in the same fashion as below.
    Filename Date Time
    070129-1 30.01.2007 07:56:53
    070129-10 30.01.2007 08:08:44
    070129-100 30.01.2007 11:13:33
    070129-101 30.01.2007 11:13.53
    070129-102 30.01.2007 11:14:32
    070129-103 30.01.2007 11:14:39
    070129-104 30.01.2007 11:44:41
    070129-105 30.01.2007 11:44:43
    070129-106 30.01.2007 11:15:56
    070129-107 30.01.2007 11:16:34
    070129-108 30.01.2007 11:16:36
    070129-109 30.01.2007 11:46:40
    070129-11 30.01.2007 08:10:23
    070129-110 30.01.2007 11:16:46

  • Why does my MacBook Pro OS X show a different file size than previous models, of the same folder?

    I'm copying a folder from the server to burn to disc from my MacBook Pro OS X 10.6.8, then tried to burn it to disc using Toast Titanium.
    It gave an error saying it's too big: 4.7 GB, then when we checked from a previous model MacBook Pro OS X 10.5.8 and it was 4.3 GB.
    How is this possible?

    Apple changed the way file sizes are reported between Leopard and Snow Leopard. Leopard saw 1024 bytes as a kilobyte, 1024 KB as a megabyte, and 1024 MB as a gigabyte: the traditional binary-numeral nomenclature used for decades in the computer industry. But consumers didn't understand that, and always wondered why a hard drive with, say, 250,000,000,000 bytes of storage space on it was reported to contain 232 GB instead of 250. So to cater to a dumbed-down public, in Snow Leopard and Lion, 1000 bytes is a KB, 1000 KB is a MB, and 1000 MB is a GB: decimal nomenclature. Consequently, SL and Lion report larger file sizes than older versions of OS X for the same files and folders.

  • Implicit cast differs?

    hello,
    I'm just getting weird concerning the following issue:
    public class Strange {
           public static void main(String[] args) {
                  int n = 10;
                  while (true) {
                        try {
                             n -= 0.5;
                             System.out.println(n);
                             Thread.sleep(100);
                        } catch(Exception e) {
                             e.printStackTrace();
    }First: don't mind that this class makes no sense.
    What I tried is to decrease an int by 0.5
    n starts at 10 and decreases to zero, but then it doesn't decrease anymore.
    So 0.5 is once casted implicitly to one and later to zero?
    Can anyone explain this behaviour?

    From JLS: A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
    So n -= 0.5;is semantically equivalent to n = (int)(n - 0.5)The expression (n - 0.5) produces a result of type double (numeric promotion.)
    Then this value is cast into an int. The language uses round toward zero when converting a floating value to an integer.
    Therefore, both double values 0.5 and -0.5 will be produce a int value 0.

  • How to get booleans out of a byte?

    I have a ByteBuffer where the first byte contains some booleans. How can I get the booleans out of that byte?

    You can use bitwise operators manually: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
    e.g. if you want to know if bit 2 (value 4) is set:
    if (value&4==4) {...}
    Where does "value 4" come from?It's the decimal value of a byte whose "2" bit is set (1) and all other bits are 0.
    My "indexing" scheme is the exact contrary of yours, I think mine is more conventional (see this Wikipedia entry: [url http://en.wikipedia.org/wiki/Binary_numeral_system]binary numeral system): the idea is the same as in decimal, rightmost "figures" have lower value than their left neighbor (e.g in 12, the leftmost figure is 1, but it actually represents value 10).
    bit                .  .  .  .  .  .  .  .
    index              7  6  5  4  3  2  1  0
    value (weight)   128 64 32 16  8  4  2  1The respective values for each of the 8 bits of a byte are, right to left, 1,2,4,8,16,32,64,128 (these are the power of 2; exactly as in base 10 the values of the positions are 1,10,100,1000,... as you go from right to left). The rule is: value(a.k.a. "weight")=2^index
    Note that the value 128 does not exist for a byte (because it is signed), but the bitwise operation would work anyway (as the operands are converted to 32 bits ints). See again Wikipedia on [url http://en.wikipedia.org/wiki/Two%27s_complement]Two's complement.

  • Negative out put

    Hi all,
    The output of the code below is -2147483630. On a calculator, the answer is 2147483666. Now since the calculator answer is bigger than an int, I would think that it would cast up automatically.
    What's the reason for the negative output?
    Is my thinking backwards?
    class A
    public static void main(String[] args)
    int x = 1073741833;
    System.out.println(2 * x);
    thanx for the sugessions

    Now since the calculator answer is bigger than an int,
    I would think that it would cast up automatically. But it isn't. This is because every integer operation made on an int is guaranteed to evaluate to an int. On the other hand, operations made on "smaller" integer types also evaluate to an int. IIRC, the term "numeric promotion" in the JLS has to do a lot with this.
    An automatic up cast like that would make code depending on the size of primitive types much more obscure than what it is now. Currently you are guaranteed that a calculations made with 32 bits will always stay in 32 bits and overflowing will not make the bit count jump to 64.
    (Also, if all expressions done on integer types would in fact evaluate to longs, doing an explicit down cast to int every time you wanted to do "i = 2*i+1" or something similar would be a bit of a burden...)

  • IPad Retina Mini storage stats sluggish..

    I've had my Retina Mini for a couple of weeks now and it runs just fine but every time I want to see how much space is left on the ipad (it's only a 16gb so space is tight!), it takes between 40 seconds and a minute to tell me I have X mb/gb left!
    In case you're not sure how to get this info, it's in Settings, General, Usage. The applist appears on the top right eventually.
    On my wife's ipad 3, running the same 7.04, it takes about 2 seconds. Both of us have a nearly identical app list installed!
    I've tried reseting, restoring but it doesn't make a difference at all. Has anyone had this problem?

    Hi maxgkx,
    I understand you would like more information about storage capacity, and how it is reported. The following article provides helpful information on the topic:
    How OS X and iOS report storage capacity - Apple Support
     Understanding storage capacity in iPad, iPhone, iPod
    When you view the storage capacity of your iPod, iPhone, iPad, or other electronic devices within its operating system, the capacity is reported using the the binary system (base 2) of measurement. In binary, 1 GB is calculated as 1,073,741,824 bytes.
    For example: The way decimal and binary numeral systems measure a GB is what causes a 32 GB storage device to appear as approximately 28 GB when detailed by its operating system, even though the storage device still has 32 billion bytes (not 28 billion bytes), as reported.
    You can see this difference if you look at how your computer summarizes the capacity of your iPod, iPad, or iPhone’s storage when the device is connected to your computer. You will also see this difference in the About menu on your iPod, iPad, or iPhone. The important point to understand is that the available storage capacity is the same no matter which system (decimal or binary) is used. Nothing is missing.
    The storage media in your Apple product, like all storage devices, uses some of its capacity for formatting, so actual capacity available for applications and files will be less. In addition, other factors, such as pre-installed systems or other software and media, will also use part of the available storage capacity.
    Thank you for contributing to Apple Support Communities.
    Take care,
    Bobby_D

  • Integral primitives casting question

    Hi. I noticed this thing recently and can't understand it.
    I have a byte and set it to -128. Then I cast it to an int, and because the highest bit is set to 1 all the new bits in the 3 higher bytes of an int are all set to 1. This I understand so far. What bugs me is why the int value is still -128. Isn't
    1111 1111 1111 1111 1111 1111 1000 0000 a different value? Maybe someone could explain this because such casts are oe of the operations I make in my app and I am afraid it may not always work correctly.

    > Isn't 1111 1111 1111 1111 1111 1111 1000 0000 a different value?
    Nope. That's -128 (that's kind of the whole idea in "filling in all the extra 1s" - to retain the byte's value).
    Binary numeral system (Wikipedia)
    Two's complement (Wikipedia)
    Cat and Mouse Games with Bits
    Cup Size -- a story about variables
    Hope this helps!
    ~

Maybe you are looking for

  • Getting started with an iBook G3?

    Hey, I just got an iBook G3 (500mhz, snow, airport, 10gb hd, upgraded ram), to compliment my iMac (G5 rev c) and I'm a little new. It was used, I got it via trade. Does anyone have some tips for me to get started with it ? I'm wanting to upgrade the

  • Setting up a Mail domain Alias

    Hi We are trying to set up a mail domain alias so that a user can receive mails on either [email protected] or [email protected], the default mail domain on which OCS has been installed is domainA.com I've tried the rewrite rules for inbound SMTP as:

  • Blackmagic Multibridge Pro

    Has anyone had luck with the Multibridge Pro and external monitoring with Color? I am upgrading from AJA Io and have a 36" LCD w/ HDMI and I'm hoping that buying the Multibridge Pro with HDMI out will allow me to color correct on the LCD from Color.

  • BW data Upload issue

    Hello Gurus, We have upgraded our APO system to SCM5.0. When I am trying to upload upload a flatfile into BI7.0 I am getting an error. Here are the error details: Error 1 when loading external data Message no. RSAR234 Diagnosis Error number 1 occurre

  • My iPhone 4S hangs (gets stuck or unresponsive) when deleting photos from the camera roll or use Messages app

    I am now on my third replacement phone from Apple because of this issue.  My iPhone 4S frequently hangs (gets stuck), without response, when I click on the trash icon to delete a photo, for about 20-30 seconds before becoming responsive again. I noti