Thread: anyone help with this shift bit question

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    anyone help with this shift bit question

    Hello.

    User enters 11111111 into my convert program and wants it to be converted to its
    signed decimal representation (-1 will be output on stdout)

    I read that string above into an unsigned int (32-bit on my comp) with strtoul()

    strtoul puts it in the variable like this:

    00000000 00000000 00000000 11111111 <--------- 255

    I want to turn all and ONLY bits above the most significant to 1 to turn that variable into
    ULONG_MAX. Then I will use printf to print it as -1 to stdout (what user expected in this
    case).

    Question is --> Is there a shift operation to do this in C? I know there's a shift
    operator, but can it just set the bits above the most significant?

    Thanks alot

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I cannot see the logic here, really...

    If I enter 1 the most significan bit will be the first one... And you want to change all other bits to 1 and converting my input to -1? Why?

    If you want to give the possibility to enter 8-bit value and unsigned and then convert it to 8-bit signed value - use char and not unsigned int... If you want to ask user the number of bits in his value - you need some additional input based on it - you can use some masking and bit-operatons to convert unsigned to signed...

    But as you describe it - it has no meaning for me
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    If your enter 1 and want it converted to its binary representation here's how my prog works

    You enter ---> cvt -d 1 -b (convert the decimal number 1 to binary)
    My program can print either 1 or 00000001, not -1.

    If you enter cvt -h ffffffff -d my program spits out -1


    cvt -uh 11111111 -d (convert the text string 11111111 as an unsigned value to decimal)

    program spits out ---> 255

    cvt -d 255 -h ------> program spits out FF

    So when a user enters 11111111 and thinks of it as signed, my program has to
    print -1.


    Last edited by movl0x1; 05-17-2007 at 12:50 PM.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    ((~0UL) << nbits_of_input_number) | input_number
    should work

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    But ffffffff isn't the same value as 11111111. Shouldn't it just spit out -1 if they enter 32 1's (assuming you're limiting your program to 32-bit numbers)? I mean, to me, if I saw ffffffff spit out -1 when I used your program, I'd expect 11111111 to spit out 255 and 11111111111111111111111111111111 to spit out -1.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    11111111 is either 255 unsigned or -1 signed


    It can be 8-bits, 16 bits, 32 bits, 64 bits, etc. If you have a 1 in the most significant bit, that number can be negative.

    Thanks OnionKnight, I'll try that!
    Last edited by movl0x1; 05-17-2007 at 01:25 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do you know that 11111111 is 8-bit value representing -1 and not 16-bit value representing 255?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    The way the program works is that the length of the string that is
    entered(in this case 8 bit - 11111111) is to be considered the size
    of the variable that the user wants converted to whatever other
    number base. The user specifies whether he/she wants the
    string to be converted to its signed or unsigned representation.
    So if you have an 8-bit variable with all HIGH bits and user wants
    it converted to its signed representation it prints -1, otherwise it
    prints 255.

    If I wanted to convert a 16-bit variable to it's signed value,
    and I input 11111111 11111111, then my program would print
    -1.

    If I enter the binary ascii string 00000000 11111111, and
    want it converted to decimal ascii string on stdout, it's gonna
    print 255.


  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In other words, you want to implement sign extension?
    http://en.wikipedia.org/wiki/Sign_extension
    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.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And If I enter 1 - it means one bit value it should be converted to -1, right?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    No, if you enter 1 it means 1 and converted to binary or decimal is 1.

    if you enter 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000001 it will be 1


    If you enter 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111 it can be converted to
    negative or positive.


    Thanks Salem Yes, sign extension is what I'll have to implement in C. I'll have to figure out how.
    Last edited by movl0x1; 05-18-2007 at 10:45 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well given something like 0000000011111111, you need to figure out some way of telling whether is really a signed 8-bit number (which needs sign extending), or whether it is an unsigned 8-bit number (it doesn't), or a 16-bit number (it doesn't either).
    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.

  13. #13
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    If I have a 16-bit register inside a CPU and it holds 0000000011111111, and I ask you
    to convert that to its decimal representation, what would you say? 255, right?

    That's what my program will do.

    No need to figure out if 0000000011111111 is signed or unsigned or 8 bits, because
    the user entered the ascii representation of 16 bits, and using that info I print out 255.
    Convert that to hexadecimal and you get 00FF or simply FF. That's what my program does. It's simple.


    Anyways, enough on bits and bytes, I'm goin bonkers!

    PEACE ALL
    Last edited by movl0x1; 05-18-2007 at 11:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 07:15 AM
  2. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  5. quick question about bit manipulation
    By PJYelton in forum C++ Programming
    Replies: 7
    Last Post: 01-10-2003, 01:18 AM