Wrox Programmer Forums Chapter 2 Exercise 2
 |
BOOK: Ivor Horton's Beginning Visual C++ 2010
 This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880 Read more about Ivor Horton's Beginning Visual C++ 2010 or buy the book from your favorite retailerDownload the code for Ivor Horton's Beginning Visual C++ 2010
 Welcome to the p2p.wrox.com Forums. You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

May 15th, 2011, 06:59 PM
 rolndd Registered User Join Date: May 2011 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts
Chapter 2 Exercise 2

In exercise 2 wouldn't be easier to use the bitwise 'and' on the first 3 bits? Shifting 3 bits right then 3 bits left and subtracting from the original number seems like a lot of work.

June 4th, 2011, 08:35 AM
 AnTiDoD Registered User Join Date: Jun 2011 Posts: 4 Thanks: 0 Thanked 0 Times in 0 Posts

Exactly! At first I also solved the problem.

July 12th, 2011, 01:58 PM
 MD80study Registered User Join Date: Feb 2008 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
Ex2_exercise2

I checked if the user input was negative. If so, use "Two's Complement" to change the binary negative to positive, shift 3 right, then use "Two's Complement" to change it back to negative. This may be cheating, but it worked easily.

Code:
```shiftTemp = userNum;
// "Two's Complement" converts negative binary to positive by using bitwise NOT then adding one.
shiftTemp = ~shiftTemp + 1;
// Divides binary by 8. This drops remainder.
shiftTemp = shiftTemp >>= 3;
// "Two's Complement" converts positive binary to negative by using bitwise NOT then adding one.
shiftTemp = ~shiftTemp + 1;
// Multiply shiftTemp by 8 to get pre shift user number less the remaider
// then subtract this from original number to get remainder.
remainder = (userNum - (shiftTemp * 8));
```

 Similar Threads Thread Thread Starter Forum Replies Last Post Chapter 7 - Exercise [email protected] BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 2 March 16th, 2010 07:32 PM Chapter 3 exercise 2 weekapaugh BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 11th, 2010 02:28 AM Chapter 8 exercise 1 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 2nd, 2010 03:26 PM Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM chapter 3 exercise 4 walkamongus BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 0 September 10th, 2007 09:34 PM