Bitwise Operation

Discussion in 'Java' started by Pasquale Imbemba, May 6, 2004.

  1. Hello,

    in my little programm:

    public class Shift1 {
    public static void main (String [] args) {

    int thirteen = 13;
    int ten;

    ten = thirteen << 1;

    System.out.println ( thirteen + " is now " + ten);
    }
    }

    I expect 13 (1101) to be shifted by one position to the left, i.e.
    becoming 101. I expected Java to fill the righthand of the bit sequence
    with a 0, thus 1010, which should return me 10. It returns 6 instead.
    What I wrong here?

    Bye
    Pasquale

    --
    =======
    "I'd like to buy Windows." "Are you mad?" "Why, is this part of the
    license agreement"?
    =======
    Pasquale Imbemba, May 6, 2004
    #1
    1. Advertising

  2. Pasquale Imbemba wrote:
    > Hello,
    >
    > in my little programm:


    For beginner's questions, please consider comp.lang.java.help. F'up set.

    > I expect 13 (1101) to be shifted by one position to the left, i.e.
    > becoming 101.


    No, it becomes 26 (11010). As you wrote, shifted one position to the left.

    > I expected Java to fill the righthand of the bit sequence
    > with a 0,


    Yes, see above.

    > thus 1010,


    No, Java ints have space for 32 bits (31 plus one sign bit). The bit
    will drop of when you have reached that limit, not before. (you will see
    some strange results right before, when your bit hits the sign bit
    position. Your number will then suddenly presented as a negative number,
    but that's another issue).

    > which should return me 10. It returns 6 instead.


    I bet it returns 26, which is the correct answer.

    > What I wrong here?


    Your understanding of integer types and bit shifting in Java.

    To get your result you need to mask out the bits you want, effectively
    choping of the bits you don't want.

    ten = (thirteen << 1) & 0xF;
    /Thomas

    PS: Couldn't resist: Old programmer "joke": There are 10 types of people
    in the world. Those who understand binary, and those who don't :)
    Thomas Weidenfeller, May 6, 2004
    #2
    1. Advertising

  3. Pasquale Imbemba

    Roedy Green Guest

    On Thu, 06 May 2004 10:21:14 GMT, Pasquale Imbemba
    <> wrote or quoted :

    > int thirteen = 13;
    > int ten;
    >
    > ten = thirteen << 1;
    >
    > System.out.println ( thirteen + " is now " + ten);
    > }
    >}
    >
    >I expect 13 (1101) to be shifted by one position to the left, i.e.
    >becoming 101. I expected Java to fill the righthand of the bit sequence
    >with a 0, thus 1010, which should return me 10. It returns 6 instead.
    >What I wrong here?


    That is a LEFT shift, equivalent to multiplying by 2.
    thirteen is 1101 -> shifted left, with a 0 or the right is 11010
    which is 26.

    If you did a RIGHT shift:

    x = thirteen >> 1;
    you would get 1101 -> 110 with a 0 sign bit coming in the high end.


    Think of the little arrows pointing which way the bits are moving.

    see http://mindprod.com/jgloss/operator.html
    and chase links.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. biswaranjan.rath

    bitwise AND operation in xslt

    biswaranjan.rath, May 8, 2006, in forum: XML
    Replies:
    3
    Views:
    4,870
    shaunroe
    Nov 12, 2008
  2. Patrick Hoonhout

    bitwise operation...

    Patrick Hoonhout, Aug 27, 2003, in forum: C Programming
    Replies:
    13
    Views:
    658
    Alan Balmer
    Aug 28, 2003
  3. Magix

    Bitwise operation

    Magix, Jul 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    721
    Joe Wright
    Jul 31, 2004
  4. Magix

    Bitwise Operation

    Magix, Oct 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    548
    Dimension
    Oct 15, 2004
  5. Jerry

    Bitwise operation for division

    Jerry, Mar 2, 2005, in forum: C Programming
    Replies:
    7
    Views:
    8,198
    Chris Williams
    Mar 2, 2005
Loading...

Share This Page