# Thread: anyone help with this shift bit question

1. ## 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. 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

3. 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.

4. ((~0UL) << nbits_of_input_number) | input_number
should work

5. 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.

6. 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!

7. how do you know that 11111111 is 8-bit value representing -1 and not 16-bit value representing 255?

8. 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. In other words, you want to implement sign extension?
http://en.wikipedia.org/wiki/Sign_extension

10. And If I enter 1 - it means one bit value it should be converted to -1, right?

11. 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.

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

13. 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

Popular pages Recent additions