Snylt Master

Ranch Hand

Posts: 55

posted 15 years ago

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).]

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).]

Preparing for the Java 2 Certification exam

SandeepKumar

Greenhorn

Posts: 3

posted 15 years ago

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

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

Posts: 55

Dave Vick

Ranch Hand

Posts: 3244

posted 15 years ago

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

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