Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-18-2005, 09:41 AM   #1 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
What is an odd number?

If anyone could answer the question, I'll be glad.
What is an odd, even number?
What is the difference between odd and even numbers?
silex is offline   Reply With Quote
Old 01-18-2005, 11:59 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Even can be devided by two, which actualy includes the number 0.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 01-19-2005, 09:11 AM   #3 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
Thanks redhead
So checking if a number is even should be:

number % 2
if mod = 1 then number is odd
else mod = 0 then number is even
I understand.
silex is offline   Reply With Quote
Old 01-19-2005, 09:41 AM   #4 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Another more optimal approach is to simply check the 1 bit.

In C++ it looks like this:
Code:
if (number & 1 == 1) {
  // number is odd
}
else {
  // number is even
}
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 01-19-2005, 11:09 AM   #5 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
what does the '&' do?
silex is offline   Reply With Quote
Old 01-19-2005, 06:59 PM   #6 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
The & is a bitwise AND function. The AND function returns true (or 1 in the case of bits) if both bits are 1. It returns 0 in every other case. In this example, it is used as a mask. You can stop here if you already understand this, and I am sorry if I am teaching the teachers... but I will continue if you do so wish.

If you have a byte (take the number 5: 0101) and you only want the last bit (1) you can use a bitmask to find it by using the AND operator. You would use the mask 0001. If you AND this with 5: 0101 & 0001 .. all the bits in positions 2 through 4 are set to 0, with the first be being 1 (0001). Remember, any two bits that are not 0 get set to 0 by the AND function.

In this case, this is what we want. If a number is odd, it will have a 1 in the first bit position (5: 0101) while even numbers don't (4: 0100). This is true for all odd/even numbers, because only 2^0 is odd (1). Therefore, we can use a bitmask of 1 to see whether this bit is set: someNumber & 1 = 0000x where x is 1 or 0, 'odd' or 'even'.

I'm pretty sure you'd have to take care of the edge case where the user supplies 0, as it is not really even or odd. I guess it is considered even though, from a previous post I don't think this algorithm would work on negative numbers though. I guess modulus wouldn't either, I'll have to test it.

Hope this helps!

PS: There is also OR and XOR operators. These could be discussed at another time. OH.. I think I feel my first article coming on! Well, that is, if it has not been done before.
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 01-20-2005, 08:52 AM   #7 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
Thanks ender.
I understand this, your post was really helpful.

I got some C excercises, and I try to write a program that:
1. Gets an int array
2. Using some loops, it will reverse the numbers in array to display them backwards // I don't know if loops are good for this task//
3. Write the reversed array in new int array

give me some directions or tips, these would be really helpful.
silex is offline   Reply With Quote
Old 01-24-2005, 07:48 PM   #8 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
lol, i had to do the same thing but for words. i had to reverse the words then compare them to the original words to see if they were palindromes. I don't have the code with me, but yes, i did use a loop.
Androto is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stumped about a random number ... metazai PHP 4 01-11-2005 02:40 PM
Odd And Even freesoft_2000 Java 3 10-27-2004 01:27 PM
Trying to get number of e-mails in a txt file to appear on my main page netpants PHP 4 08-19-2003 09:46 PM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM


All times are GMT -8. The time now is 04:33 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting