473,473 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help! How to program about 2's complemet with 22 bits value?

Hi all,

If I have a variable and it's just have 22 bits,
and I must set this variable unsigned integer.
This 2's complement value maybe is negative.

For example:
A= 11 1111 1111 1110 1100 0000 = 4193984
3 f f e c 0
A's 2's complement should be
00 0000 0000 0001 0100 0000 = 320

I try to write a sample code below:
----------------------------------------------------
unsigned int A=4193984;
unsigned char Byte2=~(((A & 0x00ff0000) >> 16)+192); // 192 is sum of
the first 2 bits of one byte.
unsigned char Byte3=~((A & 0x0000ff00) >> 8);
unsigned char Byte4=~(A & 0x000000ff);
unsigned int ATwoSComplement=(Byte2 << 16) + (Byte3 << 8) + Byte4 + 1;
----------------------------------------------------
But when some variable's 2's complement is negative, this code fails.
How to find it's 2's complement value correctly?
Thanks for your help.

Regards,
cylin.
Jul 19 '05 #1
6 3476


cylin wrote:

Hi all,

If I have a variable and it's just have 22 bits,
and I must set this variable unsigned integer.
This 2's complement value maybe is negative.

For example:
A= 11 1111 1111 1110 1100 0000 = 4193984
3 f f e c 0
A's 2's complement should be
00 0000 0000 0001 0100 0000 = 320

I try to write a sample code below:
----------------------------------------------------
unsigned int A=4193984;
unsigned char Byte2=~(((A & 0x00ff0000) >> 16)+192); // 192 is sum of
the first 2 bits of one byte.
unsigned char Byte3=~((A & 0x0000ff00) >> 8);
unsigned char Byte4=~(A & 0x000000ff);
unsigned int ATwoSComplement=(Byte2 << 16) + (Byte3 << 8) + Byte4 + 1;
----------------------------------------------------
But when some variable's 2's complement is negative, this code fails.
How to find it's 2's complement value correctly?
Thanks for your help.


What you have looks too complicated. Also: I don't
understand what the addition of 192 should account for.

Can I assume that sizeof( unsigned int ) >= 4 ?
What's wrong with:

unsigned int ATwoSComplement = ( ~A + 1 ) & 0x003FFFFF;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

"cylin" <cy***@avant.com.tw> ¼¶¼g©ó¶l¥ó·s»D
:bn************@ID-154203.news.uni-berlin.de...
Hi all,

If I have a variable and it's just have 22 bits,
and I must set this variable unsigned integer.
This 2's complement value maybe is negative.

For example:
A= 11 1111 1111 1110 1100 0000 = 4193984
3 f f e c 0
A's 2's complement should be
00 0000 0000 0001 0100 0000 = 320

I try to write a sample code below:
----------------------------------------------------
unsigned int A=4193984;
// 192 is sum of the first 2 bits of one byte.
unsigned char Byte2=~(((A & 0x00ff0000) >> 16)+192);
unsigned char Byte3=~((A & 0x0000ff00) >> 8);
unsigned char Byte4=~(A & 0x000000ff);
// ATwoSComplement should be long integer,not unsigned integer
long ATwoSComplement=(Byte2 << 16) + (Byte3 << 8) + Byte4 + 1;
----------------------------------------------------
But when some variable's 2's complement is negative, this code fails.
How to find it's 2's complement value correctly?
Thanks for your help.

Regards,
cylin.


Jul 19 '05 #3
unsigned int ATwoSComplement = ( ~A + 1 ) & 0x003FFFFF;

Yes, that's simple.
But it seems that "( ~A + 1 ) & 0x003FFFFF" is always positive.
How can I handle if the 2's complement is negative?
Thanks.

Regards,
cylin.

Jul 19 '05 #4


cylin wrote:

unsigned int ATwoSComplement = ( ~A + 1 ) & 0x003FFFFF;

Yes, that's simple.
But it seems that "( ~A + 1 ) & 0x003FFFFF" is always positive.
How can I handle if the 2's complement is negative?


It is bit 21 which tells you if the number should be output
as a negative one.

bit 21 set -> negative number. output '-' and the 2-s complement of the number
bit 21 not set -> positive number, just output it.

So in your case:

22 1111 1111 1100 0000 0000
10 9876 5432 1098 7654 3210

A= 11 1111 1111 1110 1100 0000 = 4193984

since bit 21 is set, A holds a negative number.
But which one:

11 1111 1111 1110 1100 0000 number
00 0000 0000 0001 0011 1111 1-compl
00 0000 0000 0001 0100 0000 2-compl = 256 + 64 = 320

so the bit pattern

11 1111 1111 1110 1100 0000

represents the number -320

The benefit of 2-s complement comes into play when doing
arithmetic. You just don't care if the number is positive
or negativ, you simply treat all numbers as beeing positive
and do the arithmetic (the sign bit will take care of itself).

It is only during input/output that an entered sign or bit 21 needs
attention.
Input: If the user enters a negative number, you read that
number as if it were a positive one (without the '-') and
do a 2-s complement on it to negate it.

Output: if bit 21 is set, you output a '-' since it is a negative
number, apply 2-s complement on the number (again: to negate it
and thus make it positive) and output that number.

Also note: with 22 bits, one bit for sign, you can't
represent 4193984. You simply don't have enough bits
for it.

Also note: you can't use your compilers facilities to output
negative numbers, since your compiler doesn't know that you
treat bit 21 as the sign bit. Thats why it is not meaningfull
in your case to talk about the number 4193984 although you
will see that number when you do a printf on A. But your
compiler doesn't know that bit 21 is the sign bit, thus ...

void OutputMyNumber( unsigned int A )
{
if( A & 0x002000000000 ) { // bit 21 set?
// if yes: negative number
printf( "-" );
A = ( ~A + 1 ) & 0x003FFFFF;
}
printf( "%d", A );
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5


Karl Heinz Buchegger wrote:

cylin wrote:

unsigned int ATwoSComplement = ( ~A + 1 ) & 0x003FFFFF;

Yes, that's simple.
But it seems that "( ~A + 1 ) & 0x003FFFFF" is always positive.
How can I handle if the 2's complement is negative?
It is bit 21 which tells you if the number should be output
as a negative one.

bit 21 set -> negative number. output '-' and the 2-s complement of the number
bit 21 not set -> positive number, just output it.

So in your case:

22 1111 1111 1100 0000 0000
10 9876 5432 1098 7654 3210

A= 11 1111 1111 1110 1100 0000 = 4193984

since bit 21 is set, A holds a negative number.
But which one:

11 1111 1111 1110 1100 0000 number
00 0000 0000 0001 0011 1111 1-compl
00 0000 0000 0001 0100 0000 2-compl = 256 + 64 = 320

so the bit pattern

11 1111 1111 1110 1100 0000

represents the number -320

The benefit of 2-s complement comes into play when doing
arithmetic. You just don't care if the number is positive
or negativ, you simply treat all numbers as beeing positive
and do the arithmetic (the sign bit will take care of itself).

It is only during input/output that an entered sign or bit 21 needs
attention.
Input: If the user enters a negative number, you read that
number as if it were a positive one (without the '-') and
do a 2-s complement on it to negate it.

Output: if bit 21 is set, you output a '-' since it is a negative
number, apply 2-s complement on the number (again: to negate it
and thus make it positive) and output that number.

Also note: with 22 bits, one bit for sign, you can't
represent 4193984. You simply don't have enough bits
for it.

Also note: you can't use your compilers facilities to output
negative numbers, since your compiler doesn't know that you
treat bit 21 as the sign bit. Thats why it is not meaningfull
in your case to talk about the number 4193984 although you
will see that number when you do a printf on A. But your
compiler doesn't know that bit 21 is the sign bit, thus ...

void OutputMyNumber( unsigned int A )
{
if( A & 0x002000000000 ) { // bit 21 set?
// if yes: negative number
printf( "-" );
A = ( ~A + 1 ) & 0x003FFFFF;

Sorry: must be .... & 0x001FFFFF;
since we want to get rid of the sign bit also.
}
printf( "%d", A );
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6
Dear Karl,

Regard to your method,
I solve it.
Thanks you very much.

Best Regards,
cylin.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
2
by: BT Openworld | last post by:
I have just had to upgrade to Access 2003 as Access 97 EMail (SendObject) doesn't work when loaded on Windows XP. I'm finding my way around Access 2003 but my biggest problem is getting...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.