Converting negative integer to octal/hexadecimal

J

jaks.maths

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??
 
B

bert

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

+568 = hex ...0000238 = 2 * 16^2 + 3 * 16 + 8
-568 = hex ...FFFFDC8, verify the sum is zero.

+568 = oct ...0001070 = 8^3 + 7 * 8
-568 = oct ...7776710, verify the sum is zero.
--
 
V

Vladimir Oka

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

And how is this a C question?

Even if it were, you were not precise enough. Do you mean 1's or 2's
complement, sign-plus-absolute value? How many bits wide your numbers
are?
From where I sit, the following program:

#include <stdio.h>

int main( void )
{
int i = -568;

printf("i = %x\n", i);

return 0;
}

produces the following:

i = fffffdc8

[FYI: that's 32 bit 2's complement. YMMV.]
 
O

osmium

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

Convert is not quite the right word. As used in the industry, octal and hex
are *representations* of numbers. Choose your number and a coding
technique, Write it (perhaps on paper) in binary and then octal and hex are
shortcut ways of communicating, to people mostly, the binary number you are
speaking of. There are at least three ways of representing negative
numbers: one complement, twos complement and sign and magnitude (using a
"sign box"). They would all be different for your sample number.

If that bothers you, think of the difference between a thing and a picture
of that thing.
 
E

Eric Sosman

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

Hexadecimal: -0x238
Octal: -01070

(These are not flippant answers. Pay no attention to
offered "answers" like 0xFFFFFDC8 or 037777776710, because
they merely perpetuate the confusion between representation
and value.)
 
V

Vladimir Oka

Eric said:
Hexadecimal: -0x238
Octal: -01070

(These are not flippant answers. Pay no attention to
offered "answers" like 0xFFFFFDC8 or 037777776710, because
they merely perpetuate the confusion between representation
and value.)

I guess the obviousness of the above correct answer leads to reading
into the question something that (possibly) wasn't there in the first
place. (Have the standards of teaching Maths declined that much?) To me
at least, the intent of the original question is not 100% clear.
 
E

Eric Sosman

Vladimir Oka wrote On 06/06/06 10:36,:
I guess the obviousness of the above correct answer leads to reading
into the question something that (possibly) wasn't there in the first
place. (Have the standards of teaching Maths declined that much?) To me
at least, the intent of the original question is not 100% clear.

I think the question is clear (of course, I may be
mistaken!) but misguided. C programmers seem inclined
to worry about representations when they ought to be
thinking about values; that leads to "mysteries" like

short s = 0xFFFF;
assert (s == 0xFFFF); /* why does it abort? */

There are, of course, times when the programmer must
think about representations. When you're converting
between an internal form and an externally-imposed form,
representations often (not always!) come into play. When
you're treating a data object like a bunch of bit fields
rather than like a number, you're entirely right to be
thinking about representations. But when you're dealing
with numbers as counts and quantities and indices and the
like, think about their values and properties and not about
their implementations. Much grief is thereby avoided.
 
M

Martin Ambuhl

How to convert negative integer to hexadecimal or octal number?

You don't convert values, but external representations.
Ex: -568
What is the equivalent hexadecimal and octal number??


#include <stdio.h>

void shownum(int x)
{
unsigned v, s = 0;
s = (x < 0);
v = (x < 0) ? -x : x;
printf("%d (decimal) is %s%#x (hex) and %s%#o (octal).\n",
x, s ? "-" : "", v, s ? "-" : "", v);
}

int main(void)
{
shownum(-568);
shownum(568);
return 0;
}


-568 (decimal) is -0x238 (hex) and -01070 (octal).
568 (decimal) is 0x238 (hex) and 01070 (octal).
 
C

CBFalconer

bert said:
+568 = hex ...0000238 = 2 * 16^2 + 3 * 16 + 8
-568 = hex ...FFFFDC8, verify the sum is zero.

+568 = oct ...0001070 = 8^3 + 7 * 8
-568 = oct ...7776710, verify the sum is zero.

That assumes 2's complement arithmetic. While common, that is not
guaranteed. 1's complement and sign magnitude are also possible,
when the results will be different.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
O

Old Wolf

Vladimir said:
From where I sit, the following program:

#include <stdio.h>

int main( void )
{
int i = -568;
printf("i = %x\n", i);
return 0;
}

produces the following:

i = fffffdc8

[FYI: that's 32 bit 2's complement. YMMV.]

YMMV indeed because the program causes undefined behaviour:
the parameter corresponding to %x must be an unsigned int
(or, arguably, an int with a non-negative value).

You could replace the line with
printf( "i = %x\n", (unsigned)i );

and then it is clear that i is not fffffdc8; it is a different
number that i has been converted to.
 
V

Vladimir Oka

Old said:
Vladimir said:
From where I sit, the following program:

#include <stdio.h>

int main( void )
{
int i = -568;
printf("i = %x\n", i);
return 0;
}

produces the following:

i = fffffdc8

[FYI: that's 32 bit 2's complement. YMMV.]

YMMV indeed because the program causes undefined behaviour:
the parameter corresponding to %x must be an unsigned int
(or, arguably, an int with a non-negative value).

You could replace the line with
printf( "i = %x\n", (unsigned)i );

and then it is clear that i is not fffffdc8; it is a different
number that i has been converted to.

You are right, of course. I was careless.

(I do not use printf/scanf family in day to day work.)
 
J

jaks.maths

My question is to get cleared about the following.

Can we have negative octal numbers and hex decimal numbers, Please
confirm?
 
R

Richard Heathfield

(e-mail address removed) said:
My question is to get cleared about the following.

Can we have negative octal numbers and hex decimal numbers, Please
confirm?

There is no such thing as an octal number, a hex number, a decimal number,
or a hex decimal number. There are just numbers. Numbers can be negative.
Octal, decimal, and hexadecimal are not number systems. They are number
*representation* systems.

Whether a C integer type can store a negative integer depends on whether it
is a signed type. Thus:

unsigned int i = -0xF; /* i does not store a negative value,
despite the - sign */
int j = -0xF; /* j stores a negative value. */
 
S

spibou

My question is to get cleared about the following.

Can we have negative octal numbers and hex decimal numbers, Please
confirm?

Can we have them in what context ? In the context or programming ?
In the context of a mathematics book ? In the context of a money-in
money-out list ?

As others have said things like octal or hexadecimal or decimal have
to do with how to represent a number. As with any other form of
representation they ultimately depend on established convention.

Anyway to try and give you some sort of an answer , in every context
I can think of , adding a minus sign in front of a hexadecimal or octal
representation of a number will represent a negative number assuming
that a decimal representation of the same number would be legal in that
same context.

For example if it is legal in some context to write -255 then it will
probably
also be legal to write -FF or -377 perhaps with some additional
notation
to signify that what follows the minus sign is a hex or octal number.

This reply has come out more convoluted than what I was hoping
but the question isn't very clear either.

Spiros Bousbouras
 
C

Chris Dollin

My question is to get cleared about the following.

Can we have negative octal numbers and hex decimal numbers, Please
confirm?

There are no "octal numbers" or "hexadecimal numbers". There are
numbers /written in/ octal and hexadecimal, just as there are
numbers /written in/ decimal.

A lot of the time - such as in casual conversational use - the distinction
between decimal /numerals/ and the /numbers/ they represent doesn't
much matter; but programming isn't one of those times.

You can write some decimal numeral, eg 1066. This represents a positive
number. You can write some hexadecimal number, eg 0xffff. This also
represents some positive number. You /can't/ write a decimal numeral
that represents a negative number, and you /can't/ represent a
hexadecimal number that represents a negative number either.

What you /can/ write is a numeral that expresses a number which cannot
be expressed in the number of bits allocated for its representation
by its context. For example, when you write 12345678910111213141516,
its value typically cannot be represented as a C int; similarly
0xffffffff cannot be represented as a C int if the implementation
uses 32-bit ints.

Various things are permitted to happen, and I don't offhand know
the details, but one outcome that /may/ happen is that the bits of
the /binary/ numeral that represents the value are stuffed willy-nilly
into the representation, and a bit that was supposed to represent a
big and positive value occupies the slot used for a big and /negative/
value, so that this twos-complement binary numeral represents a
negative number.

So a numeral 0xffffffff may end up being a bit-pattern that represents
a negative number in your implementation. It's not the hexadecimal
number that's negative: it's the surviving bitsvalue after its been
pintpotted into the available space.

Just as on a 16-bit-int implementation, the numeral 40000 may end
up as a bitpattern the machine thinks represents a negative number.
This doesn't mean that (the value represented by the decimal number) 40000
is negative.
 
E

Eric Sosman

My question is to get cleared about the following.

Can we have negative octal numbers and hex decimal numbers, Please
confirm?

In C source, all integer constants expressed in decimal,
octal, or hexadecimal are non-negative. There is no way to
write a negative number in any of these bases.

You can, however, combine constants with operators to
form expressions with negative values. One easy way of doing
this is to use the `-' operator as a prefix, as in `-42' or
`-052' or `-0x2A'. These are not negative integer constants,
but expressions each consisting of a positive integer constant
and a `-' operator that negates its value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top