ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In a text description of Bitwise &, it is mentioned:
Quote:
The bitwise AND operator is often used with a mask.-----
One common C usage is this statement:
ch &= 0xff;
This mask leaves the final 8 bits of ch alone and sets the rest to 0. Regardless whether the original ch is 8 bits, 16 bits, or more, the final value is trimmed to something that fits into a single byte.
Though I'm not aware of the exact purpose of such "truncation" in coding, when I run the following:
Code:
#include <stdio.h>
int main(void)
{
char MASK = 0xff;
unsigned int a = 521027;
unsigned int b;
b = a & MASK;
printf("b is now u\n",b);
return 0;
}
I get the following output, which does not appear "truncated" to an eight bit value:
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,155
Rep:
use unsigned int for MASK at the moment MASK is a char which is 8 bits but you are appling it to an int which can vary in size depending on the architecture of you machine, and could be anything up to 32 bits long, using tthe same size will mean you wont have undefined bits at the top end.
Thanks to both replies, I now see it working properly. It seems that the text was misleading in the statement
Quote:
Regardless whether the original ch is 8 bits, 16 bits, or more, the final value is trimmed to something that fits into a single byte.
That was why I tried the code with
Code:
char MASK = 0xff;
expecting that I would see the truncation suggested.
Thanks for your forum, as I always find that so many texts serve better as reference books for those who are already familiar with the subject.
You shouldn't mix binary operations and signed numbers. Unsigned is easier, and doesn't have platform specific behaviours.
Now masking the LS byte is goes like this:
You shouldn't mix binary operations and signed numbers. Unsigned is easier, and doesn't have platform specific behaviours.
Now masking the LS byte is goes like this:
Code:
b = (unsigned char)a;
Everything well explained, but what does "LS byte" stand for?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.