This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

Hello everyone! I need some help with bitpatterns. I've read a chapter about it in java 2 study guide and I've altso red cat and mice in campfire. I have some question though. This is how I think it is. When shifting >>> the bitpattern will be filled with 0. When shifting << the bitpattern will be filled with 0. When shifting >>, if the bittpattern is negativ it will be filled with 1 else it will be filled with 0. example 0000110 >> 2 gives 0000010 Please confirm or correct. If anyone can explain 64 or 32 bits then I will be happy. // Snylt Master

[This message has been edited by Snylt Master (edited July 09, 2001).]

Hi Master about your query, its not like sometime it filled by 0 sometime by 1 but >> and << operators work in similer fashion they shift the bits. like is the patter is 00100100 >> make it 00010010 and << make it 01001000 but as you must be knowing that the first bit always represent the sign so if you perform << on some number like 011100 it will become 111000 which is negative that surprize you some time so use <<< use operator which doesn't change sign bit. hope ur query is cleared

Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55

posted

0

Thanks for you comply but now i'm even more confused! Does even <<< shift exist? I thought there was << , >> , >>>. Or am I totally out of line here? // Snylt Master

Snylt there are 3 bit shift operators: << unsigned left shift, this shifts the bits left and fills in with 0's.<br /> >>> unsigned right shift, this shifts the bits right and fils in with 0's. >> signed right shift, this shifts the bits right and fills in with whatever the sign bit is. decimal 12 is 00001100 12 << 2 would give:<br /> 00001100 << 2 gives 00110000 which is 48.<br /> 12 >>> 2 would give:<br /> 00001100 >>> 2 gives 00000011 which is 3. 12 >> 2 would give: 00001100 >>> 2 gives 00000011 which is 3. There is no <<< because in the left shifts there is no need to worry about a sign bit.<br /> I don't know what you meant by explain 32 or 64 bits but it could be one of two things:<br /> 1. The fact that when you shift an int you are actually shifting the right hand operand % 32. So 26 >> 32 would have no effect on the value 26. On the same note shifting a long uses % 64 on the right hand operand. OR<br /> 2. You're refering to the fact that ints have 32 bits and longs have 64. This is important because you need to keep this in mind when you do the shifts look at this:<br /> -5 >>> 2 you might be tempted to write this as: 11111011 >>> 2 which gives 00111110 this would be 62, the only problem is that as a 32 bit number -5 in binary is really: 11111111 11111111 11111111 11111011 so the shift gives you 00111111 11111111 11111111 11111110 which is 1,073,741,822. I hope this helps you out, if not, repost, and I can clarify whatever you need. Dave

Dave

Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55

posted

0

Thanks Dave for a good explaination I got it now. // Snylt Master