Inserting the contents of an Array into an integer problem

Hello all,
this is my first post here, but i have been working on Arduino for quite some time now.
For instance i got some cars to work with servo’s and ultrasonic sensors, calibrated the speed of the wheels using infrared sensors, eventually also controlled by means of a remote control.

This time I am trying to read a DHT22 sensor without the use of a library.
Yeah yeah i know… but i still want to be able to achieve this and understand the way these things work and be able to program the code for that.
I have succeeded in initializing the sensor and reading the 40 bits into 5 arrays, each holding 8 positions to represent the bits.

Now i am having trouble putting together the first two of the arrays and convert that to the humidity value.
From the datasheet i understand that the first sixteen bits sent by the sensor are the humidity value times 10.

So to get the principle working i created a small separate program to just get that to work. Right now i have an array containing 16 values (1 or 0). I want to convert this to an integer, by shifting the bits into the integer which should work because that would be a 16 bit value.

But i am getting the strange result of 16 one’s in front of my actual values when showing it on the serial monitor.

int TwoBytes=0x0000;
int Byte1[]={1,0,1,0,1,1,0,0,1,1,1,0,1,1,1,1};


void setup()
{
	Serial.begin(9600);
  /* add setup code here */
 
 Serial.println(TwoBytes,BIN);
 for (int i=0;i<16;i++)
	 {
		 Serial.print(Byte1[i],BIN);
	 }
 Serial.println();
 
   
 
for (int i=0;i<16;i++)
	{
			TwoBytes += (Byte1[i]<<(15-i));
			Serial.print("Step:");
			Serial.print(i);
			Serial.print(" ");
			Serial.println(TwoBytes,BIN);
	}



}

void loop()
{
	
	
  /* add main program code here */

}

I know that the first loop works since it correctly shows the contents of the array, and this is what i would expect to show up in step 15 (see below).
The second loop gives the result described above and shown below:

Opening port
Port open
0
1010110011101111
Step:0 11111111111111111000000000000000
Step:1 11111111111111111000000000000000
Step:2 11111111111111111010000000000000
Step:3 11111111111111111010000000000000
Step:4 11111111111111111010100000000000
Step:5 11111111111111111010110000000000
Step:6 11111111111111111010110000000000
Step:7 11111111111111111010110000000000
Step:8 11111111111111111010110010000000
Step:9 11111111111111111010110011000000
Step:10 11111111111111111010110011100000
Step:11 11111111111111111010110011100000
Step:12 11111111111111111010110011101000
Step:13 11111111111111111010110011101100
Step:14 11111111111111111010110011101110
Step:15 11111111111111111010110011101111

After changing the count (…;i<14;… and …<<(14-i)…)in the second “for loop” the result is this:

Port open
0
1010110011101111
Step:0 100000000000000
Step:1 100000000000000
Step:2 101000000000000
Step:3 101000000000000
Step:4 101010000000000
Step:5 101011000000000
Step:6 101011000000000
Step:7 101011000000000
Step:8 101011001000000
Step:9 101011001100000
Step:10 101011001110000
Step:11 101011001110000
Step:12 101011001110100
Step:13 101011001110110
Step:14 101011001110111

But now i am missing the final bit.

Anyone know how to help me on this?
I have been at it for quite some time now and come to the conclusion that i am stuck.

Thanks in advance, any help is appreciated!

You should use an unsigned integer for the target integer,
there is no reason to add zeros to the target, it is not even neccessary to add, it can be a simple or.

uint16_t value;
byte bit[] = {1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1};

void setup() {
  Serial.begin(250000);
  for (int i = 0; i < 16; i++) {
    if (bit[i]) {
      value |= 1 << (15 - i);
    }
    Serial.print(F("Step:"));
    if (i < 10) {
      Serial.write(' ');
    }
    Serial.print(i);
    Serial.write(' ');
    Serial.println(value, BIN);
  }
  Serial.print(F(" Input  "));
  for (int i = 0; i < 16; i++) {
    Serial.print(bit[i]);
  }
  Serial.println();
}

void loop() {}
Step: 0 1000000000000000
Step: 1 1000000000000000
Step: 2 1010000000000000
Step: 3 1010000000000000
Step: 4 1010100000000000
Step: 5 1010110000000000
Step: 6 1010110000000000
Step: 7 1010110000000000
Step: 8 1010110010000000
Step: 9 1010110011000000
Step:10 1010110011100000
Step:11 1010110011100000
Step:12 1010110011101000
Step:13 1010110011101100
Step:14 1010110011101110
Step:15 1010110011101111
 Input  1010110011101111

@OP

Are you looking for something that has been shown in the screenshot?
sm42.png

Codes:

int TwoBytes = 0x0000;
bool Byte1[16] = {1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1};

void setup()
{
  Serial.begin(9600);
  /* add setup code here */

  Serial.println(TwoBytes, BIN);
  for (int i = 0; i < 16; i++)
  {
    Serial.print(Byte1[i], BIN);
  }
  Serial.println();

  uint16_t x = 0;
  for (int i = 15, j = 0; i >= 0, j < 16; i--, j++)
  {
    bitWrite(x, i, Byte1[j]);
  }

  Serial.print(x, BIN);
  Serial.print(" = ");
  Serial.print(x, HEX);
  Serial.print(" = ");
  Serial.println(x, DEC);

  /*

    for (int i=0;i<16;i++)
    {
      TwoBytes += (Byte1[i]<<(15-i));
      Serial.print("Step:");
      Serial.print(i);
      Serial.print(" ");
      Serial.println(TwoBytes,BIN);
    }

  */

}

void loop()
{


  /* add main program code here */

}

sm42.png

Whandall:
You should use an unsigned integer for the target integer,
there is no reason to add zeros to the target, it is not even neccessary to add, it can be a simple or.

Excellent! Indeed it was the unsigned part that did it.
Good tip on the Or function as well, anything to make code more efficient.

Thank you both!

I should have asked yesterday, I wouldn't have lost the sleep I did... :o