6 Replies - 5964 Views - Last Post: 29 October 2006 - 07:21 AM Rate Topic: -----

#1 dejabu18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: size of a variable in bits

Posted 29 October 2006 - 05:57 AM

View PostNyeNye, 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

Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: size of a variable in bits

Posted 29 October 2006 - 06:56 AM

View PostgregoryH, on 29 Oct, 2006 - 06:27 PM, said:

8 bits in byte.. right?

Not necessarily. C++ standard says that byte has atleast 8 bits.
Was This Post Helpful? 0
  • +
  • -

#5 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: size of a variable in bits

Posted 29 October 2006 - 06:59 AM

View PostXing, on 29 Oct, 2006 - 06:56 AM, said:

View PostgregoryH, 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
Was This Post Helpful? 0
  • +
  • -

#6 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • 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>.
Was This Post Helpful? 0
  • +
  • -

#7 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: size of a variable in bits

Posted 29 October 2006 - 07:21 AM

View PostXing, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1