Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Complement of unsigned char

Reply
Thread Tools

Complement of unsigned char

 
 
omisols@gmail.com
Guest
Posts: n/a
 
      03-28-2006
Gurus -

Can anyone explain the behavior here?

#include <stdio.h>
#include <stdlib.h>

int main ()
{
unsigned char c = 0;

printf (" c = %u, ~c = %u \n", c, ~c);

return 0;
}

The output for the above chunk of code (when run on a linux box with
gcc) is:
c = 0, ~c = 4294967295

The value for ~c is (2 ^ 32) - 1.

Isn't an unsigned char 8 bits long?
If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

I'm confused.

Thanks,
OmiSols

 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      03-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Gurus -
>
> Can anyone explain the behavior here?
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main ()
> {
> unsigned char c = 0;
>
> printf (" c = %u, ~c = %u \n", c, ~c);
>
> return 0;
> }
>
> The output for the above chunk of code (when run on a linux box with
> gcc) is:
> c = 0, ~c = 4294967295
>
> The value for ~c is (2 ^ 32) - 1.
>
> Isn't an unsigned char 8 bits long?
> If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?
>
> I'm confused.
>
> Thanks,
> OmiSols
>

You've run afoul of promotions. Look 'em up in your C book. In the
context of the printf function, the expression c yields an int value.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
 
omisols@gmail.com
Guest
Posts: n/a
 
      03-28-2006
Thanks Joe!

I casted it explicitly and it worked as expected.

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      03-29-2006
# The output for the above chunk of code (when run on a linux box with
# gcc) is:
# c = 0, ~c = 4294967295
#
# The value for ~c is (2 ^ 32) - 1.
#
# Isn't an unsigned char 8 bits long?

Not necessarily.

# If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

The (unsigned char) is converted to (unsigned int) when it shows up
in an expression.


#include <stdio.h>

int main(int N,char **P) {
unsigned char x,y;
x = 0xE1; y = ~x;
printf("%u %u %u %u %u %u\n",x,x|0,y,y|0,~x,~x|0);
return 0;
}


@ cc x.c; a.out
225 225 30 30 4294967070 4294967070

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2006
SM Ryan <(E-Mail Removed)> writes:
[...]
> The (unsigned char) is converted to (unsigned int) when it shows up
> in an expression.


If int can represent all the values of unsigned char (as it can on
most systems), the unsigned char value is promoted to (signed) int.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      03-29-2006
(E-Mail Removed) wrote:
> I casted it explicitly and it worked as expected.


What did you cast to have it work as expected?
What were the expectations? What was the previous error?

Please quote context in your followups.
Remember to keep attribution lines (the "somebody wrote:" lines) and
trim text not relevant to your followup.
Also read the link in my signature.


If you have a C99 compiler, you can use the "hh" length modifier for
the printf() conversion specification:

#include <stdio.h>
/* #include <stdlib.h> */

int main (void)
{
unsigned char c = 0;

printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
return 0;
}


--
If you're posting through Google read <http://cfaj.freeshell.org/google>
 
Reply With Quote
 
omisols@gmail.com
Guest
Posts: n/a
 
      03-29-2006
> (E-Mail Removed) wrote:
> > I casted it explicitly and it worked as expected.

>
> What did you cast to have it work as expected?

printf (" c = %u, ~c = %u \n", (unsigned char) c, (unsigned char)
(~c)); worked. And by worked I mean, I wanted to see 255 printed and
the above statement did that.

> What were the expectations? What was the previous error?

I wanted to see 255 printed, however I was seeing 4294967295 printed
instead.

>
> Please quote context in your followups.
> Remember to keep attribution lines (the "somebody wrote:" lines) and
> trim text not relevant to your followup.
> Also read the link in my signature.

Will do that from now on.

>
> If you have a C99 compiler, you can use the "hh" length modifier for
> the printf() conversion specification:
>
> #include <stdio.h>
> /* #include <stdlib.h> */
>
> int main (void)
> {
> unsigned char c = 0;
>
> printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
> return 0;
> }

Didn't try this yet. But am sure it'll work.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2006
(E-Mail Removed) writes:
[...]
>> Please quote context in your followups.
>> Remember to keep attribution lines (the "somebody wrote:" lines) and
>> trim text not relevant to your followup.
>> Also read the link in my signature.

> Will do that from now on.


The article you're replying to was written by Pedro Graca, but I can't
tell that by reading your followup. Please don't delete attribution
lines (e.g., the "(E-Mail Removed) writes:" line above).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
one's complement of unsigned char subramanian C Programming 6 12-30-2006 08:37 AM
1's complement and 2's complement sarathy C++ 22 08-02-2006 05:53 PM
1's complement and 2's complement sarathy C Programming 20 08-02-2006 05:53 PM
sign magnitude, ones complement, two's complement Mantorok Redgormor C Programming 8 10-07-2003 11:52 PM



Advertisments