Need to find size of my sensor data in bits

unsigned int sound=A0;
void setup()
{
  pinMode(sound,INPUT);
  Serial.begin(9600);
}
void loop()
{
  int a=analogRead(sound);
  Serial.println(a);
  delay(100);
}

i get my sensor data in int a; i like to know lengrh of my sensor data int a in bits.

Welcome to the forum

The size of an int will depend on which processor that your Arduino uses

Try

Serial.println(sizeof(int));

It will tell you how many bytes an int takes on your system and each byte has 8 bits

Try __builtin_clz

but i need to find bit value

You doing an analogRead, so unless you're on a Due, it's not going to exceed ten bits.

1. Assume that you are using Arduino UNO.
2. You can see in Fig-1 below, the ADC provides 10-bit data (B9 - B0) fo any sample of DC signal presented to its input channel.

3. Step-2 says that you are getting 10-bit data (range: 0000000000 to 1111111111 in base-2 = 0 to 1023 in base-decimal) for a sample of signal coming from your sensor. If you want to store the data in variable a, it is required that you declare the variable with an appropriate data type.

4. In Arduino/C, the following three are among many avilable data types:

byte  (size 8-bit: range: 0 to 255 decimal-base = 0x00 to 0xFF in hex-base)
int  (size 16-bit:  range: -32768 to 0 to 32767 = 0x8000 to 0x0000 to 0x7FFF)
usigned int  (size 16-bit: range: 0 to 65535 = 0x0000 to 0xFFFF)

5. There is no data type to accomodate your data of range: 0 to 1023. So, you have to use unsigned int for the data type of your variable a. (You can also use int type as its range is: -32768 to 0 to 32767.)

6. Finally, your sensor data is actually 10-bit; but the variable a holds 16-bit data; where, the upper 6-bit are always 0s.

7. Conceptual diagram for the internal details of the ADC of ATmega328P MCU


Figure-1:

If you want to find the value of each bit in the int then the easy way is to use the bitRead() function. Alternatively you can look to use bit masking but might find that more complicated

1. Any bit of an expression is associated with:
(1) Positional Factor,
(2) Positional Weight, and
(3) Positional Value.

2. For example:
Given:

byte z = 0b10101101;  //z7 z6 z5 z4 z3 z2 z1 z0

(1) Bit-3 (z3) has Positional Factor of: 1
(2) Bit-3 (z3) has Positional Weight of: 2^3 = 8
(3) Bit-3 (z3) has Positional Value of: Positional Factor * Positional Weight = 1*8 = 8.

Now, you figure out the meaning of your saying : but i need to find bit value

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.