Connecting Tech Pros Worldwide Help | Site Map

Reduced bit number in short

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 10:19 AM
Philipp
Guest
 
Posts: n/a
Default Reduced bit number in short

Hello
I'm working on a piece of code which I did not write and it has
variables defined as:

unsigned short i:13;
unsigned short j:13;
unsigned short k:3;

As I understand the code, this means that i is reduced to being coded on
13 bits (same for j) and k is reduced to 3 bits.
1) Is my understanding right?
2) Is there any documentation on this ":" operator somewhere (I could
not find anything by searching the web)

Thank you for your answers Phil

  #2  
Old July 22nd, 2005, 10:19 AM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Reduced bit number in short


"Philipp" <nospam_kitschen@hotmail.com> wrote in message news:408fe268$1@epflnews.epfl.ch...[color=blue]
> Hello
> I'm working on a piece of code which I did not write and it has
> variables defined as:
>
> unsigned short i:13;
> unsigned short j:13;
> unsigned short k:3;
>
> As I understand the code, this means that i is reduced to being coded on
> 13 bits (same for j) and k is reduced to 3 bits.
> 1) Is my understanding right?
> 2) Is there any documentation on this ":" operator somewhere (I could
> not find anything by searching the web)
>[/color]

Look up "bitfield" (or perhaps "bit field") in your C text.

: here isn't an operator, it's part of the declaration syntax. Bitfields are a bit funky.
It is used to carve up an integral type into smaller pieces. The packing of the bits
into the larger type is extremely machine dependent.

  #3  
Old July 22nd, 2005, 10:19 AM
Bill Seurer
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

Philipp wrote:
[color=blue]
> 2) Is there any documentation on this ":" operator somewhere (I could
> not find anything by searching the web)[/color]

Search on "bit fields"
  #4  
Old July 22nd, 2005, 10:19 AM
Jacek Dziedzic
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

Ron Natalie wrote:
[color=blue]
> "Philipp" <nospam_kitschen@hotmail.com> wrote in message news:408fe268$1@epflnews.epfl.ch...
>[color=green]
>>Hello
>>I'm working on a piece of code which I did not write and it has
>>variables defined as:
>>
>> unsigned short i:13;
>> unsigned short j:13;
>> unsigned short k:3;
>>
>>As I understand the code, this means that i is reduced to being coded on
>>13 bits (same for j) and k is reduced to 3 bits.
>>1) Is my understanding right?
>>2) Is there any documentation on this ":" operator somewhere (I could
>>not find anything by searching the web)
>>[/color]
>
>
> Look up "bitfield" (or perhaps "bit field") in your C text.
>
> : here isn't an operator, it's part of the declaration syntax. Bitfields are a bit funky.
> It is used to carve up an integral type into smaller pieces. The packing of the bits
> into the larger type is extremely machine dependent.[/color]

Whoa, what are these? I've never seen a thing like that before! :).
Is this Standard C++, or C perhaps? Or a relic of some sorts like
the auto keyword? My compiler gives a "declaration syntax error" for:

// ---
int main() {
unsigned short k:13;
}
// ---

Can anybody shed some light on this?

TIA,
- J.
  #5  
Old July 22nd, 2005, 10:20 AM
Old Wolf
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

Jacek Dziedzic <jacek__NOSPAM__@janowo.net> wrote :[color=blue]
> Ron Natalie wrote:[color=green]
> > "Philipp" <nospam_kitschen@hotmail.com> wrote:
> >[color=darkred]
> >>Hello
> >>I'm working on a piece of code which I did not write and it has
> >>variables defined as:
> >>
> >> unsigned short i:13;
> >> unsigned short j:13;
> >> unsigned short k:3;
> >>[/color]
> > Look up "bitfield" (or perhaps "bit field") in your C text.[/color][/color]

Actually you need to look up "bit-field", if searching the Standard
[color=blue]
> Whoa, what are these? I've never seen a thing like that before! :).
> Is this Standard C++, or C perhaps? Or a relic of some sorts like
> the auto keyword? My compiler gives a "declaration syntax error" for:
>
> // ---
> int main() {
> unsigned short k:13;
> }[/color]

They've been in C for decades but are not widely used. They are only
valid within structs or unions, which is why your code is an error.
Also, bit-fields must be _Bool, int, unsigned int, or signed int,
although it's a common extension to allow "unsigned short" etc.

In the original example, if unsigned short has at least 29 bits,
then there will only be 1 actual unsigned short in the struct,
and "i", "j" and "k" will be packed into it, and whenever you use
i,j,k in expressions, the compiler automatically twiddles the bits.
Otherwise they would be packed into 2 unsigned shorts.

Probably why they are unpopular is that you can't do 'sizeof' on them
and you can't take their address (hence, you can't pass pointers to
them to a function, for example), and the packing method is
implementation-defined. I don't think anyone would really mind if
they were deprecated.
  #6  
Old July 22nd, 2005, 10:20 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

On Wed, 28 Apr 2004 23:01:59 +0200, Jacek Dziedzic
<jacek__NOSPAM__@janowo.net> wrote in comp.lang.c++:
[color=blue]
> Ron Natalie wrote:
>[color=green]
> > "Philipp" <nospam_kitschen@hotmail.com> wrote in message news:408fe268$1@epflnews.epfl.ch...
> >[color=darkred]
> >>Hello
> >>I'm working on a piece of code which I did not write and it has
> >>variables defined as:
> >>
> >> unsigned short i:13;
> >> unsigned short j:13;
> >> unsigned short k:3;
> >>
> >>As I understand the code, this means that i is reduced to being coded on
> >>13 bits (same for j) and k is reduced to 3 bits.
> >>1) Is my understanding right?
> >>2) Is there any documentation on this ":" operator somewhere (I could
> >>not find anything by searching the web)
> >>[/color]
> >
> >
> > Look up "bitfield" (or perhaps "bit field") in your C text.
> >
> > : here isn't an operator, it's part of the declaration syntax. Bitfields are a bit funky.
> > It is used to carve up an integral type into smaller pieces. The packing of the bits
> > into the larger type is extremely machine dependent.[/color]
>
> Whoa, what are these? I've never seen a thing like that before! :).
> Is this Standard C++, or C perhaps? Or a relic of some sorts like
> the auto keyword? My compiler gives a "declaration syntax error" for:
>
> // ---
> int main() {
> unsigned short k:13;
> }
> // ---
>
> Can anybody shed some light on this?[/color]

Bit-fields have been part of C for about 30 years now, before the
publication of the first edition of K&R. They have been part of C++
since its beginning.

But they cannot be used as stand-along objects, only as members of
structs and (in C++ only, of course) classes.

Any decent book on C or C++ should cover them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
  #7  
Old July 22nd, 2005, 10:21 AM
Philipp
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

Thank you all for your answers.
Phil
  #8  
Old July 22nd, 2005, 10:21 AM
Jacek Dziedzic
Guest
 
Posts: n/a
Default Re: Reduced bit number in short

Jack Klein wrote:[color=blue]
>
> Bit-fields have been part of C for about 30 years now, before the
> publication of the first edition of K&R. They have been part of C++
> since its beginning.
>
> But they cannot be used as stand-along objects, only as members of
> structs and (in C++ only, of course) classes.
>
> Any decent book on C or C++ should cover them.
>[/color]

Thanks a lot to both of you!

- J.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.