473,802 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
7 1796

"Philipp" <no************ *@hotmail.com> wrote in message news:40******** @epflnews.epfl. ch...
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)


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.

Jul 22 '05 #2
Philipp wrote:
2) Is there any documentation on this ":" operator somewhere (I could
not find anything by searching the web)


Search on "bit fields"
Jul 22 '05 #3
Ron Natalie wrote:
"Philipp" <no************ *@hotmail.com> wrote in message news:40******** @epflnews.epfl. ch...
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)

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.


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 "declaratio n syntax error" for:

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

Can anybody shed some light on this?

TIA,
- J.
Jul 22 '05 #4
Jacek Dziedzic <ja************ *@janowo.net> wrote :
Ron Natalie wrote:
"Philipp" <no************ *@hotmail.com> wrote:
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;
Look up "bitfield" (or perhaps "bit field") in your C text.


Actually you need to look up "bit-field", if searching the Standard
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 "declaratio n syntax error" for:

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


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.
Jul 22 '05 #5
On Wed, 28 Apr 2004 23:01:59 +0200, Jacek Dziedzic
<ja************ *@janowo.net> wrote in comp.lang.c++:
Ron Natalie wrote:
"Philipp" <no************ *@hotmail.com> wrote in message news:40******** @epflnews.epfl. ch...
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)

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.


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 "declaratio n syntax error" for:

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

Can anybody shed some light on this?


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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6
Thank you all for your answers.
Phil
Jul 22 '05 #7
Jack Klein wrote:

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.


Thanks a lot to both of you!

- J.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

122
5353
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) { /* array differs from value, do something*/
21
13488
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
3
10364
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte, etc. I need: short = byte+byte, short = byte+byte, etc.
18
4362
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
19
33449
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
3
1380
by: TS | last post by:
short x = 1; //this has build error saying missing conversion x = x + 2; Why can't you just add this? I guess the 1 is a literal that translates to an int32? How would I accomplish this statement?
7
11339
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: function test() { var sValue="short unusuallyLongAndWayTooLongString short2"; var regEx=/\S{10,}/; return regEx.test(sValue);
23
9805
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
30
29699
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.