# size of a variable in bits

Page 1 of 1

## 6 Replies - 9838 Views - Last Post: 29 October 2006 - 07:21 AMRate 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=20149&amp;s=71c2c66e71878f44e1e6cdae6e2d4a53&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dejabu18

• New D.I.C Head

Reputation: 0
• Posts: 32
• Joined: 03-March 06

# size of a variable in bits

Posted 27 October 2006 - 03:26 AM

hi guys I have some problems whit this function I need to write a function than can return the numbers of bits using for coding a short int variable, I wrote this

```int taille_short(){
int r=16;
for(int i=0;i>r;i++)
{
return i;

}
}
```

```int taille_short(){
int r=16;
for(int i=0;i>r;i<<1)
{
return i;

}
}
```

can you tell me what you think , I have some problem with the binary operators and whow thy works

thanks

Is This A Good Question/Topic? 0

## Replies To: size of a variable in bits

### #2 NyeNye

Reputation: 1
• Posts: 248
• Joined: 24-September 06

## Re: size of a variable in bits

Posted 27 October 2006 - 03:32 AM

you just measure or count how many indexs on your ouput....

just a suggestion how bout converting int to a string type then strlen

or if you are using string.h you could use <Strtname>.length()

try for this

### #3 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

## Re: size of a variable in bits

Posted 29 October 2006 - 05:57 AM

NyeNye, on 27 Oct, 2006 - 03:32 AM, said:

you just measure or count how many indexs on your ouput....

just a suggestion how bout converting int to a string type then strlen

or if you are using string.h you could use <Strtname>.length()

try for this

Would there be anything wrong with using the sizeofoperator/function?

eg

short int bitsz = sizeof(char) * 8 ; // sizeof returns bytes... 8 bits in byte.. right?

This post has been edited by gregoryH: 29 October 2006 - 05:58 AM

### #4 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: size of a variable in bits

Posted 29 October 2006 - 06:56 AM

gregoryH, on 29 Oct, 2006 - 06:27 PM, said:

8 bits in byte.. right?

Not necessarily. C++ standard says that byte has atleast 8 bits.

### #5 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

## Re: size of a variable in bits

Posted 29 October 2006 - 06:59 AM

Xing, on 29 Oct, 2006 - 06:56 AM, said:

gregoryH, on 29 Oct, 2006 - 06:27 PM, said:

8 bits in byte.. right?

Not necessarily. C++ standard says that byte has atleast 8 bits.

Trust a committee to turn a hrose into a camel....

Sort of goes against the traditional meaning of a byte...

Thanks Xing.. i'll go give them a blast

### #6 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: size of a variable in bits

Posted 29 October 2006 - 07:15 AM

If you want to find out how many bits are there in a byte in your implementation then use CHAR_BIT macro which is defined in <climits>.

### #7 gregoryH

Reputation: 60
• Posts: 656
• Joined: 04-October 06

## Re: size of a variable in bits

Posted 29 October 2006 - 07:21 AM

Xing, on 29 Oct, 2006 - 07:15 AM, said:

If you want to find out how many bits are there in a byte in your implementation then use CHAR_BIT macro which is defined in <climits>.

so I should have used short int bitsz = sizeof(char) * CHAR_BIT

I did a quick look into wikipedia on the humble byte....

Seems it has varied over time from 3 bits up to 12, and with the wide character and unicode characters it may yet have an upgrade...

This post has been edited by gregoryH: 29 October 2006 - 07:23 AM