LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2011, 01:17 PM   #1
minivy
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Rep: Reputation: Disabled
C Language Question


Hi Linux fans,

I am reusing the header file xhci.h in the link below and I have trouble to understand the line of code below.

Link:
http://git.kernel.org/?p=linux/kerne...7e6e08b3b46f5e

Line of code:
unsigned sw_lpm_support:1;

Question1: Why no base type in the code like int, short int, char, etc ?

Question2: What does the character ":1" do?

Thanks in advance
 
Old 11-30-2011, 01:25 PM   #2
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
I'm not so sure about Q2, but as to Q1: if there's no explicit type given in addition to the signed/unsigned qualifier, int is assumed, i.e. "unsigned" == "unsigned int".

…hope this helps.
 
Old 11-30-2011, 02:53 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-30-2011, 03:36 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Q1 & Q2 it is an unsigned bit field, value can be 0 or 1
 
Old 11-30-2011, 04:19 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Q1 already answered perfectly by MrCode.

Q2: It is important that the line you quoted is inside a struct definition and that the following line is:

Code:
	unsigned		hw_lpm_support:1;
Alone and outside a struct definition, the line you asked about might just declare a one bit field as Cedrik said.

Inside a struct definition (which is where such syntax is most commonly used) it should allocate an entire unsigned int but allow that unsigned int to be shared with following bit fields.

So the immediately following hw_lpm_support in the linked code is another bit of the same unsigned int allocated by the declaration of sw_lpm_support.

Edit: I just wrote and tested a program I hoped would clarify the rules I described above. But either I misunderstood the rules myself or something is strange about my gcc.
Code:
#include <stdio.h>
typedef struct { unsigned x:1; unsigned y:1; char z; } a;
typedef struct { unsigned char x:1; unsigned char y:1; char z; } b;
int main(int argc, char *argv[])
{
    printf("sizeof(a)=%d\n", sizeof(a));
    printf("sizeof(b)=%d\n", sizeof(b));
    return 0;
}
The size of b is simple: x and y each use 1 bit of the same char, z is another char, so the total is 2.

I thought a would be 8. x and y each use 1 bit of the same unsigned int, z is an additional char, forcing three more chars for alignment. But actually sizeof(a) was 4, meaning that x forced a full unsigned int into the struct as I expected, but both y and z then used left over bits of that unsigned int. (I don't use this stuff in real work).

Last edited by johnsfine; 11-30-2011 at 04:34 PM.
 
Old 11-30-2011, 06:56 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by johnsfine View Post
I thought a would be 8. x and y each use 1 bit of the same unsigned int, z is an additional char, forcing three more chars for alignment. But actually sizeof(a) was 4, meaning that x forced a full unsigned int into the struct as I expected, but both y and z then used left over bits of that unsigned int. (I don't use this stuff in real work).
On my 64-bit system, using GCC 4.5.2, I received the following warnings when I compiled with the -pedantic compiler option:
Code:
foo.c:3:16: warning: type of bit-field ‘x’ is a GCC extension
foo.c:3:16: warning: type of bit-field ‘y’ is a GCC extension
When I ran the executable, I obtained these results:
Code:
sizeof(a)=4
sizeof(b)=2
I presume this is what you originally anticipated would be the result for 'a'.

P.S. On a 64-bit system, sizeof() returns a long int, thus I had to modify the formatting operator in the printf()s to be "%ld".

--------------------

EDIT: I just re-read your post; it seems I misread it. You anticipated the size of 'a' would be 8, and instead yielded 4... just like I did on my system.

Last edited by dwhitney67; 11-30-2011 at 06:58 PM.
 
Old 12-02-2011, 09:14 AM   #7
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Rep: Reputation: 31
Some reference on Wikipedia, if it helps.

http://en.wikipedia.org/wiki/C_syntax#Bit_fields
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
little question about assembly language !thanks topheraholic Linux - Newbie 2 05-28-2011 04:50 AM
question on expect language javier_ccs Programming 1 01-09-2006 10:44 AM
Question on Grub language Denny711 Linux - Newbie 2 07-17-2005 12:42 AM
Quick Question:Language Problem??? Help michaellok Linux - Software 1 11-24-2004 02:54 PM
language question? gexiaofei Linux - Newbie 1 08-03-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration