LinuxQuestions.org
Review your favorite Linux distribution.
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 03-22-2011, 06:29 PM   #1
jyothidamu
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Rep: Reputation: 0
Regarding the 16 bit enumeration in c lang


enum argu {
send = 0x0001
, receive = 0x0002

};


can any one explain what is the meaning of the 0x0001 and 2. i am very glad if any one can explain with example
 
Old 03-22-2011, 06:50 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
0x0001 is the hexadecimal equivalent of the value of 1. I'll let you guess what 0x0002 is.

As for the enumerated identifiers, they were chosen to uniquely identify a value. Thus "send" is 0x0001 and "receive" is 0x0002. Whether these numbers actually mean anything, or were arbitrarily chosen, cannot be deduced from the code snip you provided.

Note, when an enumerated list is defined, if the first identifier is not assigned a value, then it will be assigned the default value of zero; all subsequent identifiers will have a value of one more than the previous, unless specifically assigned a value. For example:
Code:
enum Foo
{
   ZERO,           // has the value of 0
   APPLE,          // has the value of 1
   ORANGE = 3,     // obviously has value of 3
   TOMATO,         // has the value of 4
   ZUCCHINI = 6    // obviously has value of 6
};
Enumerated values are not 16-bit values, but instead allocated a full 32-bits (4 bytes). See for yourself on your system using this program:
Code:
#include <stdio.h>

enum Foo
{
   ZERO,
   ONE
};

int main()
{
   printf("sizeof(ZERO) = %u\n", sizeof(ZERO));
   return 0;
}

Last edited by dwhitney67; 03-22-2011 at 06:53 PM.
 
Old 03-22-2011, 07:44 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jyothidamu View Post
enum argu {
send = 0x0001
, receive = 0x0002

};


can any one explain what is the meaning of the 0x0001 and 2. i am very glad if any one can explain with example
The answer is (at least) twofold:
  1. in C99 standard ( http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf ) look for 'hexadecimal';
  2. if you do not know what hexadecimal in particular and radix in general are, read http://en.wikipedia.org/wiki/Radix .
 
Old 03-23-2011, 04:31 AM   #4
jyothidamu
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by dwhitney67 View Post
0x0001 is the hexadecimal equivalent of the value of 1. I'll let you guess what 0x0002 is.

As for the enumerated identifiers, they were chosen to uniquely identify a value. Thus "send" is 0x0001 and "receive" is 0x0002. Whether these numbers actually mean anything, or were arbitrarily chosen, cannot be deduced from the code snip you provided.

Note, when an enumerated list is defined, if the first identifier is not assigned a value, then it will be assigned the default value of zero; all subsequent identifiers will have a value of one more than the previous, unless specifically assigned a value. For example:
Code:
enum Foo
{
   ZERO,           // has the value of 0
   APPLE,          // has the value of 1
   ORANGE = 3,     // obviously has value of 3
   TOMATO,         // has the value of 4
   ZUCCHINI = 6    // obviously has value of 6
};
Enumerated values are not 16-bit values, but instead allocated a full 32-bits (4 bytes). See for yourself on your system using this program:
Code:
#include <stdio.h>

enum Foo
{
   ZERO,
   ONE
};

int main()
{
   printf("sizeof(ZERO) = %u\n", sizeof(ZERO));
   return 0;
}
what is the use of Hexadecimal values these numbers are arbitrarily chosen and thanks for the good explanation
 
Old 03-23-2011, 06:42 AM   #5
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 jyothidamu View Post
what is the use of Hexadecimal values these numbers are arbitrarily chosen and thanks for the good explanation
I'm not sure if this is a question... but oftentimes, hex values are used in lieu of decimal because it makes them easier to read. All numbers within a computer are stored in binary, thus the use of hex or even base-10 numbers serves no purpose other than to gratify humans, who both develop and read, code.
 
Old 03-23-2011, 11:00 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jyothidamu View Post
what is the use of Hexadecimal values these numbers are arbitrarily chosen ...
It depends. I.e. if a program is to deal with an HW controller, the numbers are to be values to be written the HW controller control register.

If you convert the numbers into binary, you'll see that in your case both number have just one bit set.
 
Old 03-23-2011, 08:21 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by jyothidamu View Post
enum argu {
send = 0x0001
, receive = 0x0002

};


can any one explain what is the meaning of the 0x0001 and 2. i am very glad if any one can explain with example
Those aren't 16-bit numbers. Those zeros just mean nothing, maybe they're to align the code or make it look nicer. You could write 0x1 or 0x0000000000000001 and it would mean the same thing to the compiler.
 
1 members found this post helpful.
Old 04-18-2011, 05:46 PM   #8
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
Hexadecimal is used because these are masks for bits. Very long ago I wrote some assembler routines that did serial input/output directly, inputting or outputing characters. There was a data register and a status register, the last bit of the status register was set if a character was still being sent, the previous one if a character had been received but not processed. To send a string, one sent the first character to the data register, waited till the last bit was unset, then sent the next and so on. Similarly with input. To see if a character had just been received, one loaded "receive" to a register, logical anded it with the status register: if the result was non-zero, one could copy the resultant character from the input data register, which would reset the status bit ready for the next character to be received.
 
Old 04-18-2011, 06:13 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by dwhitney67 View Post
I'm not sure if this is a question... but oftentimes, hex values are used in lieu of decimal because it makes them easier to read. All numbers within a computer are stored in binary, thus the use of hex or even base-10 numbers serves no purpose other than to gratify humans, who both develop and read, code.
Also, since each hex digit represents exactly 4 bits, it is almost trivial to create code in very low-level languages like assembler to convert numbers to their human-readable ASCII format as hexadecimal. Same for octal. Converting to ASCII in a decimal radix is comparatively difficult to code, requires larger code and is slower.

--- rod.
 
  


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
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature canonas Linux - Software 3 06-16-2008 04:00 AM
Linux USB Enumeration Vs Windows USB Enumeration rajasekarpadmanaban Linux - Software 1 05-02-2007 03:36 AM
What is the use of lang in <head lang=''> tooparam Programming 2 02-01-2006 05:55 PM
Process Enumeration with C++ amitrawal_17 Programming 2 06-08-2004 01:16 AM

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

All times are GMT -5. The time now is 04:38 AM.

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