473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Evens only input

Hello all,

I am trying to figure out a way to allow even only input in C++. Is there soemthing that I'm not seeing somewhere? Any help is appreciated in advance.

Thanks,
Straub
Aug 23 '07 #1
13 1672
gpraghuram
1,275 Expert 1GB
Hello all,

I am trying to figure out a way to allow even only input in C++. Is there soemthing that I'm not seeing somewhere? Any help is appreciated in advance.

Thanks,
Straub
I am not able to understand ur question.
can u provide more explanation
Raghuram
Aug 23 '07 #2
Meetee
931 Expert Mod 512MB
Hello all,

I am trying to figure out a way to allow even only input in C++. Is there soemthing that I'm not seeing somewhere? Any help is appreciated in advance.

Thanks,
Straub
If I am predicting correctly, do you want user to enter only even values as an input?

Regards
Aug 23 '07 #3
kreagan
153 100+
Hello all,

I am trying to figure out a way to allow even only input in C++. Is there soemthing that I'm not seeing somewhere? Any help is appreciated in advance.

Thanks,
Straub
You can use the MOD operator to check if the input is even or odd. If it is odd, you can decide what action should be taken. Either you don't do anything with the input or you force the input to an even number.

Expand|Select|Wrap|Line Numbers
  1. //get input
  2. if ( input % 2 == 1 ){
  3.       input++;
  4. }
Aug 23 '07 #4
JosAH
11,448 Expert 8TB
You can use the MOD operator to check if the input is even or odd. If it is odd, you can decide what action should be taken. Either you don't do anything with the input or you force the input to an even number.

Expand|Select|Wrap|Line Numbers
  1. //get input
  2. if ( input % 2 == 1 ){
  3.       input++;
  4. }
An extremely lazy way is to automatically turn every input int to its closed even
value:

Expand|Select|Wrap|Line Numbers
  1. int input; // the user input value
  2. input&= ~1;
  3.  
kind regards,

Jos
Aug 23 '07 #5
kreagan
153 100+
An extremely lazy way is to automatically turn every input int to its closed even
value:

Expand|Select|Wrap|Line Numbers
  1. int input; // the user input value
  2. input&= ~1;
  3.  
kind regards,

Jos
That is just brilliant. Though it might make some people's head explode.
Sep 1 '07 #6
Studlyami
464 Expert 256MB
input &= ~1;

would you mind explaining this line? I havn't seen syntax like that before.
Sep 1 '07 #7
kreagan
153 100+
input &= ~1;

would you mind explaining this line? I havn't seen syntax like that before.
I haven't either, but it was explained to me. FYI, it might be easier to think about this in binary.

& is logical AND
~ is bit invert. So 0....001 (1) converts to 1...110 (~1)

if you don't know what logical AND is. The truth table is

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

Lets say input is 5 or 101 (binary).

input = 11 & 1.....11110.

or
1...11110
0...00101 AND
0...00100

which means that every bit will remain the same except for the last bit which is forced to zero. This last bit determines if the number is even or odd (in binary, if the number ends in 0, it is even. If it ends in 1, the number is odd). Therefore the number is forced to even.

hope that explains it.
Sep 1 '07 #8
Studlyami
464 Expert 256MB
Ahhh alright thanks for the reply, but what is the ~(1) part? does that specify the specific bit that is to change? so ~ = the number and (1) = 1 bit start at the end?
Sep 1 '07 #9
kreagan
153 100+
Ahhh alright thanks for the reply, but what is the ~(1) part? does that specify the specific bit that is to change? so ~ = the number and (1) = 1 bit start at the end?
The tilda inverts all the bits.

so 1 is 0...00001
and ~1 is 1...11110

2 is 0...00010
and ~2 is 1...11101

3 is 0...00011
and ~3 is 1...11100

I hope that makse sense.
Sep 1 '07 #10
Studlyami
464 Expert 256MB
Not quite. let me try to break down where im getting confused.

Lets go back to the original example

Expand|Select|Wrap|Line Numbers
  1. int input; // the user input value
  2. input&= ~1;
  3.  
lets assume that the user inputs in 349
in binary 101011101

now if the tilda inverts all bits we are going to get 0's straight across if we and them.

010100010 //
101011101 //user value
-----------------
000000000

so I'm guessing that ~1 specifies what bit is to be inverted.

so if is &= ~1 we and the value below.

111111110 // this was specified by the & ~(1)
101011101 //user value
----------------
101011100

now using the same number above but input &= ~2;
do we get . . .

111111110 // invert the last 2 bits?
101011101 //user value
----------------
101011100

am i close?
Sep 1 '07 #11
kreagan
153 100+
Not quite. let me try to break down where im getting confused.

Lets go back to the original example

so I'm guessing that ~1 specifies what bit is to be inverted.
No, it inverts the binary number.

so if is &= ~1 we and the value below.

111111110 // this was specified by the & ~(1)
101011101 //user value
----------------
101011100
Yes

now using the same number above but input &= ~2;
do we get . . .

111111110 // invert the last 2 bits?
101011101 //user value
----------------
101011100
No, ~2 is 111111101
Sep 1 '07 #12
Studlyami
464 Expert 256MB
Alright thanks for the info I believe I understand now.
Sep 1 '07 #13
JosAH
11,448 Expert 8TB
Oh dear, all that turmoil just because a bit of bitfiddling; an even number has its
lowest bit set to 0 (zero). So for any number, if you set its lowest bit to zero it'll
be an even number. Suppose I have a bitmask 11 .... 11111110 (the lowest bit
is zero, all the others are 1 (one)) and I bitwise 'and' it with any number; all the
other bits remain the same (x & 1 == x) except for the lowest bit; it will be equal
to zero (x & 0 == 0).

I don't know how many bits there are in an int. I do know that I want the oposite
of the number 00 ... 00000001. The ~ operator flips all bits, so ~1 does exactly
what I want. So I 'and' every number with ~1 to turn it to an even nubmer:

x&= ~1;

kind regards,

Jos
Sep 2 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
8
by: highroller152 | last post by:
Not to step on anyone, but in reference to this thread on summing odds and evens, why not just use the 'continue' statement? So adding up the odds would look something like this: -on some event-...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.