 # bit writer require help

Hi I am new to programming, I am trying to create bytes, but i keep getting 28(11100) when im expecting 170(10101010). I suspect it is because I did not add 0b eg. 0b10101010 but it wont allow me to place 0b in front of the equation.

code:- void loop() int bit1 = 1; int bit2 = 0; int bit3 = 1; int bit4 = 0; int bit5 = 1; int bit6 = 0; int bit7 = 1; int bit8 = 0; byte b; b = (bit1*2^7)+(bit2*2^6)+(bit3*2^5)+(bit4*2^4)+(bit5*2^3)+(bit6*2^2)+(bit7*2^1)+(bit8*2^0); //equals 170 if 10101010 Serial.println(b, BIN);

Why all the XOR operations?

but it wont allow me to place 0b in front of the equation

What is "it"?

AWOL: Why all the XOR operations?

Probably teaching you to suck eggs but ^ is BASIC syntax for raise to the power of.

Yes, I know BASIC - I still have the flashbacks, but after all these years, they're getting less frequent.

i am using XOR as it is the only way I know

the arduino software wont let me put 0b infront of the long equation to form

``````b = 0b(bit1*2^7)+(bit2*2^6)+(bit3*2^5)+(bit4*2^4)+(bit5*2^3)+(bit6*2^2)+(bit7*2^1)+(bit8*2^0)
``````

AWOL: Yes, I know BASIC - I still have the flashbacks, but after all these years, they're getting less frequent.

And just as your getting over it I shove an egg in your mouth. XD

Onmt39:
i am using XOR as it is the only way I know

the arduino software wont let me put 0b infront of the long equation to form

``````b = 0b(bit1*2^7)+(bit2*2^6)+(bit3*2^5)+(bit4*2^4)+(bit5*2^3)+(bit6*2^2)+(bit7*2^1)+(bit8*2^0)
``````

But you shouldn’t be using XOR at all - that’s your problem.

``````b = (bit1 << 7)+(bit2 << 6)+(bit3 <<5) . . . etc
``````

Though I’d prefer to see the LSB named bit0.

Thank you AWOL its working now i changed the LBS to bit0, my next plan is to use a Xbox 360 controller with USB host to control an arduino rc car.

I want to use 'hand shake' protocol so the arduino knows the RC car has received the message. The only way i could think of to do such is transmit the exact same message back to arduino no.1 . what do you think about this?

Onmt39: I want to use 'hand shake' protocol so the arduino knows the RC car has received the message. The only way i could think of to do such is transmit the exact same message back to arduino no.1 . what do you think about this?

Or you could just check-sum the message and send the checksum back, depending on how long/complex the transmission processes are.

Try looking up << in the reference section of this site.
Also see & and |
Try dabbling with such code as

``````byte a

void setup()
{
Serial.begin(9600);
a = 32;
a = a | (1<<3);
a = a | (1<<7);
Serial.println(a,BIN);
}
``````

Hope this gives you some syntax to find out how stuff works.

Done in a Loop a different way.

``````void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
byte bits[] = {0, 1, 0, 1, 0, 1, 0, 1};
byte b = 0;
for (byte i = 0; i < 8; i++)
{
bitWrite(b, i, bits[i]);
}
Serial.print(b);
Serial.print("  ");
Serial.println (b, BIN);
}
``````
``````byte bits[] = {0, 1, 0, 1, 0, 1, 0, 1};
``````

That's slightly counter-intuitive - that looks like 0x55 to me, not 0xAA.

byte bits = {0, 1, 0, 1, 0, 1, 0, 1};

That’s slightly counter-intuitive - that looks like 0x55 to me, not 0xAA.

It’s binary, not BCD. Also, You’re looking at it backwards. LSB is at the left.

However that can be changed to LSB on right.

It's binary, not BCD

You can represent 0xAA in BCD? Who knew?

Also, You're looking at it backwards

No, you've presented it backwards.

So here…!

``````void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
byte bits[] = {1,0,1,0,1,0,1,0};
byte b = 0;byte j = 0;
for (byte i = 7; i < 8; i--)
{
bitWrite(b, i, bits[j]);j++;
}
Serial.print(b);
Serial.print("  ");
Serial.println (b, BIN);
}
``````

I only showed the other because that’s what i already had for something else

that looks like 0x55 to me, not 0xAA.

Actually was 85 in decimal.

Actually was 85 in decimal

So, not 0x55? Damn.

Says Who?

I am trying to create bytes, but i keep getting 28(11100) when im expecting 170(10101010).

The OP is using Decimal and binary, No Hex!

thank you steinie44 i tested you code and worked well
I cant understand this though

`````` for (byte i = 7; i < 8; i--)
{
bitWrite(b, i, bits[j]);j++;
}
``````

I understand ‘for’ with the initialize, test and increment, i just don’t know what is happening.

Also how would I change the individual bits without requiring to change everything.
eg. (i didn’t know how else to say it)

``````if (door is open){
bit3 = 1
}
if(door is closed){
bit3 = 0
}
``````
``````bit3 = 1
``````

bitWrite