Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question about octal (http://www.velocityreviews.com/forums/t741191-question-about-octal.html)

Mateusz_madi 12-31-2010 12:28 PM

Question about octal
 
Hi, can anybody explain why thist prog
#include<stdio.h>

int main()
{
int sx = -1;
unsigned ux = -1;
printf("%o\n", sx);
printf("%o\n", ux);
}
will print:

37777777777
37777777777

I mean how are values below zero evaluates in octal format?

regards
Mateusz

Malcolm McLean 12-31-2010 12:46 PM

Re: Question about octal
 
On Dec 31, 2:28*pm, Mateusz_madi <madi.cz...@gmail.com> wrote:
> Hi, can anybody explain why thist prog
> #include<stdio.h>
>
> int main()
> {
> *int sx = -1;
> *unsigned ux = -1;
> *printf("%o\n", sx);
> *printf("%o\n", ux);}
>
> will print:
>
> 37777777777
> 37777777777
>
> I mean how are values below zero evaluates in octal format?
>

In a twos complement system, -1 is all bits set. (To substract you
invert, increment and add, discarding the overflow).

In octal 777777 repested is all bits set. The 3 is because the 3 bits
of octal don't make up a whole 32 bits.

unsigned ux = -1; should generate an error, and would in most
languages. In C it's allowed because it's an old language and people
were playing fast and loose with types and representations. It sets ux
to the same bit pattern as -1 in signed integers.

lawrence.jones@siemens.com 12-31-2010 07:01 PM

Re: Question about octal
 
Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:
>
> unsigned ux = -1; should generate an error, and would in most
> languages. In C it's allowed because it's an old language and people
> were playing fast and loose with types and representations. It sets ux
> to the same bit pattern as -1 in signed integers.


No, it doesn't. It sets it to the maximum possible value, which won't
be the same bit pattern as -1 in signed integers unless the system uses
2's complement notation. In C, unsigned numbers aren't just unsigned,
they're also modulo -- they're guaranteed to wrap around from the
largest possible value to zero and vice versa.
--
Larry Jones

Mom would be a lot more fun if she was a little more gullible. -- Calvin

osmium 12-31-2010 07:41 PM

Re: Question about octal
 
"Mateusz_madi" wrote:

> Hi, can anybody explain why thist prog
> #include<stdio.h>
>
> int main()
> {
> int sx = -1;
> unsigned ux = -1;
> printf("%o\n", sx);
> printf("%o\n", ux);
> }
> will print:
>
> 37777777777
> 37777777777
>
> I mean how are values below zero evaluates in octal format?


Think of octal as just an encoding, a shorthand way to represent binary
numbers. Don't think of it as a (the?) base 8 sytsem of numbers.



Ralf Damaschke 12-31-2010 07:51 PM

Re: Question about octal
 
China Blue Ribbon <chine.bleu@yahoo.com> wrote:

> The only place I see octal anymore are unix st_modes.


Don't tell me that current C programs don't use the constant 0 ;-)
cf. 6.4.4.1

-- Ralf

Keith Thompson 12-31-2010 10:24 PM

Re: Question about octal
 
Mateusz_madi <madi.czadi@gmail.com> writes:
> Hi, can anybody explain why thist prog
> #include<stdio.h>
>
> int main()
> {
> int sx = -1;
> unsigned ux = -1;
> printf("%o\n", sx);
> printf("%o\n", ux);
> }
> will print:
>
> 37777777777
> 37777777777
>
> I mean how are values below zero evaluates in octal format?


Strictly speaking, your program's behavior is undefined. (That
doesn't mean it's going to blow up, just that the C standard doesn't
say what it will do.) The "%o" format requires an unsigned int
argument; in your first printf call, your passing it a (signed) int.
This is ok *if* the signed int value is within the numeric range
representable as an unsigned int, but in this case it isn't.

In practice, what will almost certainly happen is that the
*representation* of the signed int value will be interpreted as if it
were an unsigned int. On a two's-complement system, (int)-1 has the
same bit pattern as (unsigned int)UINT_MAX, which apparently is 2**32-1
on your system (4294967295 decimal, 037777777777 octal).

Note that the declaration
unsigned ux = -1;
doesn't store a negative value in ux (it can't). Instead, the int
value -1 is converted from int to unsigned int; by the language's
conversion rules, the result is guaranteed to be UINT_MAX.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"

Seebs 01-01-2011 01:08 AM

Re: Question about octal
 
On 2010-12-31, Mateusz_madi <madi.czadi@gmail.com> wrote:
> Hi, can anybody explain why thist prog


Hi. I'd like to suggest that you proofread a little more carefully;
showing an interest in how you present your posts increases the willingness
of other people to answer your questions.

> int main()
> {
> int sx = -1;
> unsigned ux = -1;
> printf("%o\n", sx);
> printf("%o\n", ux);
> }


You may want to consider the implications of the fact that you used the
same format string both times. How would printf() know that it had two
different kinds of arguments? It wouldn't. %o tells it how to interpret
its input.

> I mean how are values below zero evaluates in octal format?


This is the wrong question. Octal isn't at issue; signed/unsigned is. You
appear to be on a machine using "twos complement" arithmetic (the most common
kind for most people today), on which the value -1 in a signed object, and
the value -1 gets converted to when converted to unsigned, have the same
representation, which is all-bits-1.

The octal specifier has nothing to do with values below zero, it's just
representing the bits you gave it. It doesn't care whether you think they're
below zero or not.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Alan Curry 01-01-2011 08:09 AM

Re: Question about octal
 
In article <3617c77b-34eb-4734-aa2c-ecd5e7f2a01c@j29g2000yqm.googlegroups.com>,
Mateusz_madi <madi.czadi@gmail.com> wrote:
>Hi, can anybody explain why thist prog
>#include<stdio.h>
>
>int main()
>{
> int sx = -1;
> unsigned ux = -1;
> printf("%o\n", sx);
> printf("%o\n", ux);
>}


Answered well enough already, but I'd like to add this:

There would have been less chance for confusion here if printf had
included separate specifiers for printing negative values in hex and
octal, just like it has %d and %u for decimal. If you want to print the
value -255 as "-FF", printf doesn't make it easy.

Base and signedness are orthogonal, printf doesn't treat them that way.

%u should have been %ud (u as a modifier, not a primary). %uo, %ux,
and %uX would have followed naturally.

--
Alan Curry

Mateusz_madi 01-03-2011 08:26 AM

Re: Question about octal
 
I don't know if I understood weel, so if i say:
int sx = -1;
unsigned ux = -1;

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?




Hans Vlems 01-03-2011 01:47 PM

Re: Question about octal
 
On 3 jan, 09:26, Mateusz_madi <madi.cz...@gmail.com> wrote:
> I don't know if I understood weel, so if i say:
> int sx = -1;
> unsigned ux = -1;
>
> 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?


Perhaps. Try to predict the output of this:

#include<stdio.h>

int main(void)
{
int sx;
for (sx=-10;sx<12;sx+=1)
printf("%d %u %o %x\n",sx,(unsigned) sx,sx,sx);
return 0;
}

Again, try to predict the output and don't look at the C coding style.

Hans


All times are GMT. The time now is 08:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.