Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

What is This?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

What is This?

by stinkbutt on Mon Oct 25, 2010 6:16 pm

Code: Select all | TOGGLE FULL SIZE
PORTB = 0;      //All of port B should be off.
DDRB =  63U;   // #0011 1111; PB0 - PB6 are our outputs


PORTD = 0;
DDRD =   48U;   // Pin D4, D5 are our LED outputs


Evidently, the "U" in DDRB and DDRD is ever yet still another way to express the same damn number. I understand that I can write, say, 63 out with:

0b00111111

or

0x3F

But why is this code (which I got from EMSL, if you must know) putting a "U" at the end? Wouldn't it just be 63 or 48? This is c written for avr-gcc to be compiled for a 2313 if that's at all helpful...

[EDIT]
To clarify, I'm only asking about the numbering scheme. I understand what DDRB and DDRD are.
[/EDIT]
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: What is This?

by oPossum on Mon Oct 25, 2010 7:11 pm

U = Unsigned
L = Long
LL = Long Long
F = Float (single)
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: What is This?

by skylen on Wed Oct 27, 2010 3:00 am

First, remember that in the source code, "63" is an integer literal value with type "int". In contrast, "63U" is an integer literal value with type "unsigned int". Both signed and unsigned int can represent values that can't be represented in the other type.

Because the DDRx registers are variables defined with an unsigned integral type, assigning a signed integer value may result in compiler warnings since there are signed values that can't be represented in the unsigned type which is the target of the assignment. Obviously since 63 is a literal value, it is clear that it can be represented in an unsigned type, but for consistency's sake, some compiler warning levels will always warn for this type of thing, since implicit signed-unsigned conversions can cause very sneaky bugs. I personally tend to use gcc's -Wsign-conversion flag to enable this warning because I've wasted too much time chasing stupid bugs related to this.

Here's a section from my gcc-warnings.mk makefile:
Code: Select all | TOGGLE FULL SIZE
# For C++, also warn for conversions between NULL and non-pointer types;
# confusing overload resolution for user-defined conversions; and conversions
# that will never use a type conversion operator: conversions to void, the same
# type, a base class or a reference to them. Warnings about conversions between
# signed and unsigned integers are disabled by default in C++ unless
# -Wsign-conversion is explicitly enabled.
C_CXX_WARNING_FLAGS += -Wconversion

# Warn for implicit conversions that may change the sign of an integer value,
# like assigning a signed integer expression to an unsigned integer variable.
# An explicit cast silences the warning. In C, this option is enabled also by
# -Wconversion.
C_CXX_WARNING_FLAGS += -Wsign-conversion
skylen
 
Posts: 56
Joined: Fri Sep 04, 2009 5:54 pm

Please be positive and constructive with your questions and comments.