Go Down

Topic: 1UL = unsigned long but how to write 1 unsigned char? (Read 200 times) previous topic - next topic

alexblade

1UL = unsigned long but how to write 1 unsigned char?

1U - unsigned integer

1UC error
1CU error

JaBa

1 would work just fine unless you mean the ASCII representation which would then be '1'.

Binary will work as well B00000001.

Montmorency

#2
Apr 18, 2019, 06:12 pm Last Edit: Apr 18, 2019, 07:27 pm by Montmorency
You can just say

Code: [Select]
(unsigned char) 1

Note though that in C and C++ values of smaller integral types (`char` and `short`) are virtually always promoted to type `int` before any further processing in expressions. This process is called integral promotions.

For this reason there's usually no need and no point in forcing integral values to have smaller integral types. For the very same reason the language does not provide dedicated suffixes for literals of such types. Plain `1` will work just as well, without being forced into a smaller type.

Why do you need this? In what context?

Jiggy-Ninja

Use UINT8_C(1). You might have to #include <stdint.h> to make it work since that's where the macro is defined. If you're interested in what it actually does, here's the excerpt.

Code: [Select]
#define __CONCAT(left, right) left ## right

#define INT8_C(value) ((int8_t) value)
#define UINT8_C(value) ((uint8_t) __CONCAT(value, U))
#define INT16_C(value) __CONCAT(value, L)
#define UINT16_C(value) __CONCAT(value, UL)
#define INT32_C(value) ((int32_t) __CONCAT(value, LL))
#define UINT32_C(value) ((uint32_t) __CONCAT(value, ULL))
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

marco_c

Standard derived types are already available with no additional effort.
int8_t, int16_t, int32_t
uint8_t, uint16_t, uint32_t
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

gfvalvo

but how to write 1 unsigned char?
Why would you need to do that? The main use for xxxxUL is to ensure that compile-time arithmetic is done using 32-bit math rather than 16-bit. What are you trying to achieve? Truncating compile-time arithmetic to 8 bits? If that's the case, just assign the result to a 'uint8_t' or 'const uint8_t'.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

alexblade

gfvalvo, because I get error "cannot convert 1U to unit8_t" for
unit8_t &port = 1;

but as it turned out later, error was not  in "convert" error was in const :)
const unit8_t &port = 1;

is working , thank you all

Robin2

In the Arduino world "byte" is unsigned char

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Montmorency

#8
Apr 19, 2019, 03:08 pm Last Edit: Apr 19, 2019, 03:12 pm by Montmorency
but as it turned out later, error was not  in "convert" error was in const :)
const unit8_t &port = 1;

is working , thank you all
Now it appears to be an XY problem even more than it was originally. The question is really why one'd even do that:

Code: [Select]
const unit8_t &port = 1;

What is the point of using a reference here? It has a truckload of cons and not a single pro.

Why not just a regular

Code: [Select]
const unit8_t port = 1;

?



gfvalvo

What is the point of using a reference here? It has a truckload of cons and not a single pro.
I have a feeling it's related to this thread. Same variable name.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

alexblade

What is the point of using a reference here?


because its a port register
&portc = PINC;

one'd neede because in some case Im not send all arguments

is there in my logic some wrong ?

Go Up