unsigned int 와(부호있는) int의 차이는 가장 높은 비트(때때로 "sign" 비트로 참조되는 비트)를 해석하는 방법이다. 아두이노에서 int (부호 있음) 형에서, 높은 비트가 "1"이면, 그 수는 음스로 해성되고 다른 15비트는 (2’s complement math)로 해석된다.
문법
unsigned int var = val;var - unsigned int 변수 이름
val - 변수에 할당하는 값
예제 코드
unsigned int ledPin = 13;
주의와 경고
부호없는 변수가 그 최대 용량을 넘으면 0으로 "roll over" 되면, 다른 경우는 아래와 같다:
unsigned int x;
x = 0;
x = x - 1; // x 는 이제 65535 - 음의 방향 으로 roll over
x = x + 1; // x 는 이제 0 - rolls over
부호없는 변수를 가진 수학은 기대하지 않은 결과를 일으킬 수 있다, 부호없는 변수가 롤오버 안 되더라도.
MCU는 아래 규칙을 적용한다:
계산은 대상변수의 scope 에서 이루어진다.
예를들어, 대상변수가 부호 있으면, 두 입력 변수가 부호 없더라도, 부호있는 연산을 한다.
그러나 중간 결과가 필요하면, 중간결과의 scope는 코드에서 정해지지 않는다.
이 경우, MCU 는 부호 없는 연산을 하는데, 왜냐면 두 입력이 부호없기 때문이다!
unsigned int x = 5;
unsigned int y = 10;
int result;
result = x - y; // 5 - 10 = -5, as expected
result = (x - y)/2; // 5 - 10 in unsigned math is 65530! 65530/2 = 32765
// solution: use signed variables, or do the calculation step by step.
result = x - y; // 5 - 10 = -5, as expected
result = result / 2; // -5/2 = -2 (only integer math, decimal places are dropped)
We care about the privacy and personal data of our users.
To continue, please give us your consent:
Please confirm that you have read the privacy policy
Thank you for subscribing!
Curious to learn more?
Are you also a teacher, student, or professional that loves using Arduino in your day-to-day activities?
Then keep up-to-date with either our STEM or Professional monthly newsletters.
Arduino weekly newsletter (already subscribed)
Educators can benefit from the ever growing tech that shapes our environment through fun cool projects.
Why not awe your boss with highly innovative ways to help keep your enterprise connected at no extra cost?
Arduino Survey
We'd like to get to know you little better.
Please help us improve by answering this super short optional survey.