• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Bit shift and recombine negative integer

Q&A Forum › Category: Arduino › Bit shift and recombine negative integer
0 Vote Up Vote Down
Steven Falivene asked 5 years ago

I am trying to convert an integer into two separate bytes and then recombine it (for efficient sending data via Lora). I can do it for positive long variables and integers, but not negative integers.  I did read that the integer must be “signed” so I did it and still not luck. I am measuring temperature which could be positive or negative number.

Help please

signed int temperature = -157;
byte partTemp[2];
void setup()
{

Serial.begin(115200);
partTemp[0] = temperature >> 8;
partTemp[1] = temperature;
signed int pT1 = (signed int)partTemp[0]<<8;
signed int pT0 = (signed int)partTemp[1];
igned int combinedpT = pT1 | pT0;
Serial.println("Array method integer temperature -157"); 
Serial.println(combinedpT, HEX); //
Serial.println(combinedpT); //
}
void loop()
{

}
Question Tags: bit shift
1 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Steven.
I’m not familiar with converting integers into bytes.
Can you read this article and see if it helps: https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-negative-numbers
I’m sorry that I can’t help much.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.