# Simple Challenge C++

• (5 Pages)
• « First
• 3
• 4
• 5

## 71 Replies - 47646 Views - Last Post: 19 July 2013 - 07:04 PM

### #61 htoruen

Reputation: 4
• Posts: 14
• Joined: 26-December 12

## Re: Simple Challenge C++

Posted 10 January 2013 - 05:05 PM

ishkabible, on 22 May 2012 - 02:56 PM, said:

the '&' operator is the bit-wise 'and' operator. because 1 is represented by the first bit and all other bits are multiples of 2 all other bits don't effect weather a number is odd or not. by 'anding' the first bit with 1 you check to see if the first bit is set. if it then the number is odd. but we want weather it is even or not; this means inverting the result with '!'.

Not exactly. Your actually anding all of the corresponding bits.

For example,

1 0 1 0 0 1 1 0
& 1 0 1 0 1 1 1 1
------------
= 1 0 1 0 0 1 1 0

In C/C++ 0 is false, and not zero is true. true and true == true, true and false == false, false and false == false.

Because 1 only has one on bit,

0 0 0 0 0 0 0 1

Anything anded with 1 is either 1 or zero.

0 0 0 0 0 0 0 1
& ? ? ? ? ? ? ? ?
= -------------
0 0 0 0 0 0 0 ?

### #62 jjl

• Engineer

Reputation: 1120
• Posts: 4,647
• Joined: 09-June 09

## Re: Simple Challenge C++

Posted 10 January 2013 - 08:06 PM

Quote

In C/C++ 0 is false, and not zero is true. true and true == true

That may be true for C, since there is no defined boolean type; however, for C++, the integer equivalent of "true" is defined as 1.

This post has been edited by jjl: 10 January 2013 - 08:06 PM

### #63 htoruen

Reputation: 4
• Posts: 14
• Joined: 26-December 12

## Re: Simple Challenge C++

Posted 11 January 2013 - 04:59 AM

Well, I'm not sure that the C definition of true and false is not true also of C++. Sure a bool type when converted to an integer is guaranteed to be a 0 or a 1. That's just a protocol. I wouldn't say that true is the integer equivalent of 1, it's just converted to 1 when converted to an int. Any number other than zero will still evaluate to true as a boolean expression.

I would say that a non-zero values is the logical equivalent of true I guess.

For example, if (-12293) is logically equivalent to if (1) and if (true).

### #64 htoruen

Reputation: 4
• Posts: 14
• Joined: 26-December 12

## Re: Simple Challenge C++

Posted 11 January 2013 - 05:13 AM

But actually you are right. (true == 2) evaluates to false.

### #65 htoruen

Reputation: 4
• Posts: 14
• Joined: 26-December 12

## Re: Simple Challenge C++

Posted 11 January 2013 - 08:47 AM

Although
(true == (bool) 2)
will evaluate to true.

### #66 snypeNET

Reputation: 1
• Posts: 12
• Joined: 06-October 12

## Re: Simple Challenge C++

Posted 12 January 2013 - 09:46 AM

Done in 9 characters. Easier than I thought. I get spoiled with using C# at work.

Spoiler

### #67 Apmeyer

Reputation: 1
• Posts: 1
• Joined: 27-March 13

## Re: Simple Challenge C++

Posted 27 March 2013 - 01:40 AM

Woo I actually answered a challenge without looking up the answer />
Spoiler

### #68 Virtent

Reputation: 0
• Posts: 16
• Joined: 28-May 13

## Re: Simple Challenge C++

Posted 02 June 2013 - 02:44 PM

(f & 1) != f

### #69 Skydiver

• Code herder

Reputation: 4250
• Posts: 13,600
• Joined: 05-May 12

## Re: Simple Challenge C++

Posted 02 June 2013 - 03:43 PM

Unfortunately that won't work...
Let f == 3. 3 & 1 = 1. 1 != 3. so isEven() will return true, but 3 is not even.

### #70 Axman10

Reputation: 1
• Posts: 36
• Joined: 17-November 11

## Re: Simple Challenge C++

Posted 09 June 2013 - 12:16 AM

Spoiler

Is what I came up with, I assume many others have as well.

### #71 salazar

Reputation: 102
• Posts: 596
• Joined: 26-June 13

## Re: Simple Challenge C++

Posted 30 June 2013 - 03:55 PM

The limitation made the best option obvious: bitwise operators.
#include <iostream>
using namespace std;

bool isEven(int f)
{
return !(f & 1);
}

int main (int argc, char * argv[])
{
int f = 10;
cout << f << ( isEven(f)  ?  " is even" : " is odd" ) << endl;
return 0;
}

### #72 coilygeekintraining

Reputation: 0
• Posts: 17
• Joined: 28-March 13

## Re: Simple Challenge C++

Posted 19 July 2013 - 07:04 PM

I have to try this challenge!