# Variables Explained

Page 1 of 1

## 7 Replies - 2368 Views - Last Post: 12 March 2013 - 01:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=315204&amp;s=5c64ec168b38b6b6f9434cbf2c90c104&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bbarrick

Reputation: 0
• Posts: 4
• Joined: 12-March 13

# Variables Explained

Posted 12 March 2013 - 11:57 AM

I just started studying up on C#, it's been awhile since I've done any programming so I picked up Wrox's book Beginning Visual C# 2012 Programming. Unfortunately, their forums are just about dead. I understand variables, what they do. This appears to be describing how they work in a way that I do not quite understand it. Without quoting the book exactly I'll provide an example.

Variable storing N bits enables representation of numbers between 0 and (2^n - 1).

Mapping between integers and bits representing those integers:

0 = 00

It's like I understand what it's trying to say, but I'm not quite grasping it. Any help dumbing this down would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Variables Explained

### #2 Ryano121

• D.I.C Lover

Reputation: 1457
• Posts: 3,282
• Joined: 30-January 11

## Re: Variables Explained

Posted 12 March 2013 - 12:00 PM

If you have a variable of a size N bits, then if you assume that the variable is an integer, then taking into account that the size of the variable is N bits, the variable would be able to hold numbers between 0 and 2^N - 1 (stored in binary).

Not really sure what the second bit is trying to say, but I guess it's trying to explain how numbers in base 10 (the numbers we know and love) can be represented in base 2 (binary with loads of 1's and 0's).

Not sure if that really helped as I've really just repeated it again, but it's pretty dumbed down as it is to be honest.

This post has been edited by Ryano121: 12 March 2013 - 12:01 PM

### #3 bbarrick

Reputation: 0
• Posts: 4
• Joined: 12-March 13

## Re: Variables Explained

Posted 12 March 2013 - 12:18 PM

Yea, I think it's just the way to book has it written, either that or I'm trying to overthink it.

Declaring a variable as int doesn't allocate n bits, right? Doesn't it allocate the bits based on the size of the integer that get's assigned to the variable? It's been a decade or so since I've done much with programming. I'm trying to get back into it though. The storing a single digit integer in memory requires a minimum of 8 bits right?

### #4 Ryano121

• D.I.C Lover

Reputation: 1457
• Posts: 3,282
• Joined: 30-January 11

## Re: Variables Explained

Posted 12 March 2013 - 12:26 PM

In C# each and every int variable is assigned 32 bits in memory regardless of how many of them are actually used. Although in a sense you could say that they are all still used to determine the number.

The N bits thing is a general rule of the range of numbers that a variable with N bits can store. The actual number of bits used will depend on what system/language you use. However normally a predefined number of bits is assigned to each variable. It doesn't care if if it needs the bits to store the number - it assigns them anyway.

It doesn't necessarily require 8 bits. It depends on what number you want to store. A small number you could store in a couple of bits. Large numbers can need more like 32 or 64 bits each.

### #5 bbarrick

Reputation: 0
• Posts: 4
• Joined: 12-March 13

## Re: Variables Explained

Posted 12 March 2013 - 12:32 PM

Ok, I think that cleared things up a bit for me.

When declaring a variable as an integer type, n in the equation becomes 32 right? I know I've read this somewhere before, probably even understood it back in the day when my mind was on things of less importance. In school I was pretty sharp at RPGIV programming on AS/400 mainframes, lol.

### #6 Ryano121

• D.I.C Lover

Reputation: 1457
• Posts: 3,282
• Joined: 30-January 11

## Re: Variables Explained

Posted 12 March 2013 - 12:37 PM

Yep you got it.

Which means for a positive integer (uint variable) in C#, the range of numbers that is stores is 0 to 4,294,967,295 (2^32 - 1).

### #7 pharylon

Reputation: 41
• Posts: 86
• Joined: 01-September 12

## Re: Variables Explained

Posted 12 March 2013 - 01:05 PM

Hey, by the way, I ended up getting three books learnign C# - Beginning C#, Visual C# Step By Step, and Head First C#.

Of the three, Beginning C# was by far my least favorite. I highly recommend "Head First C#" and "C# Step by Step", though. I would suggest going through "Head First" first (ha, I see what I did there...) and then going through "Step by Step." I think "Head First" teaches it better, but "Step By Step" goes into more detail and is a bit more advanced by the end, though it lacks a lot of do-it-yourself exercises that I think are important in learning to program.

I found Beginning C# overly-confusing, and I think it does a terrible job of explanation.

### #8 bbarrick

Reputation: 0
• Posts: 4
• Joined: 12-March 13

## Re: Variables Explained

Posted 12 March 2013 - 01:11 PM

Thanks Pharylon, I'm beginning to think the same thing. I was hoping this would get me to an intermediate or advanced C# programming book. I got this one because it supposedly get's into working with HTML/XML files later on.