473,664 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bool int


Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

--
Texeme
http://texeme.com
Nov 16 '05 #1
10 1839
Elementary Penguin wrote:

Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;


i guess because c# is strongly typed
Nov 16 '05 #2
Or...

if (true) ++count;

....which is arguably an order of magnitude more readable anyway.
Afterall, I'm still not sure if you wrote the number 1 or the letter l.

Brian

Elementary Penguin wrote:
Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

--
Texeme
http://texeme.com


Nov 16 '05 #3
Gee, you must be an ex-C hack like me!

C was one of the few languages that got ints and bools confused. You
couldn't do that in Pascal, either!

C# is strongly typed: booleans and integers are different things. I
prefer it, but that's just me. :)

Nov 16 '05 #4

Let me give you more background.

I wanted to count bits on type byte, and I found a c routine:

http://www.caam.rice.edu/~dougm/twiddle/BitCount.html

int count = 0;
int tmpbits = bits;
while (tmpbits) {
if (tmpbits & 1) ++count;
tmpbits >>= 1;
}
return count;

but to turn it into a c# method requires:

int cbits(byte b)
{
int count = 0;
int tmpbits = b;
while (tmpbits>0)
{
if ((tmpbits & 1)==1) ++count;
tmpbits >>= 1;
}

return count;
}
Brian Gideon wrote:
Or...

if (true) ++count;

...which is arguably an order of magnitude more readable anyway.
Afterall, I'm still not sure if you wrote the number 1 or the letter l.

Brian

Elementary Penguin wrote:
Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

--
Texeme
http://texeme.com


--
Texeme
http://texeme.com
Nov 16 '05 #5
On Fri, 28 Jan 2005 11:16:58 -0800, Elementary Penguin wrote:
Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;


If requires a condition that evaluates to either true or false. 1 does not
evaluate true or false. You can either go with your second line, or:

if(true) ++count;

For us former C/C++ developers, we can also do:

if(Convert.ToBo olean(1)) ++count;

Convert.ToBoole an(short/int/long/double/decimal) overloads return true if
the value is non-zero, otherwise false.
--
Tom Porterfield
Nov 16 '05 #6
I see where you're coming from now.

So the ole bit count eh? Here is what I used recently for a chess
engine in C#.

public int BitCount(UInt64 bitboard)
{
int count;
for (count = 0; bitboard != 0; bitboard &= bitboard - 1;) count++;
return count;
}

This is one function I was hoping would be implemented by the framework
because the JIT compiler could output the faster x86 instructions for
bit counting if the application was running on a CPU that supported
those instructions.

Brian

Elementary Penguin wrote:
Let me give you more background.

I wanted to count bits on type byte, and I found a c routine:

http://www.caam.rice.edu/~dougm/twiddle/BitCount.html

int count = 0;
int tmpbits = bits;
while (tmpbits) {
if (tmpbits & 1) ++count;
tmpbits >>= 1;
}
return count;

but to turn it into a c# method requires:

int cbits(byte b)
{
int count = 0;
int tmpbits = b;
while (tmpbits>0)
{
if ((tmpbits & 1)==1) ++count;
tmpbits >>= 1;
}

return count;
}


Nov 16 '05 #7
no, because that's infinitely worse.

for example, if I intend to do if(i == x) but wrote if( i = x ), in your
scenario, it will still run, but will become a hard to track bug.

"Elementary Penguin" wrote:

Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

--
Texeme
http://texeme.com

Nov 16 '05 #8
Worse? Not at all, if( i = x ) won't even compile as the expression is of
type int and if expects a bool.

Willy.

"Daniel Jin" <Da*******@disc ussions.microso ft.com> wrote in message
news:37******** *************** ***********@mic rosoft.com...
no, because that's infinitely worse.

for example, if I intend to do if(i == x) but wrote if( i = x ), in your
scenario, it will still run, but will become a hard to track bug.

"Elementary Penguin" wrote:

Shouldn't I be able to do this:

if(1) ++count;

it seems really a pain to have to do

if(1==1) ++count;

--
Texeme
http://texeme.com

Nov 16 '05 #9
Willy Denoyette [MVP] <wi************ *@pandora.be> wrote:
Worse? Not at all, if( i = x ) won't even compile as the expression is of
type int and if expects a bool.


No, that's exactly what Daniel was saying. In the kind of situation the
OP was talking about (where if(1) compiles), for instance in C, you get
the type of problem Daniel mentioned.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

3
2608
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
4
2945
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
19
3350
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or something else? I ask because I want to use vector<bool> with a few billion entries exceeding the int32 range for indexing and I want to use as few memory as possible for the whole
4
10747
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags); Thanks Ole
10
19136
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using System; using System.Runtime.InteropServices; //
1
1598
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the native bool method uses to hold the return value. Is this a known VC 7.1 bug? Is there a hotfix...
4
6472
by: gpg | last post by:
I am using a legacy DLL and need to marshal some structures for use in the DLL. For the most part, I have figured out my needs except for one small item. I have a structure that contain, among other items, an array of bools (not BOOL). The array is a fixed size and is contained in the structure. Should be simple ie:
6
6807
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
64
3861
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10 years back: correct me if i am wrong) 2) int was word aligned (16bit and 32bit). Writter of above code doesnt want to give choice to compiler.
3
6312
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is that no matter if we pass TRUE or FALSE, the bool is always marshalled as true. // unmanaged code in dll typedef bool (__stdcall *BoolCallBack)(short b);
0
8437
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
8861
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...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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
8636
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
6187
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
5660
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
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

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.