Solved

what actually happens when a character c is assigned a value of 164?

Posted on 2003-11-05
7
225 Views
Last Modified: 2010-04-15
Hello,

what happens when the folloowing:

char x = 64;
x+=100;
cout << (int)c ;

it prints -92
but the question is how ? how does it work internally? how does it arrive at -92?

thanks and regards
Raghu
0
Comment
Question by:rags2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9688536
Replace char to unsigned char.
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9688750
Two's complement arithmetic.

For example (or use google):
http://www.hal-pc.org/~clyndes/computer-arithmetic/twoscomplement.html
0
 

Accepted Solution

by:
linuxsub earned 20 total points
ID: 9688897
x becomes 164.
164==0xA4==10100100B
In 2' s complement, it becomes:
10100100B=(-01011011B - 1) = -92

The way 2's complement works for signed values is, if the Most significant bit is 1, sign becomes negative(-), and get the absolute value by calculating  bitwise NOT of the current value and adding one to it (since there is nothing called -0).

As AlexFM says, using unsigned char should give you 164.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:imladris
ID: 9690519
What you need to note is that char is a one byte quantity. In 1 byte (8 bits) you can count from 0 through 255 if you consider it unsigned. But if you consider it signed, you can count from -128 to 127. So when you added 100 to x it became 0xA4, as linuxsub indicates. So this was now a 1 byte quantity with a decimal value of -92, that is, it had overflowed, because a 1 byte signed quantity cannot represent 164. *After* that, you cast it to an int. An int is also signed, and so the cast will "propogate" the sign bit during the conversion (in order to preserve the value; it was -92 before the cast, and this way it will also be -92 after the cast). Assuming a 4 byte int, you would get 0xFFFFFFA4.

You can solve this, as indicated, by using an unsigned char. That will allow you to represent 0 through 255.

More messily, you could also, of course force it to do what you want by masking off the top bits:

cout<<((int)c&0xff;

0
 
LVL 6

Expert Comment

by:Ajar
ID: 9692753
char can store values between  -128  to +127  and
164  = 10100100    in binary
The left most bit the sign bit is turned on meaning the number is negative
the rest of the number is   0100100  
now as the number  is negative so its stored as two's compilent  so its value becomes

 ~ 0100100] =>   1011011  +1  =>        1011100  
=>  -92
0
 
LVL 16

Expert Comment

by:imladris
ID: 9702235
Did any of those answers help?

If so, it is now time to choose and answer and grade it.

If not, perhaps a clarifying question would help.
0
 

Author Comment

by:rags2k
ID: 9703641
All answers helped me but the first and most useful one tome was one by linuxsub..bu i am surprised that I am not allowed to accept/rate all answers on their merits...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 111
How to install SVN Command Line Client? 5 131
I want to transfer a file from source folder to destination folder. 35 70
keep track of class structure 1 53
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question