Thread: Logical Right Shift

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Logical Right Shift

    Hey people,

    I'm working on an assignment that requires me to right-shift a 32-bit integer with 0s rather than the most significant bit(which is what C does by default). I'm allowed to use binary operators &, ^, ~, | and logical operators +, ! and =. No if statements, no macros, nothing. Also, any integer that is declared should be less than or equal to 0xFF.

    Any help will be appreciated!
    Thanks!

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take a look at Prelude's Bit Manipulation Tutorial and then post the code you have and we can help.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The easy way is to cast to unsigned, do the shift and then cast back again.
    An unsigned shift to the right zero-fills the evacuated bits.
    If you can't do that then just figure out how to make the mask value to 'and' the result with to cancel out the leading ones that get introduced by the shift.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    @iMalc - I figured out the algorithm in pseudo-code. Here is how it goes:

    if the int is shifted by n bits, declare an int with n 1s as the least significant bits (ex: if n = 3, the declared int would be 00000..00111).
    then shift the declared int by (32-n) which will make shift the 1s to the most significant bits positions (now 1110000..0000).
    then just perform a bitwise "&" on the original int and the declared int.

    But the problem I'm having is how to declare an int with n 1s in the least significant bit positions (keeping in mind of course that I can't declare an int greater than 0xFF)...

    If I can get a hint on how to do that, I think I can take it from there..

    Thanks!

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Pseudo Code

    @iMalc - I figured out the algorithm in pseudo-code. Here is how it goes:

    if the int is shifted by n bits, declare an int with n 1s as the least significant bits (ex: if n = 3, the declared int would be 00000..00111).
    then shift the declared int by (32-n) which will make shift the 1s to the most significant bits positions (now 1110000..0000).
    then just perform a bitwise "&" on the original int and the declared int.

    But the problem I'm having is how to declare an int with n 1s in the least significant bit positions (keeping in mind of course that I can't declare an int greater than 0xFF)...

    If I can get a hint on how to do that, I think I can take it from there..

    Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You haven't stated how many times do you need to right shift the 32 bit integer by?
    If int <= 0xff, then right shifting will push 0's not 1's from the left end as the MSB==0.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    What do you get when you shift 1 left by a certain number of bits? Look for a way to change from this value to one which sets the 1 to a 0 and all the 0s below it to 1s, which is what your mask has to be. If you need help, write out binary numbers from decimal 1 to 16 and notice what's just before or after the values which are all 1s in the LSBs.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by itCbitC View Post
    You haven't stated how many times do you need to right shift the 32 bit integer by?
    If int <= 0xff, then right shifting will push 0's not 1's from the left end as the MSB==0.
    As I read it, he's limited to using 0xFF or less in code he writes, but the input can be any signed 32-bit value?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by KCfromNC View Post
    As I read it, he's limited to using 0xFF or less in code he writes, but the input can be any signed 32-bit value?
    Yep! and thanks for clearing the mists swirling in my head because any other way wouldn't make sense.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Think about what you get if you take a number with lots of consecutive bits set and then add 1. Does such a number look easier to produce? You can then subtract 1 and you get a nice mask.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > requires me to right-shift a 32-bit integer with 0s rather than the most significant bit, which is what C does by default
    You got this wrong.
    right-shift of an unsigned value always inserts zeros.
    right-shift of a signed value MAY shift in zeros, or MAY shift in the sign bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logical not !7 logical whaaaa?
    By Charbot in forum C Programming
    Replies: 2
    Last Post: 03-22-2011, 07:19 PM
  2. Logical! Logical! Why do you have to be so Logical?
    By g8ortech14 in forum C Programming
    Replies: 24
    Last Post: 09-20-2010, 02:40 PM
  3. Bit shift
    By nenpa8lo in forum C Programming
    Replies: 8
    Last Post: 09-03-2008, 07:30 AM
  4. arithmatic vs logical shift operation
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 02-21-2008, 04:21 AM
  5. Shift Key
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-31-2002, 05:47 PM

Tags for this Thread