Results 1 to 6 of 6
- 04-09-2009, 04:43 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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.
I don't understand what that last line is doing. Can anyone explain this?Java Code:int i1, i2, i3, i4; //set the ints to some values int x = i1 << 24 | i2 << 16 | i3 << 8 | i4;
- 04-09-2009, 04:51 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In your expression you have three operators, =, << and |. I guess did you know the meaning of each of these.
- 04-09-2009, 05:28 PM #3
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.
- 04-09-2009, 06:30 PM #4
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.
- 04-10-2009, 03:23 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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;
- 04-11-2009, 03:21 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually this is about ternary operator, simplest form of if-else condition. Logical operators are totally different from this.
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Syntax error on token "(", ; expected
By baltimore in forum AWT / SwingReplies: 3Last Post: 10-28-2009, 12:19 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks