Simple Challenge C++

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

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

#61 htoruen  Icon User is offline

  • New D.I.C Head

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

Re: Simple Challenge C++

Posted 10 January 2013 - 05:05 PM

View Postishkabible, 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 ?

Was This Post Helpful? 1
  • +
  • -

#62 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,452
  • 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

Was This Post Helpful? 1
  • +
  • -

#63 htoruen  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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).
Was This Post Helpful? 0
  • +
  • -

#64 htoruen  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#65 htoruen  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#66 snypeNET  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#67 Apmeyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 :D/>
Spoiler

Was This Post Helpful? 1
  • +
  • -

#68 Virtent  Icon User is offline

  • New D.I.C Head

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

Re: Simple Challenge C++

Posted 02 June 2013 - 02:44 PM

(f & 1) != f
Was This Post Helpful? 0
  • +
  • -

#69 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • 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.
Was This Post Helpful? 1
  • +
  • -

#70 Axman10  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • 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.
Was This Post Helpful? 0
  • +
  • -

#71 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • 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;
}



Was This Post Helpful? 0
  • +
  • -

#72 coilygeekintraining  Icon User is offline

  • New D.I.C Head

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

Re: Simple Challenge C++

Posted 19 July 2013 - 07:04 PM

I have to try this challenge!
Was This Post Helpful? 0
  • +
  • -

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