Results 1 to 6 of 6
  1. #1
    KentDMc is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Unfamiliar Syntax "int x = i1 << 24 | i2 ..."

    Hi folks,

    I'm coming accross a syntax I'm not familiar with and am unable to Google it because of the characters involved...

    Here is an example.

    Java Code:
    int i1, i2, i3, i4;
    
    //set the ints to some values
    
    int x = i1 << 24 | i2 << 16 | i3 << 8 | i4;
    I don't understand what that last line is doing. Can anyone explain this?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In your expression you have three operators, =, << and |. I guess did you know the meaning of each of these.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    i believe the << is a bit operator meaning shift left and the | works the same as || but for bit manipulation.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I was correct. Read up here Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    the << is the Signed left shift and the | is a bitwise inclusive or. the following truth table explains how your 4 parameters would end up true

    p1|p2|p3|p4|'|'|
    0 | 0| 0 | 1 |1
    0 | 0| 1 | 0 |1
    0 | 1| 0 | 0 |1
    1 | 0| 0 | 0 |1

    any other combination will return false unless you place parentheses around your operators.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    KentDMc is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default SOLVED - Thanks

    Thank you for your responses, guys. When I first looked at this I thought it was some kinda of logical shortcut, similar to max = (a > b) ? a : b;

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 12:19 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •