Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,396 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,111 people online right now. Registration is fast and FREE... Join Now!




size of a variable in bits

 
Reply to this topicStart new topic

size of a variable in bits

dejabu18
27 Oct, 2006 - 02:26 AM
Post #1

New D.I.C Head
*

Joined: 3 Mar, 2006
Posts: 31


My Contributions
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

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


CODE
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
User is online!Profile CardPM
+Quote Post

NyeNye
RE: Size Of A Variable In Bits
27 Oct, 2006 - 02:32 AM
Post #2

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
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
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Size Of A Variable In Bits
29 Oct, 2006 - 04:57 AM
Post #3

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(NyeNye @ 27 Oct, 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


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 Oct, 2006 - 04:58 AM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Size Of A Variable In Bits
29 Oct, 2006 - 05:56 AM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(gregoryH @ 29 Oct, 2006 - 06:27 PM) *
8 bits in byte.. right?

Not necessarily. C++ standard says that byte has atleast 8 bits.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Size Of A Variable In Bits
29 Oct, 2006 - 05:59 AM
Post #5

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Xing @ 29 Oct, 2006 - 06:56 AM) *

QUOTE(gregoryH @ 29 Oct, 2006 - 06:27 PM) *
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
User is offlineProfile CardPM
+Quote Post

Xing
RE: Size Of A Variable In Bits
29 Oct, 2006 - 06:15 AM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
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>.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Size Of A Variable In Bits
29 Oct, 2006 - 06:21 AM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Xing @ 29 Oct, 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>.

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 Oct, 2006 - 06:23 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month