Bitwise shift operator...

I know this is very basic but I've never done this...
If I have an object that is an Integer, and I want to increase it by 1, isn't this how you do it?
Object ob=new Integer(10);
ob=ob>>1;it tells me that >> operator cannot be applied to object, int
So what can it be applied to..?

Ummmm,sadly, no... :(
I really do lack basic computer skills, I don't know
a lot about binary and stuff... but how would you do
what I want to do..?I asked because it's quite a weird code. Just checking.
OK, so:
the binary shift operator can only be applied between to ints, that is primitives, no Objects (like Integer is).
This operator is not used to increase a number, but to shift its bits by one to the right.
Usage:
int i,j;
j = 10;
i = j >> 1;
Integer obj = new Integer(10);
int res = obj.intValue() >> 1;
Integer resObj = new Integer(res);

Similar Messages

  • Left-shift operator used on byte values

    Hello,
    I'm reviewing some problems, and I need some help. I have a program that has some code like the following:
    byte y = 10; // 00001010 in binary
    byte result = (byte) (y << 1);
    System.out.println("result: " + result); // 20.  Ok.
    result = (byte) (y << 7);
    System.out.println("result: " + result); // 0.  Ok.
    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.
    // 8 % 8 = 0 number of bits for the shift.
    result = (byte) (y << 6);
    System.out.println("result: " + result); // -128.  Ok.
    result = (byte) (y << 10);
    System.out.println("result: " + result); // 0.  Why???
    // Shouldn't it be 2 bits for the shift?
    // That is, 10 % 8 = 2.
    // I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.the result of (y << 8) is an int, not a byte. the byte "y" is promoted to int for the bit shift. so the int result of the bit shift is 00000000 00000000 00001010 00000000. when you cast that back to byte, the 24 leftmost bits get lopped off, and you're left with zero.
    hth,
    p

  • How can i do bitwise (binary) operations (AND, OR, XOR)

    I need to do bitwise (binary) operations such as AND, OR, and XOR.  For example, how do I AND 10101000 with 11111000?

    for OR, with 10101000 in A1 and 11111000 in A2, try:
      =1×SUBSTITUTE(A1+A2,2,1)
    Result:  11111000
    and for XOR, try:
       =1×SUBSTITUTE(A1+A2,2,0)
    Result: 1010000  (missing leading 0)
    You can display missing leading zeros by setting the cell's Data Format to 'Numeral System' with Base set to 10 and Places to 8, or turn the result into a string with something like this (assuming the result is in A3):
         =RIGHT("0000000"&A3,8)
    SG

  • Logical right shift ( ) operator

    Hi,
    I suspect a bug in the logical right shift operator >>> (as opposed to the arithmetic right shift >>).
    The documentation says "0-s will be shifted from the left". It is not the case.
    byte b = -128;                         // this means 0x80 in hex notation
    byte b1 = (byte)(b >>> 4);I would expect b1 = 0x8 (0-s shifted in from the left) but the result is
    b1 = 0xF8 instead, as if I had used the >> operator. The sign bit was shifted in from the left instead of 0-s.
    Of course, there is a workaround:
    byte b1 = (byte)((b >>> 4) & 0x0F); which is the same operation as using >>
    If this is the case, what is the purpose of using >>> instead of >> ?
    Anyway, the ONLY thing that frustrates me in the Java language that it does not support unsigned integers. It would be important for embedded systems where unsigned numbers are used more frequently.
    Any suggestions?
    Thank you,
    Frank

    sabre150:
    It depends on the size of processor you use. The Java program runs on a 32- or 64-bit processor, so there is no problem promoting from byte to short or int. But I am talking to an 8-bit processor over a network. So I get a byte stream that has to be converted to numbers here on the Java side. The data types are various: byte or short, even sometimes two 4-bit nibbles (here comes the >>> operator), signed or unsigned. If I get a two-byte sequence, I have to convert it to a short, signed or unsigned. That means in Java promoting the byte to int, shift left the high byte, OR the low byte then cast it back to short if signed, or mask with 0xFFFF if unsigned. The resulting int can be converted to string in the usual way. With distinguished signed and unsigned types, this would be done by the compiler. But I come from the Assembler (and then Pascal/Delphi) world, so it is not a big problem for me.
    I agree that this feature is used very rarely, I already wrote the conversion routines, so why bother? As a newcomer to Java (after struggling with C for years, I love it), I just wanted to see whether other people have a better and more efficient idea.
    Thank you all for your replies!
    Frank

  • The Shift operator Question

    Dear sir/madam
    I having question for the shift operator.
    What different between >>> and >> ?
    I have refer to sun tutorial, it state >>> is unsign, what does it mean?
    and last is
    if integer is 13
    what anwer for 13>>8
    is it move for 8 right by position? pls told me in detail
    is it answer same with the 13>>>8 ?
    Thanks for ur solutions

    You can play with the following for a while.
    final public class BinaryViewer{
      final static char[] coeffs = new char[]{(char)0x30,(char)0x31};
    public static String toBinary(int n) {
      char[] binary = new char[32];
      for(int j=0;j<binary.length;j++) binary[j]=(char)0x30;
      int charPointer = binary.length -1;
      if(n==0) return "0";
      while (n!=0){
          binary[charPointer--] = coeffs[n&1];
          n >>>= 1;
    return new String(binary);
    public static void print(int n){
        System.out.print(BinaryViewer.toBinary(n));
        System.out.print("   ");
        System.out.println(String.valueOf(n));
    public static void main(String[] args) throws Exception{
       int n=Integer.MIN_VALUE;//Integer.parseInt(args[0]);
       int m=Integer.MAX_VALUE;//Integer.parseInt(args[1]);
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
        n=1024;
        m=-1024;
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
       n=2048;
       m=-2048;
       BinaryViewer.print(n);
       BinaryViewer.print(n>>8);
       BinaryViewer.print(n>>>8);
       System.out.println();
       BinaryViewer.print(m);
       BinaryViewer.print(m>>8);
       BinaryViewer.print(m>>>8);
      System.out.println();
    }

  • Shift Operation in Prodcution order.

    How to record the shift operation in the Production order?

    Hi,
    Please try to simply your subject of posting. It is not necessary your subject and body of discussion should be same.
    Yes possible to remove under SQL management studio provided you have authorization to access.
    Thanks & Regards,
    Nagarajan

  • Pda reproducible bug with logical shift operation when y 3 for arrays

    Folks,
    I have discovered a problem and wonder if this is a wider bug or if I am doing something wrong:
    Labview PDA 8.0.1  -  running on a Dell Axim:
    I create a 2x1000 array of U32 and feed it as x into the logical shift operator.  If  y is between -3 and 3, the logical shift works properly.  If y >= 4 or <= -4, then the array is returned with all zeros.
    I have reproduced this for I32 data and smaller arrays.  Logical shift works properly for scalars.
    Has anyone else seen this?  Is this a bug?
    Thanks for any help.

    It might very well be a bug. If it works differently on the PC and on the PDA then it is almost certainly one.
    I suggest you post a simple example showing this so that people with current versions of the PDA module (i.e. not me) can test this. Maybe it was even fixed for 8.2?
    Try to take over the world!

  • How can I do Shift operation in plsql

    hello,
    i want to know whether there is any package or operator to do the shift operation in plsql.
    like (myVar<<8 in c++).

    why don't you use
    myvar := mywar * power(2,8);

  • JAVA (SHIFT OPERATOR)

    Hi,
    I really want to know SHIFT operator in Java like >> , << , >>>
    Could anybody kindly help to explain??
    And
    byte a =-1;
    a = (byte)(a>>>2);
    why the output become -1??
    thanks.

    umm i did get them mixed up. preserved sign bit right shift is >>, pulling-zeros is >>>. so keep that in mind. (ie, 18>>2 == 10010>>2 == 11100 which is -4)
    To calculate the resulting number from >>> right shifts, check this: http://www.janeg.ca/scjp/oper/shift.html
    In response to your original question, it seems it's far more complicated than I at first thought. The -1 that you're getting is an odd (and rather interesting, imo) byproduct of modular arithmetic. what you wrote is exactly similar to this code (ie, my code mimics the castings that happen in your code):
            byte a = -1;
            int b = (int) a;
            int c = b>>>2;
            byte d = (byte) c;
            System.out.print(d);So what happens is A, which is a string of ones (ie 11111111) is casted to an int, B. In general, casting a byte to an int returns an int with exactly the same bits at the end with the sign bit repeated 24 times at the start (ie, 01101011-->(int)0000...00001101011). So B is 11111111111111...1111 (32 ones). Then we right shift B by 2 (we'd get the same answer right shifting it by 0, 1, 2, etc, all the way up to (and including) 24). Then we recast the int C to a byte. This is where the interesting thing happens: Depending how much we right shifted B to get C, C will be a certain number of zeros (call this Z) followed by (32-Z) ones. Apparently, in (mod 2^8) in two's complement all of these numbers are congruent to -1, so the cast gives negative one.
    (remember that the int 127 cast to byte is 127, while the int 128 cast to byte is -128. conversions to a lower-bit type always happen mod 2^N, where N is the number of bits in the type we're casting to.)
    BTW, thanks for this problem. i had a fun half hour figuring it out. hopefully it made some sense to you...
    (EDIT: Flounder is right--you can just view the cast from int to byte as keeping the last 8 bits. much easier to think of it that way, though my definition does give some intuitions about WHY all this works.)
    Edited by: a_weasel on Oct 13, 2008 8:00 PM

  • Bitwise shift in ABAP?

    Hi all,
    How can I shif data bitwise in SAP? Is it possible?
    Best Regards
    Marcin Cholewczuk

    Hi,
    Perhaps the below link could give ideas:
    http://stackoverflow.com/questions/8349886/bit-shifts-with-abap
    PS: I am not familiar with ABAP coding, as mainly into Basis - just thought could help
    Regards,
    Srikishan

  • Shift operator in work center

    Our plant works in 3 shifts. for every shift there are total six persons working on it. 2 in each shift.
    we need to assign the name of person working on the particular machine. Where can be the assignment  of the actual person working on the workcenter be made.
    thanks and warm regards.
    Edited by: pankaj lade on May 8, 2008 12:32 PM

    Hi,
    One information required..
    Why you wanted to have the operator names to be attached to your work centers?
    You can have the Operator Names at the time of Confirmation of the Operation (This is what you wanted for Tracking purpose?)
    1. Have the Operator Name or No. in Personal Number Filed in CO11N (you need to actvate the HR Transactions)
    2. Activate a Stadard Value key with Operator and during confirmation the relevnt operator can enter their name or No. there
    3. in CO11N text field as the operator to enter their name..
    Hope this helps..
    Regards,
    Siva
    Edited by: Siva Kumar M on May 8, 2008 4:22 PM

  • Shift operation on Weekly off?

    Hi
    Gurus,
    what to do when i have  to run Production on Weekly Off during urgency ?
    We have weekly off on tuesday on this day i want to carried out production.
    Should i define / change holiday calender? or what......
    Plz guide me
    Harish

    Hi,
    Goto Workcenter where you want to perform operations,
    CR02, goto Capacities view, click on capacity header, click on intervals & shifts,  For valid period select in workdays column maintain 1- Work day( overides factory calander). then its a working day for work center, although its a factory declared holiday also.
    Regards
    Jagadeesh K

  • Shift operator operations

    int i=1;
    i <<= 31;
    i >>= 31;
    i >>= 1;
    can anyone explain how the result i value=-1
    thanks

    The high order bit of an integer is the sign. The ">>" operator maintains the sign. See the results of your shifts below which results in a value of -1. Use ">>>" if you do not want to propagate the sign.
    i = 1      00000000000000000000000000000001
    i <<= 31   10000000000000000000000000000000
    i>>= 31    11111111111111111111111111111111
    i>>=1      11111111111111111111111111111111

  • Bitwise XOR Operator

    I am going through a certification book, that I have found to have errors. I am not real familiar with bitwise operations, but I have come across one that I think is in error. Can someone confirm or deny whether the following is correct?
    00001010
    ^ 00001000
    00001000
    Thanks

    The problem that I posted here was one of many I have found in the certifcation book called "All In One Java 2 Exam Guide". I see on Amazon.com that there is new version of this book being offered in October.
    I would recommend that you avoid this book and its new version. There are errors in text. There are errors in code examples. There are errors in questions. There are errors in the answers to the questions. As many reviewers on Amazon.com noted, this is one of the worst books on the market. There was obviously a rush to market. I can't imagine anyone with any technical knowledge proofread the book.
    God Bless America

  • Bitwise And operator

    If the binaryStrings of two ints "a" and "b" are different lengths will the & operator always return zero? i.e
    int a =8;
    int b =4;
    String binarya = "1000"
    String binaryb = "100"
    System.out.println(8,4); --> 0
    Thanks

    847102 wrote:
    Kayaman wrote:
    There is no "different length", the two parameters will always have the same length. Bitwise AND will work on bytes, shorts, ints and longs, meaning 8-bit, 16-bit, 32-bit or 64-bit "lengths".
    Each resulting bit is 1 if both input bits are 1, otherwise the resulting bit is 0.The answer 8 & 4 = 0Both are ints. Both are 32 bits.
       00000000 00000000 00000000 00001000  // 8
       00000000 00000000 00000000 00000100  // 4
       00000000 00000000 00000000 00000000
    1000
    100That's the same as if, in 2nd grade arithmetic, when you're adding 1000 + 100, you do
       1000
       100
    +_____
       2000Edited by: jverd on Apr 20, 2011 8:35 AM

Maybe you are looking for

  • Can I have more than one device active on my iTunes account?

    ON my iTunes account I currently have two iPhones and one iPod touch. Everything worked fine until a month ago when suddenly iMessage started sending incorrectly between the iPhones. We don't access iMessage on the iPod. It created a message thread o

  • Error in displaying  database content in choice

    Sir, The program is meant to show the contents of the the fields of the database file in choice control. But the data is not seen in the choice control //The program displays the data stored in the database file students.mdb. The //file contains the

  • Business Contact Manager - reports

    When I am making a report for project tasks the only column available for showing time used (actual work) show the time in the number of days. When working with tasks you put in number of hours. now if you have many short tasks that lasts for under 8

  • Query Related to Work Order

    Hello Experts, I am facing problem regarding costs in work order. After creating a work order of type PM02 we are not able to view the Plan Cost in "Costs" tab. This is happening from year 2012 as in previous orders we were able to view the costs in

  • Way to populate data for current quater months from last month of previous quater

    Hi  I have a requirement to load forecast data into the SSAS cube. My current data: Snapshot_M       Date         F_Quantities 1/1/2014        1/1/2014    100 2/1/2014        2/1/2014    90 3/1/2014        3/1/2014    80 4/1/2014        4/1/2014    2