LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2015, 11:15 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
bitwise operators


Wondering if anyone can explain whatever difference there might be between using
Code:
unsigned int val = val & mask;
or
Code:
unsigned int val = val & ~mask;
for changing bit values to '0'. If a difference exists, when would each expression be preferred?

Last edited by atlantis43; 03-13-2015 at 11:17 PM.
 
Old 03-14-2015, 12:10 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
unsigned int val = val & mask;
This is used to set just bits of val having one that are also one in mask.

Quote:
unsigned int val = val & ~mask;
'~mask' inverts all mask bits. So '1' become '0' and vice versa.

So in whole this sets all the one bits of val which have corresponding bits in mask as zero.
 
1 members found this post helpful.
Old 03-14-2015, 01:33 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
it's a bit like to a = a + b versus a = a + -b
Both do something, just not the same thing.
 
Old 03-14-2015, 02:26 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not like that approach: this is not about preference, they do different things and they will have different result. So use the first one if you need that and use the second if you need that one. And do not use anyone of them if you do not need....
val & mask clears the bits outside of the mask (so will keep only what is masked), val & ~mask will clear the bits specified by mask and will not modify the others.
 
1 members found this post helpful.
Old 03-14-2015, 07:19 AM   #5
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
val & mask clears the bits outside of the mask (so will keep only what is masked), val & ~mask will clear the bits specified by mask and will not modify the others.
The responses received to my query helped me realize the clear difference (using the code example that follows---included just for the use of any other novices with a question similar to mine) that:
1) using the &= mask parsing serves to leave bits ON which are ON in the mask, and turns others OFF, while:
2) using the & ~mask parsing leaves bits unchanged (ON) which are NOT ON in the mask, and turns OFF bits which are ON in the mask.
Thanks again to all.
Code:
#include <stdio.h> 
char * itobs(int, char *); 
void show_bstr(const char *);

int main(void)
{
    char bin_str[8 * sizeof(int) + 1];
    int number, mask;
    int result;
    puts("Enter integers and see them in binary.");
    puts("Non-numeric input terminates program.");
    while (scanf("%u", &number) == 1)
    {
        itobs(number,bin_str);
        printf("%d is ", number);
        show_bstr(bin_str);
        putchar('\n');
        puts("Enter integers for the mask.");
        scanf("%u", &mask);
        itobs(mask,bin_str);
        printf("%d is ", mask);
        show_bstr(bin_str);
        putchar('\n');
       	 // now show results
       
        result = number & mask;
        itobs(result,bin_str);
        puts("result of number &= mask is");
        show_bstr(bin_str);
        putchar('\n');

        result = number & ~mask;
        itobs(result,bin_str);
        puts("result of number & ~mask is");
        show_bstr(bin_str);
        putchar('\n');

    }
    puts("Bye!");
    return 0;

} 
char * itobs(int n, char * ps)  // remember that n is in binary format
{
    int i;
    static int size = 8 * sizeof(int);
    for (i = size - 1; i >= 0; i--, n >>= 1)
        ps[i] = (01 & n) + '0';       // sets val to either 0 + 0 or 1 + 0
    ps[size] = '\0';
    return ps;
}
/* show binary string in blocks of 4 */
void show_bstr(const char * str)
{
    int i = 0;
    while (str[i])
        /* not the null character */
    {
        putchar(str[i]);
        if (++i % 4 == 0 && str[i])
            putchar(' ');
    }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problems with NOT ~ bitwise operator - C++ Skyer Programming 10 09-23-2011 05:02 PM
Bitwise Operators - C Programming User Name. Programming 5 03-12-2007 10:41 PM
Bitwise >> formula concept... debiant Programming 10 08-31-2006 08:28 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
Bitwise operators kamransoomro84 Programming 8 04-22-2004 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration