Wrox Programmer Forums
|
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
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
 
Old July 29th, 2011, 10:32 AM
Registered User
 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 2 - Bitwise NOT ~ just a bit confused

hi all,

ok, so on page 86 under the section Bitwise NOT, we have a working example of using ~ on the variable letter1, which is 65 which is represented as:

0000 0000 0000 0000 0000 0000 0100 0001

in the book it says ~letter1 = 190, but how can that be if i invert it i get:

1111 1111 1111 1111 1111 1111 1011 1110 which is significantly larger than 190. Now what makes things even more confusing is that on my pc i get -66?

Now i know that letter1 is declared as a char so its only 8 bits, but i've tried making letter1 an int and it gives me the same result. ive noticed that coverting any positive integer like 4 or 7 will give me -5 or -8 respectively, but when using the bits that cant possibly make sense..

anyone got any ideas?


thanks a mil!!

 
Old November 13th, 2011, 03:04 PM
Registered User
 
Join Date: Oct 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default character type

Hi,
I know it's old thread, but if you still don't know the answer then I think it's the type of character value, if you try inverting unsigned char 'A' (65) it will
give you 190.
It's still weird though, I can't get why when you type:

int main()
{
unsigned char sample = 'A';
cout << static_cast<int>(sample) << endl;
unsigned char sample2 = ~sample;
cout << static_cast<int>(sample2) << endl;
cout << static_cast<int>(~sample) << endl;
return 0;

}
you will get output:
65
190
-66

I don't get why when for static_cast function I use rvalue ~sample it still gives -66 . I think after checking: ~sample also would give -66 so I guess operator ~ implicitly converts sample to type signed char before reversing bits and then outputs as int.

Analyze the output of the below so it may make it more clear for you:
unsigned char sample = 'A';
cout << static_cast<int>(sample) << endl;
unsigned char sample2 = ~sample;
cout << static_cast<int>(sample2) << endl;
cout << static_cast<int>(~sample) << endl;
cout << sample << endl;
cout << ~sample << endl;
cout << sample2 << endl;
return 0;

Now I have also tried below lines and it worked so I guess you need to explicitly convert your value to unsigned char by any means:

cout << static_cast<int>(static_cast<unsigned char>(-66)) << endl;
cout << static_cast<int>(static_cast<unsigned char>(~sample)) << endl;

I hope this helps.

Last edited by Limak; November 13th, 2011 at 03:32 PM..
 
Old November 22nd, 2011, 09:24 AM
Registered User
 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, i will have a look at this code and see if it helps

:)





Similar Threads
Thread Thread Starter Forum Replies Last Post
BITWISE SHIFT OPRATION kulfi BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 0 May 30th, 2010 11:42 AM
BITWISE SHIFT OPRATION kulfi BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 0 May 30th, 2010 11:40 AM
Confused between Appendix B and Chapter 1 xzander BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 10 February 19th, 2008 07:26 PM
64 Bit - Issue in 64 bit IIS calling Win32 API Hubman General .NET 1 August 24th, 2006 09:19 AM
Bitwise and Access owain SQL Language 10 June 8th, 2006 10:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.