Performing bitwise operations in C

Posts you want to find years later go here.

Performing bitwise operations in C

Postby Jonathan » Thu Apr 15, 2004 8:14 pm

So I want to say

Code: Select all
int x, y;
y = x[0] ^ x[2] ^ x[17];


There's not a snappy way to write that in C, is there? I'm going to have to do
Code: Select all
y = x & 0x1 ^ x & 0x100 ^ x & 0x100000000000000000;
aren't I?
Last edited by Jonathan on Wed Nov 17, 2004 7:08 pm, edited 1 time in total.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby VLSmooth » Thu Apr 15, 2004 8:38 pm

Assuming x[n] is the nth bit (not an array entry), I think so. :(
User avatar
VLSmooth
Tenth Dan Procrastinator
 
Posts: 3044
Joined: Fri Jul 18, 2003 2:02 am
Location: Varies

Postby quantus » Thu Apr 15, 2004 9:13 pm

Your constants aren't supposed to be more than 8 bits. Your compiler may still handle it ok, but it's still going to be compiled as something like the following. Besides, it's better for readability to do this as well so someone doesn't have to count all the damn 0's.

Code: Select all
int a,b,c;
c = 0x1;
y = x & c ^ x & 0x4 ^ x & (c<<17);


editted to fix binary and hex confusion
Last edited by quantus on Thu Apr 15, 2004 9:56 pm, edited 1 time in total.
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby VLSmooth » Thu Apr 15, 2004 9:21 pm

Might as well be consistent then. Using a combination of a literal (0x100) and a variable isn't very pretty. Why not just:

Code: Select all
int x, y;
y = x & 0x1 ^ x & 0x100 ^ x & (1<<17)

and screw the extra unnecessary variable?
User avatar
VLSmooth
Tenth Dan Procrastinator
 
Posts: 3044
Joined: Fri Jul 18, 2003 2:02 am
Location: Varies

Postby Jonathan » Thu Apr 15, 2004 9:24 pm

Code: Select all
int TWOTOTHE(int x) {
  return 1<<x;
}

y = x&TWOTOTHE(1) ^ x&TWOTOTHE(2) ^ x&TWOTOTHE(17);


I went this route.

I'm surprised no one complained that I mixed binary and hexadecimal when initially describing the problem. ::sniff:: I'm so proud of you!
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby VLSmooth » Thu Apr 15, 2004 9:40 pm

Hex, Binary, only a power of 4 off, no biggie 8)
User avatar
VLSmooth
Tenth Dan Procrastinator
 
Posts: 3044
Joined: Fri Jul 18, 2003 2:02 am
Location: Varies

Postby quantus » Thu Apr 15, 2004 10:01 pm

Dwindlehop wrote:I'm surprised no one complained that I mixed binary and hexadecimal when initially describing the problem. ::sniff:: I'm so proud of you!

Wow, you're right, totally missed that. As for being proud of crappy debugging skillz, you must be smoking something. Anyways, I blame it on understanding your intent and leaving implementation up to the user (ie. you).
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby Jonathan » Thu Apr 15, 2004 10:16 pm

quantus wrote:
Dwindlehop wrote:I'm surprised no one complained that I mixed binary and hexadecimal when initially describing the problem. ::sniff:: I'm so proud of you!

Wow, you're right, totally missed that. As for being proud of crappy debugging skillz, you must be smoking something. Anyways, I blame it on understanding your intent and leaving implementation up to the user (ie. you).

I think understanding my intent and leaving well enough alone is something we can all be proud of.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby Jonathan » Fri Apr 16, 2004 10:54 pm

Whoops. There's a problem with all the code in this thread.
Code: Select all
int TWOTOTHE(int x) {
  return 1<<x;
}

y = x&TWOTOTHE(1) ^ x&TWOTOTHE(2) ^ x&TWOTOTHE(17);

To take mine as an example, y will have the value of x[1]*2^1 + x[2]*2^2 + x[17]*2^17;, which is most definitely not want I want.

I'm going to do this instead.
Code: Select all
int NTHBIT(int x, int n) {
  return (1<<n & x)>>n;
}

Bug sightings or more intelligent solutions welcome.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby quantus » Fri Apr 16, 2004 11:57 pm

ok, you suck. You can't even represent what you want correctly. The new function you wrote barely makes sense in context of what you originally wrote. What you want is a bool essentially? And you want y to be is the xor of those 3 bools? Ok, then what you have now would work most of the time.

There is a bug though. What you don't realize is that >> shifts in x[31]. In most cases you're zeroing out this bit from the bit mask, but if you actually want x[31] and it's 1, you're not going to get what you expect. Completely correct code would be as follows:

Code: Select all
int NTHBIT(int x, int n) {
  return ((1<<n & x)>>n)&1;
}
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby quantus » Sat Apr 17, 2004 12:01 am

Oh, and this is what you get for being proud of us being not overly picky. By trying to understand what you meant, we ignored the complete lack of consistency in your original examples of psuedo-code.
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby Jonathan » Sat Apr 17, 2004 12:05 am

I have dishonored my warrior clan.

Yes, I want the xor of three bools. In my actual implementation, I'm using unsigned long longs instead of ints. I went ahead and took the least significant bit only, though.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby quantus » Sat Apr 17, 2004 12:16 am

long longs? like 64-bit longs?
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby Jonathan » Sat Apr 17, 2004 12:24 am

That's the definition of long long on Intel hardware, yes.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby Jonathan » Tue Apr 18, 2006 9:54 pm

Here's the perl equivalent, so I never have to think again:
Code: Select all
sub bit_slice() {
  $val = shift;
  $pos = shift;
  return ((1<<$pos & $val)>>$pos)&1;
}

Now the question is, how to return a range of bits? I'm talking about the Verilog equivalent of "signal[9:3]".
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Re: Performing bitwise operations in C

Postby quantus » Wed Apr 19, 2006 3:21 am

When did:
Dwindlehop wrote:
Code: Select all
int x, y;
y = x[0] ^ x[2] ^ x[17];


become:
Dwindlehop wrote:
Code: Select all
int TWOTOTHE(int x) {
  return 1<<x;
}

y = x&TWOTOTHE(1) ^ x&TWOTOTHE(2) ^ x&TWOTOTHE(17);


notice that the first index became 1 instead of 0... Looks to be Jonathan's error...
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby quantus » Wed Apr 19, 2006 5:17 am

Try:
Code: Select all
sub bit_slice() {
  $val = shift;
  $pos1 = shift;
  $pos2 = ( $#_ >= 0 ) ? shift : $pos1;
  warn "YOU SUCK!!!" if ( $pos1 > 31 || $pos2 > 31 || $pos1 > $pos2 );
  my $mask = (1<<($pos2-$pos1+1))-1;
  return ( $val >> $pos1 ) & $mask;
}

editted to set $pos2 = $pos1 if there is no 3rd argument
Last edited by quantus on Mon Apr 24, 2006 10:54 pm, edited 2 times in total.
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Re: Performing bitwise operations in C

Postby Jonathan » Wed Apr 19, 2006 6:43 pm

quantus wrote:Looks to be Jonathan's error...
If you're trying to point out that I suck, you won't get any arguments from me.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Postby quantus » Wed Apr 19, 2006 8:45 pm

Have you really done this so often over the last 2 years that you've needed to keep a final solution somewhere?
Have you clicked today? Check status, then: People, Jobs or Roads
User avatar
quantus
Tenth Dan Procrastinator
 
Posts: 4613
Joined: Fri Jul 18, 2003 2:09 am
Location: San Jose, CA

Postby Jonathan » Wed Apr 19, 2006 10:11 pm

Happens more than once, yeah. Usually it's generating cache state.
Jonathan
Grand Pooh-Bah
 
Posts: 5946
Joined: Tue Sep 19, 2006 7:45 pm
Location: Portland, OR

Next

Return to The Vault

Who is online

Users browsing this forum: No registered users and 1 guest

cron