Velocity Reviews > Question about octal

Guest
Posts: n/a

 01-03-2011
I think i got it, but to be shure, let's say i've got:
int x = -10;
printf("%d %u %o %x\n", x , (unsigned) x, x , x);

-1 is binary 111111....1111

It will evaluate:
1) %d - x -> just -10
2) %u -> (unsigned)x -> %u -> 111111...1111(unsigned, there is no inf
to tread this 1111...1111 as -1 because it beacme unsigned, so) -> %u
- (2^32-10)
3) %o - x -> %o - 1111...1111 (hidden become unsigned) -> %o -
111111...111 -> %o - 111111...1111 0110 (octally number is 3 bit so) -
> %o - 377...766

4) %x - x -> %x - 1111...1111 (hidden become unsigned) -> %x -
11111...1111 -> %x - 111111...1111 0110 (hex takes 4 bits so) -> %x -
FFF...F6

Isn't it?

regards, mateusz

Keith Thompson
Guest
Posts: n/a

 01-03-2011
> I don't know if I understood weel, so if i say:
> int sx = -1;

This initializes sx to -1.

> unsigned ux = -1;

This initializes ux to UINT_MAX, the result of converting -1 from
int to unsigned int.

> and hav a machine using twos complement arithmetic, values goes:
> printf("%o\n", sx) -> 11111111...1111 -> 3FFF...FF
> and
> printf("%o\n", sx)->printf("%o\n", ux) -> 11111111...1111 ->
> 3FFF...FF
>
> Ism't it?

Maybe.

sx is a signed int; "%o" requires an unsigned int. You can pass a
signed int where an unsigned int is expected, or vice versa, *only*
if the value is within the range of representable by both types
(0 .. INT_MAX); otherwise, the behavior is undefined.

In principle, your program (or rather, a complete program that
includes your code fragments) can do literally anything.

In practice, printf will almost certainly interpret the
representation of sx (a signed int) as if it were an object of type
unsigned int, and on a 2's-complement system it will probably behave
as you expect (except that the output will be octal, not hexadecimal
as you've shown).

For example, on my system, this program:

#include <stdio.h>

int main(void)
{
int sx = -1;
unsigned int ux = -1;

printf("ux = %o (undefined behavior) \n", ux);
printf("sx = %o\n", sx);
return 0;
}

produces this output:

ux = 37777777777 (undefined behavior)
sx = 37777777777

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Francois Grieu
Guest
Posts: n/a

 01-03-2011

> I think i got it, but to be shure, let's say i've got:
> int x = -10;
> printf("%d %u %o %x\n", x , (unsigned) x, x , x);

%u %o and %x all expect unsigned int arguments. Thus either
your (unsigned) x could be x, or you missed two (unsigned).
I suspect the later, and that it could make a difference on
systems not using 2's complement (I won't bet a hand at that).

> -1 is binary 111111....1111

for some creative definition of "is", including use of 2's complement.

> It will evaluate:
> 1) %d - x -> just -10

Yes.

> 2) %u -> (unsigned)x -> %u -> 111111...1111(unsigned, there is no inf
> to tread this 1111...1111 as -1 because it became unsigned, so) -> %u
> -> (2^32-10)

I suspect that the decimal representation for UINT_MAX-9 will be output;
this may or may not be 4294967286.

> 3) %o - x -> %o - 1111...1111 (hidden become unsigned) -> %o
> -> 111111...111 -> %o - 111111...1111 0110 (octal number is 3 bit so)
> -> %o - 377...766
>
> 4) %x - x -> %x - 1111...1111 (hidden become unsigned) -> %x ->
> 11111...1111 -> %x - 111111...1111 0110 (hex takes 4 bits so) -> %x
> -> FFF...F6
>
> Isn't it?

I suspect the output for these is even harder to predict. Looks like
it depends in the number of bits in int; and the internal representation
of negative numbers, which is fully undefined under C89 (C99 allows three
options).

Francois Grieu

Hans Vlems
Guest
Posts: n/a

 01-04-2011
On 3 jan, 15:57, Mateusz_madi <(E-Mail Removed)> wrote:
> I think i got it, but to be shure, let's say i've got:
> int x = -10;
> printf("%d %u %o %x\n", x , (unsigned) x, x , x);
>
> -1 is binary 111111....1111
>
> It will evaluate:
> 1) %d - x -> just -10
> 2) %u -> (unsigned)x -> %u -> 111111...1111(unsigned, there is no inf
> to tread this 1111...1111 as -1 because it beacme unsigned, so) -> %u
> - (2^32-10)
> 3) %o - x -> %o - 1111...1111 (hidden become unsigned) -> %o -
> 111111...111 -> *%o - 111111...1111 0110 (octally number is 3 bit so) -> %o - 377...766
>
> 4) %x - x -> %x - 1111...1111 (hidden become unsigned) -> %x -
> 11111...1111 -> *%x - 111111...1111 0110 (hex takes 4 bits so) -> %x -
> FFF...F6
>
> Isn't it?
>
> regards, mateusz

Correct. The little exercise shows the difference between the contents
of a 32 bit binary value and a few possible representations. May be
incorrectly, I got the feeling that you were somewhat lost in that
respect.
Hans