## how to AND a variable by bit coded values (e.g. B00001100) ?

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### how to AND a variable by bit coded values (e.g. B00001100) ?

how can I do this on my Raspi?

altAB &= B00001100;

It works fine on my Arduino!

Code: Select all

``````#define  pin_encA   4
#define  pin_encB   5
volatile int8_t altAB;
int8_t schrittTab[16] = {0,0,0,0,1,0,0,-1, 0,0,0,1,0,0,-1,0};

in32_t  encval;

void compenc {
altAB <<= 2;
altAB &= B00001100;
encval += schrittTab[altAB];
}``````
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jamesh
Raspberry Pi Engineer & Forum Moderator
Posts: 23679
Joined: Sat Jul 30, 2011 7:41 pm

### Re: how to AND a variable by bit coded values (e.g. B0000110

You need to convert the binary number to hex or decimal (or octal..). C doesn't have a 'binary' representation. Hex rep is with 0x in front of it,

num &= 0x[hexnum]

eg

num &= 0x34fa
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

a pity!
also not a lib for binary representation available?

otherwise FTM I would take as workaround

#define B00001100 12

would that be correct?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:altAB &= B00001100;
Arduino has a header file <binary.h> that says:

Code: Select all

``#define B00001100 12``
You could use that file on other platforms, but it is obviously not practical to scale it beyond 8bit values.

GCC supports binary constants of the form 0b00001100 as an extension to the language (including on Arduino), as long as you do not compile with -pedantic-errors.

The portable solution is to use hex.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

yes, thank you a lot,
0b00001100
works absolutely fine!

#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

DougieLawson
Posts: 36130
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:yes, thank you a lot,
0b00001100
works absolutely fine!

Isn't it easier to combine each block of 4-bits into a hex digit. So 0b00001100 becomes 0x0C. That's a lot easier as soon as you're working with more than 8-bits.

0xABCD is much easier than using something like (0b10101011 << 8) | 0b11001101
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

This is a doctor free zone.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

no, the binary thing is fine and I want to keep the code compatible to my Arduino code which works absolutely perfect and accurate!

0b0001100 is absolutely ok now!
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

DougieLawson
Posts: 36130
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK

### Re: how to AND a variable by bit coded values (e.g. B0000110

You'll regret it later, get to 128-bits and it's even a pain in hex.

Code: Select all

``````0x2a0014504009801000000000000200e
== 0b0010101000000000000101000101000001000000000010011000000000010000000000000000000000000000000000000000000000000010000000001110``````
Which is why we abbreviate that hex to 2a00:1450:4009:801::200e

Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

This is a doctor free zone.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

why don't you stop arguing and spamming and trolling?
I need it just for 1 special purpose, and I'm fine for this unique special purpose!

It' finally my code, and 0b0001100 works, so what ??
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jamesh
Raspberry Pi Engineer & Forum Moderator
Posts: 23679
Joined: Sat Jul 30, 2011 7:41 pm

### Re: how to AND a variable by bit coded values (e.g. B0000110

Because it's not standard C, bad practice, and not extensible.

But apart from that....

citation: I've been writing C for 30 years, 28 of those professionally.

(Trying to educate is not trolling)
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:a pity!
also not a lib for binary representation available?
In C++, there's std::bitset that might do what you want in a flexible way.
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

jamesh, to me just code compatibility to my own code was my demand, I didn't expect to be taught in a lecturing attitude - and didn't ask for that - about theoretical compatibilities or portabilities which do not affect me.
I just wanted to make my Arduino code work by B00001100 or sth quite similar.

got it?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

DougieLawson
Posts: 36130
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:why don't you stop arguing and spamming and trolling?
I'll stop when the Raspberry Pi Foundation tells me to stop. I suspect when you've got nearly 17,000 posts on the forum we may take you for something less than a beginner with an attitude problem. I'm quite happy being an expert with an attitude problem.

I'd also suggest you revise your definition of trolling.
Ubuntu Forums wrote:An "Internet troll" or "Forum Troll" is a person who posts outrageous message to bait people to answer. Trolls delight in sowing discord on the forums. A troll is someone who inspires flaming rhetoric, someone who is purposely provoking and pulling people into flaming discussion.

I've not yet added you to my forum "foes" list, but that is not far away.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

This is a doctor free zone.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

please do it, the earlier, the better!
you do not know how much I hate nitpickers, pedants, and sticklers !
(or perhaps you even do, but I don't care...)

I keep stuck to what I said
the binary thing is fine and I want to keep the code compatible to my Arduino code which works absolutely perfect and accurate!
0b0001100 is absolutely ok now!
why can't you just let this statement stand just as it is?

Re: C++
Postby DougieLawson » Sun Oct 25, 2015 9:20 am
If my main function doesn't set a return code then it won't return an int, therefore void reflects exactly what the code is doing. I don't care about your stinking standards.
viewtopic.php?f=33&t=123690#p833665

Again
mahjongg wrote: it seems here are writing unruly toddlers.
viewtopic.php?f=33&t=123690&start=25#p833733
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

mahjongg
Forum Moderator
Posts: 12228
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

### Re: how to AND a variable by bit coded values (e.g. B0000110

you seem to act as that what you claim to hate!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:please do it, the earlier, the better!
you do not know how much I hate nitpickers, pedants, and sticklers !
Unfortunately for you, nitpicking, pedantry and stickling are excellent traits for programmers. Did you know that if your program has even the tiniest error it won't work? If you are not a stickler for accuracy your programs will be pretty poor.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

### Re: how to AND a variable by bit coded values (e.g. B0000110

davenull wrote:didn't ask for that - about theoretical compatibilities or portabilities which do not affect me. I just wanted to make my Arduino code work by B00001100 or sth quite similar.
So you don't care about "compatibilities" or "portabilities" but you just want your code on one system, the Arduino, to work on another, the RPi?

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

in short words,
yes, I have tons of code written for Arduino Sketch targeting both AVR (Uno, Mega) and ARM cpus (Due, partially Zero) and the Arduino language plus it's 3rd party libs are partially designed peculiarly quite strange. Finally Arduino language IS not C but USES C, by gpp C/C++ and a lot of Java wraps around the IDE and the API.

I now will need to make the Raspi compiler understand my bunch of Arduino code.
the bit/binary thing was not by me but also by another author, it might have been easy though to change either one by a decimal or hex coding, but I wanted to keep the included libs kind of "unspoiled" as possible not to muddle things up, accidentally and unintentionally.
This special coding was for a rotary encoder application I'm currently still despairing about for it's implementation to the Raspi - for lack of hardware or Timer IRQs at processor hardware level.
But that's another issue - thanks for your interest!
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

### Re: how to AND a variable by bit coded values (e.g. B0000110

you seem to act as that what you claim to hate!
I'm just reacting to OT spamming and trolling posts, and I have already reported the spam and troll posts to be removed.
Anyway, actually it's not surprising to me that the spammers and trolls complain about being identified and addressed as spammers and trolls.

But yes, I appreciate if you closed this topic because the issue is resolved (of course, AFTER the troll posts finally HAD been removed).
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jamesh
Raspberry Pi Engineer & Forum Moderator
Posts: 23679
Joined: Sat Jul 30, 2011 7:41 pm

### Re: how to AND a variable by bit coded values (e.g. B0000110

Amazing.

Locking.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."