473,785 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Condition is Always False" Question

I am using Borland C++ Builder 5 for my application. When I build my
application, it states that I have several if-statements that will
always be false. The statements appear correct, so could someone
please enlighten me as to how C++ Builder may think that the
conditions will always be false. See below. Thanks.

<snip>
td.expMon = trk2[9] * 10 + trk2[10];

// Borland says this is always false, though the condition is
fine.
if ((td.expMon < 0) || (td.expMon 12)) {
err = TKT_INVALID_MON TH;
}
</snip>

<snip>
td.expDay = trk2[11] * 10 + trk2[12];

// Borland says this is always false, though the condition is
fine.
if ((td.expDay < 0) || (td.expDay 31)) {
err = TKT_INVALID_DAY ;
}
</snip>

<snip>
td.eftvDay = trk2[28] * 10 + trk2[29];
// Borland says this is always false, though the condition is
fine.
if ((td.eftvDay < 0) || (td.eftvDay 31)) {
err = TKT_INVALID_DAY ;
}
</snip>

Feb 12 '07
30 3682
Victor Bazarov wrote:
Rolf Magnus wrote:
>Victor Bazarov wrote:
>>Brian wrote:
[..]
Correct me if I'm wrong, but if I also try to assign a negative
number to this variable, the negative number will be assessed as
the unsigned value. So if I tried to assign -1, it should be
assessed as the unsigned value of 65,535.

Seems right if your 'unsigned short' is 16 bits long.

... and the signed one is stored in 2's complement.

Actually, that doesn't matter. The standard guarantees the 'modulo
2^n' operation regardless of the representation. The bit pattern doesn't
change for 2's complement (it does for the other two reps).
Hmm, the exact wording in the standard is "If the destination type is
unsigned, the resulting value is the least unsigned integer congruent to
the source integer (modulo 2 n where n is the number of bits used to
represent the unsigned type) [Note: In a two’s complement representation,
this conversion is conceptual and there is no change in the bit pattern (if
there is no truncation). ]". It doesn't say what it means by "congruent" .
The modulo part seems only to matter if you are converting from a bigger to
a smaller type, and basically just means that the extra bits are cut off.
The note talks about two's complement, but it doesn't say what happens to
other representations or why the conversion is "conceptiua l" for two's
complement. Basically, I don't understand anything. I'm not sure if that is
due to the standard's wording or due to some lack of understanding English
on my side.

Feb 12 '07 #11
Rolf Magnus wrote:
Victor Bazarov wrote:
>Rolf Magnus wrote:
>>Victor Bazarov wrote:

Brian wrote:
[..]
Correct me if I'm wrong, but if I also try to assign a negative
number to this variable, the negative number will be assessed as
the unsigned value. So if I tried to assign -1, it should be
assessed as the unsigned value of 65,535.
Seems right if your 'unsigned short' is 16 bits long.
... and the signed one is stored in 2's complement.
Actually, that doesn't matter. The standard guarantees the 'modulo
2^n' operation regardless of the representation. The bit pattern doesn't
change for 2's complement (it does for the other two reps).

Hmm, the exact wording in the standard is "If the destination type is
unsigned, the resulting value is the least unsigned integer congruent to
the source integer (modulo 2 n where n is the number of bits used to
represent the unsigned type)
This says it all. Conceptually, the conversion from int to an unsigned
int looks like this (assume that 'big' is a signed integer type with an
infinite range):

unsigned Convert(int i)
{
big temp = i;
while(temp numeric_limits< unsigned>::max( ))
{
temp -= big(numeric_lim its<unsigned>:: max())+1;
}

while(temp < 0)
{
temp += big(numeric_lim its<unsigned>:: max())+1;
}

return unsigned(temp);
}

[Note: In a two’s complement representation,
this conversion is conceptual and there is no change in the bit pattern (if
there is no truncation). ]". It doesn't say what it means by "congruent" .
"coinciding when superimposed"
The modulo part seems only to matter if you are converting from a bigger to
a smaller type, and basically just means that the extra bits are cut off.
No, it applies whenever a value outside of the range of the unsigned
type is converted to that type.
The note talks about two's complement, but it doesn't say what happens to
other representations or why the conversion is "conceptiua l" for two's
complement.
It only says that for two's compliment representations , this conversion
is basically a no-op. Nothing in that note contradicts anything
previously said in the section, it serves only to clarify.
Basically, I don't understand anything. I'm not sure if that is
due to the standard's wording or due to some lack of understanding English
on my side.


--
Clark S. Cox III
cl*******@gmail .com
Feb 12 '07 #12
Clark S. Cox III wrote:
Rolf Magnus wrote:
>Victor Bazarov wrote:
>>Brian wrote:
[..]
Correct me if I'm wrong, but if I also try to assign a negative number
to this variable, the negative number will be assessed as the unsigned
value. So if I tried to assign -1, it should be assessed as the
unsigned value of 65,535.
Seems right if your 'unsigned short' is 16 bits long.
.... and the signed one is stored in 2's complement.

That is irrelevant. On *any* C implementation [snip]
Oops, wrong group, but this happens to apply equally well to any C++
implementation. :)
--
Clark S. Cox III
cl*******@gmail .com
Feb 12 '07 #13
Rolf Magnus wrote:
Victor Bazarov wrote:
>Rolf Magnus wrote:
>>Victor Bazarov wrote:

Brian wrote:
[..]
Correct me if I'm wrong, but if I also try to assign a negative
number to this variable, the negative number will be assessed as
the unsigned value. So if I tried to assign -1, it should be
assessed as the unsigned value of 65,535.
Seems right if your 'unsigned short' is 16 bits long.
... and the signed one is stored in 2's complement.
Actually, that doesn't matter. The standard guarantees the 'modulo
2^n' operation regardless of the representation. The bit pattern doesn't
change for 2's complement (it does for the other two reps).

Hmm, the exact wording in the standard is "If the destination type is
unsigned, the resulting value is the least unsigned integer congruent to
the source integer (modulo 2 n where n is the number of bits used to
represent the unsigned type) [Note: In a two’s complement representation,
this conversion is conceptual and there is no change in the bit pattern (if
there is no truncation). ]". It doesn't say what it means by "congruent" .
The modulo part seems only to matter if you are converting from a bigger to
a smaller type, and basically just means that the extra bits are cut off.
The note talks about two's complement, but it doesn't say what happens to
other representations or why the conversion is "conceptiua l" for two's
complement. Basically, I don't understand anything. I'm not sure if that is
due to the standard's wording or due to some lack of understanding English
on my side.
x is congruent to y modulo m means that the remainder of x divided by m
is equal to the remainder of y divided by m (where division rounds
toward negative infinity). So 3 is congruent to 8 modulo 5, as is -2. In
general, -1 is congruent to m-1 modulo m, -2 is congruent to m-2, etc. A
machine that uses n bits to represent an integral type can hold unsigned
values from 0 up to and including (2^n)-1. When you're dealing with
values outside that range, they'll generally be reduced modulo 2^n, that
is, replaced by their remainder (rounding toward negaitve infinity) when
divided by 2^n. Thus, the original value is replaced by a value in the
range 0 to (2^n)-1 that is congruent to the original value modulo 2^n.

A simpler way to think of it informally is what I mentioned earlier: -1
becomes (2^n)-1, -2 becomes (2^n)-2, etc. And (2^n)-1 is UINT_MAX.

The note is specifically about twos-complement representation. Notes
aren't normative (they don't create requirements), and if you don't do
assembly programming, it's at best confusing. So ignore it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 12 '07 #14
Rolf Magnus wrote:
Victor Bazarov wrote:
>Rolf Magnus wrote:
>>Victor Bazarov wrote:

Brian wrote:
[..]
Correct me if I'm wrong, but if I also try to assign a negative
number to this variable, the negative number will be assessed as
the unsigned value. So if I tried to assign -1, it should be
assessed as the unsigned value of 65,535.

Seems right if your 'unsigned short' is 16 bits long.

... and the signed one is stored in 2's complement.

Actually, that doesn't matter. The standard guarantees the 'modulo
2^n' operation regardless of the representation. The bit pattern
doesn't change for 2's complement (it does for the other two reps).

Hmm, the exact wording in the standard is "If the destination type is
unsigned, the resulting value is the least unsigned integer congruent
to the source integer (modulo 2 n where n is the number of bits used
to represent the unsigned type) [Note: In a two's complement
representation, this conversion is conceptual and there is no change
in the bit pattern (if there is no truncation). ]". It doesn't say
what it means by "congruent" . The modulo part seems only to matter if
http://en.wikipedia.org/wiki/Congruence_relation

A and B are congruent modulo N if (A-B) is divisible by N exactly.
So, if B is -1 and A is the number we seek (i.e. what it will be
if it's unsigned), and N=2^k where k is the number of bits in the
representation of 'A', then we get a simple equation:

A - (-1) = 2^(sizeof(A) * CHAR_BIT)

in our case:

A + 1 = 65536

which gives

A = 65535

(note that no particular hardware representation is involved here)
you are converting from a bigger to a smaller type, and basically
just means that the extra bits are cut off. The note talks about
two's complement, but it doesn't say what happens to other
representations or why the conversion is "conceptiua l" for two's
complement.
It's conceptual because the bit pattern doesn't change and no
arithmetic operation is actually performed.
Basically, I don't understand anything. I'm not sure if
that is due to the standard's wording or due to some lack of
understanding English on my side.
You were just misreading it and I couldn't explain it right.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #15
Brian wrote:
>
Correct me if I'm wrong, but if I also try to assign a negative number
to this variable
On x86 CPU yes, but it seems to me, that C++ does not require that MSB is
always sign bit, so representaion of "-1" in theory can be any (not only in
complementary binary code, where "-a == (~a)+1" , for example "-1" can be
"0xfffe", not "0xffff".

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 12 '07 #16
Grizlyk wrote:
Brian wrote:
>>
Correct me if I'm wrong, but if I also try to assign a negative
number to this variable

On x86 CPU yes, but it seems to me, that C++ does not require that
MSB is always sign bit, so representaion of "-1" in theory can be any
(not only in complementary binary code, where "-a == (~a)+1" , for
example "-1" can be "0xfffe", not "0xffff".
C++ supports three representations at this time: 2's complement, 1's
complement and signed magnitude. All of them have the MSB as the sign
bit, but I am not sure how this is relevant here.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #17

Victor Bazarov wrote:
>>
On x86 CPU yes, but it seems to me, that C++ does not require that
MSB is always sign bit, so representaion of "-1" in theory can be any
(not only in complementary binary code, where "-a == (~a)+1" , for
example "-1" can be "0xfffe", not "0xffff".

C++ supports three representations at this time: 2's complement, 1's
complement and signed magnitude. All of them have the MSB as the sign
bit, but I am not sure how this is relevant here.
I do not know what is differences between "2's complement, 1's complement
and signed magnitude", but think befor it, that C++ save internal CPU data
representation, that can be (in theory) any, so passing "int" to "unsigned"
can require "_i2u" internal converter for hosted C++ compiler to set correct
bits.

I always write -1 if I need UINT_MAX, but think it is not portable.

Or C++ have "C++ virtual machine" with signed implemented as complementary
binary, hiding internal CPU representation?

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 12 '07 #18
Grizlyk wrote:
Victor Bazarov wrote:
>>>
On x86 CPU yes, but it seems to me, that C++ does not require that
MSB is always sign bit, so representaion of "-1" in theory can be
any (not only in complementary binary code, where "-a == (~a)+1" ,
for example "-1" can be "0xfffe", not "0xffff".

C++ supports three representations at this time: 2's complement, 1's
complement and signed magnitude. All of them have the MSB as the
sign bit, but I am not sure how this is relevant here.

I do not know what is differences between "2's complement, 1's
complement and signed magnitude"
The difference is in the way negative numbers are represented. GIYF.
>, but think befor it, that C++ save
internal CPU data representation, that can be (in theory) any, so
passing "int" to "unsigned" can require "_i2u" internal converter for
hosted C++ compiler to set correct bits.

I always write -1 if I need UINT_MAX, but think it is not portable.
'unsigned int' and 'int' are required to have the same size. The
requirement is that a negative int 'a' when converted to unsigned
int produces (2^n + a) (where 'n' is the size in bits of the types).
Defining UINT_MAX as _different from_ (2^n - 1) is not possible if
we intend to satisfy the requirement that unsigned types behave the
way 3.9.1/4 requires ("obey the laws of arithmetic modulo 2^n").

So, AFA C++ is concerned, -1 converted to 'unsigned int' yields
'UINT_MAX' in any implementation.
Or C++ have "C++ virtual machine" with signed implemented as
complementary binary, hiding internal CPU representation?
Huh?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #19
Grizlyk wrote:
>
Victor Bazarov wrote:
>>>
On x86 CPU yes, but it seems to me, that C++ does not require that
MSB is always sign bit, so representaion of "-1" in theory can be any
(not only in complementary binary code, where "-a == (~a)+1" , for
example "-1" can be "0xfffe", not "0xffff".

C++ supports three representations at this time: 2's complement, 1's
complement and signed magnitude. All of them have the MSB as the sign
bit, but I am not sure how this is relevant here.

I do not know what is differences between "2's complement, 1's complement
and signed magnitude", but think befor it, that C++ save internal CPU data
representation, that can be (in theory) any, so passing "int" to
"unsigned" can require "_i2u" internal converter for hosted C++ compiler
to set correct bits.
Yes.

I always write -1 if I need UINT_MAX, but think it is not portable.
It is portable. The standard demands that conversion from -1 to any unsigned
integral type yields 2^n - 1 where n is the number of bits in the target
type. More precisely, any value x is converted to the unique integral value
in the interval [0,2^n) congruent to x mod 2^n. See [4.7/2].

Or C++ have "C++ virtual machine" with signed implemented as complementary
binary, hiding internal CPU representation?
No. However, that does not change the requirements of the standard about
conversion.
Best

Kai-Uwe Bux
Feb 12 '07 #20

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

Similar topics

2
2743
by: Jeremy S. | last post by:
By default, Web.config has the following section: <compilation defaultLanguage="c#" debug="true" /> note that debug="true" There is a comment - also in the default Web.config - that states that making debug="false" will result in faster performance but a loss of
59
4595
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
21
30544
by: Neel | last post by:
I am trying to "ping" a remote host in my C++/Redhat Linux code to check whether that host is connected or not. if (0 == system("ping -w 2 192.168.0.2)) But, in both cases (connected/disconnected), system call returns 0. Can someone please show what I am doing wrong? How to check the actual result of the ping status using C++ on Redhat linux ? Thanks in advance.
3
2185
by: Ernesto | last post by:
Within the scope of one Python file (say myFile.py), I'd like to print a message on ANY exception that occurs in THAT file, dependent on a condition. Here's the pseudocode: if anyExceptionOccurs(): if myCondition: print "Here's my global exception message"
3
3109
by: André | last post by:
Hi, I put that question already, but it's still not very clear to me, so ... Assume following option in web.config= debug="false" but in one aspx page (test.aspx) <%@ debug="true" ..%>
9
11190
by: Jamey Bon | last post by:
As a newbie to C#, I am not sure what I can do about this. I would like to do something like an Enumeration to use "constants" like Yes to indicate true and No for false. But since there seems to be no underlying 0 or non- zero for boolean values in C#, I am not sure how to handle this. Any advice would be appreciated. Thanks, JB
5
3901
by: Ben | last post by:
Hi; I use ain asp.net the CreateUserWizard control for new users. When it's done, i want to redirect them to another page (modifyprofile.aspx). This works with the code below. My question is: if i suppress the line "return false" in the javascript function profile(), the user is created but not redirected to the other aspx page. Can anybody explain me why?
1
5375
by: trint | last post by:
i have this in the html view: <asp:TemplateField HeaderText ="Remove?"> <ItemTemplate> <asp:CheckBox ID="removeSelect1" runat="server" /> </ItemTemplate> <HeaderTemplate> </HeaderTemplate> <HeaderStyle ForeColor="White" /> </asp:TemplateField>
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
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 we have to send another system
2
3645
muto222
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.