# Bitwise operations related question in code

Dear all.

Recently i bumped into the piece of code which uses the following function bit. The bit and the data type declared as (enum?). Please see the suitable code below. Lets say the code is a custom function which takes 2 arguments (Bus address and tempVol) and then stores it in the latch with the suitable address. The code uses the (bit) function. I barely understand what it does here, and how it works, also what means enum BusAdr ? . Need some help… :o

{
bit AttRel;

TempVol+= Ofs; // Offset is in 1dB steps
if(TempVol>90) TempVol = 90;
else if(TempVol<0) TempVol = 0;

if(TempVol<27)
{
AttRel = TRUE;
TempVol+= 24;
if(TempVol==50)
{
delay1ms(9);
}
}
else
{
AttRel = FALSE;
}

TempVol -= 27;
if(TempVol<0) TempVol = 0;

TempVol<<=2;
if(AttRel==FALSE) TempVol |= 0x02;

delay100us(3);
delay100us(3);

}

TempVol = actual volume
AttRel = Attenuation Relais
BusWrite = writes the value in the latch, with the following address…[/color][/color]

David_ZI:
Recently i bumped into the piece of code which uses the following functions. Byte(), and bit also there is a data type declared as (enum?). Please see the suitable code below. Lets say the code is a custom function which takes 2 arguments and then stores it in the latch with the suitable address. The code uses the (bit) function. I barely understand what it does here, and how it works, also what means enum BusAdr ? . Need some help.. :o

An 'enum' is an 'enumeration' data type. It's a list of names that have integer values. You can treat it like an int.
I don't see Byte() anywhere. Did you mean BusWrite()? That would be a function declared elsewhere in the code or in a library. Find the declaration to see how it works. It probably sends the data to the address on the bus 9whatever bus that is).
'bit' is a data type. It will be declared in a #define or typedef statement somewhere else in the code or in an included file. It appears to act like a boolean, capable of holding only TRUE or FALSE (which themselves are constants defined elsewhere in the code). You could probably use 'boolean' instead of 'bit' and 'true' and 'false' instead of 'TRUE' and 'FALSE'.
This is what it does:
Add the global volume offset 'Ofs' to the new volume.
Constrain the volume to the range 0 through 90.
If the volume is less than 27 {
Remember to turn on the attenuator and add 24 to the volume (range is now 24 to 90)
If volume is now 50 (was 26) send 0x24 (36) to the device and wait 9 mS
} else {remember to turn off the attenuator}
Subtract 27 from the volume (now the range is -3 to 63)
If the volume is below 0, set it to 0 (now the range is 0 to 63)
Shift the volume left 2 bits to multiply by 4. (range is now 0 to 252)
If we remembered to turn the attenuator OFF, set bit 1 (2nd bit) of the volume.
Send the volume to the device.
Wait 300 microseconds
Send the volume with bit 0 set to the device
Wait 300 microseconds
Send the volume to the device

If you mean this function:
https://www.arduino.cc/en/Reference/Bit

It will give the numeric value of a particular bit.

For example bit(2) will be 4.

It can be used to create a mask.

Thanks a lot for info guys. What I still can't understand what exactly those shifting of bits, give us? Lets say what is the practical usage in routine programs?. It will give a value of the particular bit, but what kind of value? and in that case what does the Byte() function?

David_ZI:
Thanks a lot for info guys. What I still can't understand what exactly those shifting of bits, give us? Lets say what is the practical usage in routine programs?. It will give a value of the particular bit, but what kind of value? and in that case what does the Byte() function?

A bit can only have two values - 0 or 1. A byte is made up of 8 bits. Those 8 bits can be used to represent a single number from 0-255 or multiple numbers of smaller size.

One of the more common examples for the use of bit shifting on something like the Arduino is to get information from or write information to the microcontroller. The microcontroller stores a bunch of information about its current state in bits which are arranged in bytes called registers. So if you want a specific bit, you first need to get the value of the whole register and then bit shift things around to get the information you're specifically looking for.

In your code it looks like it's doing something similar where it is saving a couple of pieces of information in the TempVol variable. It looks like it is storing the actual volume value in the upper 6 bits and the attenuation relay statuses in the bottom two. That saves time sending the information from one place to the other.

Correct me if i'm wrong but, that said one can basically take a random number and convert it to bits.
So as an example if I have a analogWrite() value 129 and want to convert it to bits, I should you (bit command or Byte() in API's). And if I have a bits of data and want to convert it to a number I do bitwise operation to get a binary?. Basically it is like an Array where you can access a single element and modify it, but here you have to make bitwise operations to to so?.

In code block above,

{
bit AttRel;

why Attrel is declared as bit? and what means adr=? in front of BusAdr.

There is no "bit" type. It must be defined somewhere as a typedef in the software you did not share with us.

There is no Byte() function defined or used in the tiny part of the software you shared with us.

And Byte() is probably a C++ cast.

sorry guys was wrong block of code. Here is the actual one, the byte WheelPos , and later the 3 small functions below. what is the physical meaning behind?

uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}

uint8_t red(uint32_t c) {
return (c >> 8);
}
uint8_t green(uint32_t c) {
return (c >> 16);
}
uint8_t blue(uint32_t c) {
return (c);
}

The physical meaning is explained in the manual.
https://www.arduino.cc/en/Reference/Bitshift