# example code for bit manupulation

**URL:** <https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301>\
**Category:** Programming\
**Created:** [October 22, 2018, 6:36am UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301 "2018-10-22T06:36:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJITnayak](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@AJITnayak](https://forum.arduino.cc/u/AJITnayak)\
**Post date:** [October 22, 2018, 6:36am UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301/1 "2018-10-22T06:36:57Z")

</div>

Dear all

I have 8 digital Ios. i wanted to read status of 8 digital io as bits and move the value to buffer of character 8 bits which hold status of 8 digital Io. can some one guide me how it can be done

Currently i am

unsigned char ser\_data[10];  
ser\_data[1]=0X0A;

That means it hold the value of 8 digital Io in 0000 1010 format. How can move realistic value of digitalIO ino ser\_data[1] by eliminating 0X0A line

---

<div class="post-metadata">

**Author:** ![DKWatson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dkwatson/32/220535_2.png) [@DKWatson](https://forum.arduino.cc/u/DKWatson)\
**Post date:** [October 22, 2018, 7:10am UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301/2 "2018-10-22T07:10:49Z")

</div>

How are the I/O pins aligned, next to each other, same port, scattered?

How do you want the bit data stored? bit0 = I/O pin0, bit1 = I/O pin1, etc, or what?

You can use masks, bit shifts, or bit-fields in a struct just to name three methods.

More info from you.

---

<div class="post-metadata">

**Author:** ![Lena](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Lena](https://forum.arduino.cc/u/Lena)\
**Post date:** [October 22, 2018, 7:12am UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301/3 "2018-10-22T07:12:48Z")

</div>

Take a look at [`<<`](https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftleft/) and [`|`](https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseor/) Operators - i'm shure you'll find the answer for yourself.  
Greets Lena

---

<div class="post-metadata">

**Author:** ![AJITnayak](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@AJITnayak](https://forum.arduino.cc/u/AJITnayak)\
**Post date:** [October 22, 2018, 8:10am UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301/4 "2018-10-22T08:10:33Z")

</div>

How can i move the value to ser\_data[1] and print Serially

```cpp
unsigned char ser_data[10];
#define LED D1
#define LED1 D0
#define LED2 D2
#define LED3 D3
#define LED4 D4
#define LED5 D5
#define LED6 D6
#define LED7 D7

typedef union
{
  struct
  {
    unsigned INF1: 1;
    unsigned INF2: 1;
    unsigned INF3: 1;
    unsigned INF4: 1;
    unsigned INF5: 1;
    unsigned INF6: 1;
    unsigned INF7: 1;
    unsigned INF8: 1;
  };
  unsigned char INF1_9;
} INPUT_TYPE;

INPUT_TYPE INPUT_T;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  pinMode(D0, INPUT);
  pinMode(D1, INPUT);
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);
  pinMode(D4, INPUT);
  pinMode(D5, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);

}

void loop()
{
  // put your main code here, to run repeatedly:

  if (digitalRead(D0) == 1)
  {
    INPUT_T.INF1 = 1;

  } else
  {
    INPUT_T.INF1 = 0;
  }

  if (digitalRead(D1) == 1)
  {
    INPUT_T.INF2 = 1;
    Serial.println("Pin high");
  } else
  {
    INPUT_T.INF2 = 0;
    Serial.println("Pin LOW");
  }

  if (digitalRead(D2) == 1)
  {
    INPUT_T.INF3 = 1;

  } else
  {
    INPUT_T.INF3 = 0;
  }

  if (digitalRead(D3) == 1)
  {
    INPUT_T.INF4 = 1;
    Serial.println("Pin high");
  } else
  {
    INPUT_T.INF4 = 0;
    Serial.println("Pin LOW");
  }

  if (digitalRead(D5) == 1)
  {
    INPUT_T.INF5 = 1;

  } else
  {
    INPUT_T.INF5 = 0;
  }

  if (digitalRead(D6) == 1)
  {
    INPUT_T.INF6 = 1;
    Serial.println("Pin high");
  } else
  {
    INPUT_T.INF6 = 0;
    Serial.println("Pin LOW");
  }

  if (digitalRead(D7) == 1)
  {
    INPUT_T.INF7 = 1;

  } else
  {
    INPUT_T.INF7 = 0;
  }

  delay(1000);

}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:25pm UTC](https://forum.arduino.cc/t/example-code-for-bit-manupulation/552301/5 "2021-05-05T23:25:42Z")

</div>

Closed on May 5, 2021, 11:25pm UTC


