Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming 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
  #1 (permalink)  
Old July 5th, 2006, 03:21 AM
Registered User
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default understanding Variable use

Ok, i know that bits are grouped into 8 and are called bytes. And each byte has its own address to label it. But when I declare a variable of type (int) it will occupy 4 bytes. How does the compiler link the 4 bytes together since each has its own address. Does it combine them into one massive byte? That would seem to defeat the purpose of giving each an address?

Also why is it that an integer value is from -128 to 127? Is this because 0 is included? And if so then why does a type double have the range of 10^-308 to 10^308 shouldn't it be 10^-308 to 10^307?

thanks for the help
Reply With Quote
  #2 (permalink)  
Old July 6th, 2006, 01:40 PM
Authorized User
 
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

try to read a good book of assembly language. you are missing nibbles a group of 4 bits. read about addresing, and in c++ this is call encapsulation, do you know what encapsulation meand?

Reply With Quote
  #3 (permalink)  
Old July 13th, 2006, 03:50 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hahahaha skoob you are quite the humorist. Skoob, if you're serious, learn a little more about OOP and encapsulation here: http://en.wikipedia.org/wiki/Object-...ed_programming ... but I hope you're simply practicing polymorphism in real life -- are you a forum member, or a troll? Both! :)

Rooster Cogburn, you're correct that an int has 4 bytes. Each of these bytes has its own address. Each of these addresses is in sequential order. E.g. if the first byte is at memory address 500000, the second would be at 500001, etc. Because the compiler knows you're dealing with an int, it knows that it should look at bytes 500000 through 500003 when doing numeric operations on your variable. Fortunately, modern compilers know how to deal with 32-bit numbers natively, so math operations on numbers of this size are actually quite performant and don't require a trip up to the JVM for processing.

As for the range of integer values, how are binary numbers counted? You probably know this already: 1, 10, 11, 100, 101, 110, 111, 1000, etc. Do you see a pattern? First the least significant bit (LSB) is toggled. If its previous value was 1, then the operation carries over to the next significant bit. So in the case of 11, we toggle the LSB to get 10. But since the first bit was initially 1, we go to alter the second bit. Then we get 00, but since the second bit was initially 1, we go to the third bit. We toggle it to 1, to get 100, and since it was initially 0 we stop.

Given this algorithm, how do we subtract? I'll leave that for you to ponder the exact algorithm. So consider subtracting 1 from 0. What do you get? All ones. You get enough ones to fill up however large your number is -- in the case of ints, you get 32 ones. How processors and compilers tell the difference between negative and positive numbers is by looking at the most significant bit (MSB). If MSB=1, the number is negative, otherwise, it's positive. Given this design, the lowest number you can represent is all ones and the highest number has MSB=0, but the rest all ones. Given how binary counting works, if you have 8 bits, the smallest number you can represent is -128 and the biggest number is 127.

The double type is actually more complicated than what you're thinking. A double does not work the same as integers. Instead, it has one bit dedicated to positive/negative, a set of bits dedicated to what the number is without a decimal point, and another set of bits dedicated to where the decimal point goes.

Jon Emerson
http://www.jonemerson.net/
Reply With Quote
  #4 (permalink)  
Old July 15th, 2006, 08:38 PM
Registered User
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When you say "it knows that it should look at bytes 500000 through 500003 when doing numeric operations on your variable" does this mean that it looks at them as one continuous byte? So if you needed to store 30,000 having 4 individual bytes that only have a possible range of 127 would not be able to store it right??

thanks


Reply With Quote
  #5 (permalink)  
Old July 25th, 2006, 08:03 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the late reply. When I say "it knows that it should look at bytes 500000 through 500003 when doing numeric operations on your variable", I mean that the processor knows that the memory you're addressing is four bytes in length. To understand this, let's get into the fundamentals of how a processor's API is set up. On an X86 chip, there are lots of different commands for retrieving values from memory, adding numbers, saving values to memory, branching (to support "if" statements and while loops), etc. With the exclusion of branching, each of the other types of commands I mentioned has different operation codes depending on how many bits should be retrieved, added, or saved. So from the processor's perspective, when it receives a request to add 32 bits, it receives just one request and it knows that to get 32 bits it must read all the bits over 4 bytes.

I'm not sure what you're asking in your concluding question.

Jon Emerson
http://www.jonemerson.net/
Reply With Quote
  #6 (permalink)  
Old July 28th, 2006, 03:37 PM
Authorized User
 
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

do you know what is encapsulation? why you want to know addressin in c++, no such thing.

Reply With Quote
  #7 (permalink)  
Old July 28th, 2006, 03:41 PM
Authorized User
 
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

you guys are in the wrong forum. and PANACEA has no idea about how procceso 80x86 works, go back to your mama's tit, you are brand new on assembly.

Reply With Quote
  #8 (permalink)  
Old July 28th, 2006, 06:15 PM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hai

10^308 means 308 zeros following 10 (523 X 10 ^ 308 = 5230 00000 00000 00000 00000 00000 00000 .....) .similarly 10^-308 (523 x 10 ^ -308 = 0.00000 00000 00000 00000 00000....523).This indicates how much an indiger can have maximum or minimum value and not the total number of indigers.At the same time if 10 ^ 308 is a very BIG number ( more than 308 digits ) if you want to include zero in the counting you have to add 10 00000 0000 0000 00000 ... + 1. That is not necessary.

http://www.amsterdamhotels.at/
http://www.indianhillresorts.info/
http://www.cheapparishotels.info
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding Project prasanna.nadgir Need help with your homework? 1 August 23rd, 2007 03:15 PM
Need help understanding validation kscase Pro Visual Basic 2005 1 July 7th, 2007 07:15 PM
Understanding Axes Chamkaur XSLT 1 August 6th, 2006 03:09 PM
Understanding Recordsets? JpaulH Access VBA 2 May 10th, 2006 07:18 PM
Understanding Session rajuru Beginning PHP 2 September 25th, 2004 05:05 AM





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