Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: Operators Problem

  1. #1
    remmus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Operators Problem

    I have some experience in many programming languages,and I decided to learn Java...But,it has some new operators that i didn't see at other programming languages before...

    For example, the operators: "~","^","|","&".What does everyone mean??

    And,I'm not sure with the operators: >>,<<,>>>,<<<...I know what they do..But how do they help me???

    Thanks.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Operators Problem

    Quote Originally Posted by remmus View Post
    I have some experience in many programming languages,and I decided to learn Java...But,it has some new operators that i didn't see at other programming languages before...

    For example, the operators: "~","^","|","&".What does everyone mean??

    And,I'm not sure with the operators: >>,<<,>>>,<<<...I know what they do..But how do they help me???

    Thanks.
    Those are bitwise operators.
    Have a read about them here: Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    remmus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Operators Problem

    But how can they help me? (-.-) And,please explain a little bit better...I don't understand many on that site.

  4. #4
    remmus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Operators Problem

    When should i use these operators?? how do they help?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Operators Problem

    Quote Originally Posted by remmus View Post
    I have some experience in many programming languages,and I decided to learn Java...But,it has some new operators that i didn't see at other programming languages before...
    For example, the operators: "~","^","|","&".What does everyone mean??

    And,I'm not sure with the operators: >>,<<,>>>,<<<...I know what they do..But how do they help me???
    As PHQ stated, they are bit-wise operators and are based on their C-programming language equivalents (except for the >>> and <<< as far as I know), and so they are quite commonly used in many programming languages.

    Quote Originally Posted by remmus View Post
    But how can they help me? (-.-) And,please explain a little bit better...I don't understand many on that site.
    Rather than have us re-write a tutorial, perhaps you can tell us what specifically about the tutorial on the site linked to by PHQ confuses you.

    Much luck with your Java studies!

  6. #6
    remmus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Operators Problem

    But when should I use them??

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Operators Problem

    Quote Originally Posted by remmus View Post
    But when should I use them??
    Myself, I don't use them often, but I have used them when I needed to do bit manipulations when working with color bitmaps. A color may be represented as an int with the opacity in the highest order bit, the amount of red in the next order bit, the green in the next order bit, and the blue in the last bit. If you want to extract these pieces of information from the int, you may need to use these operators. I also use them when working with flags -- ints where each bit can be turned on or off and represents a piece of information. For instance when doing Swing coding and when using a MouseListener, the mouse data can be passed to my program from Java in the MouseEvent object via two ints, the modifiers int and the modifiersEx int, and each bit in the int tells me the state of the mouse (which button is involved, if the button was pressed or released) and also whether alt, ctrl, or shift have been pressed.

    There are many other uses for them, and if you code with Java, you will run into this, I can assure you, so they are very useful to learn.

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Operators Problem

    Best way to learn how they work is by experimenting with them, once you test a few values you will start to see some patterns.
    For example
    16 << 0 prints out 16,
    16 << 1 prints out 32,
    16 << 2 prints out 64,
    16 << 3 prints out 128
    Last edited by PhQ; 12-26-2012 at 12:06 AM.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Operators Problem

    Quote Originally Posted by Fubarable View Post
    As PHQ stated, they are bit-wise operators and are based on their C-programming language equivalents (except for the >>> and <<< as far as I know), and so they are quite commonly used in many programming languages.
    Just a nasty nitpick: there exists no <<< operator; a logical left-shift operator would equal an arithmetic left-shift (<<) b.t.w. BCPL already had those bit-operators and C 'borrowed' them from BCPL. C doesn't need the distinction between logical/arithmetic shift-right because it has unsigned ints. I still wonder why none of these languages have a 'rotate' operator ...

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. operators (/,*,+,-)
    By user07 in forum New To Java
    Replies: 7
    Last Post: 09-03-2012, 09:36 PM
  2. string operators
    By cagipple in forum New To Java
    Replies: 5
    Last Post: 04-17-2011, 01:04 AM
  3. New to operators, need some help...
    By Stryker4526 in forum New To Java
    Replies: 4
    Last Post: 09-26-2009, 01:29 AM
  4. Java Operators
    By Ash-infinity in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •