LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-08-2015, 08:01 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
Bitwise & operator: what am I missing?


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:
Quote:
val is now 521027
What am I missing (or misunderstanding)?
Thanks.
 
Old 03-08-2015, 08:14 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,155

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
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.
 
1 members found this post helpful.
Old 03-08-2015, 08:21 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You want the mask to be 'unsigned char'.

signed char
Code:
bash-4.2$ ./tt
MASK 4294967295, a 521027
b is now 521027
unsigned char
Code:
bash-4.2$ ./tt
MASK 255, a 521027
b is now 67
It has to do with the way the signed integers are represented.
 
1 members found this post helpful.
Old 03-08-2015, 09:43 PM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-09-2015, 12:44 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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;
 
1 members found this post helpful.
Old 03-09-2015, 07:00 AM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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?
 
Old 03-09-2015, 07:14 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
(least significant, as in Least significant bit)
 
1 members found this post helpful.
Old 03-09-2015, 07:34 AM   #8
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
(least significant, as in Least significant bit)
Oh! In my readings, the sign-determinant bit of a signed int is shown (at least pictorially) as the N-1 bit. Most confusing.
 
Old 03-09-2015, 07:44 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
(Ordering bits of a byte (byte of a word) isn't entirely unambigous either. For bits, the cleanest notations might be this: 2^0,2^1,...2^7)

Last edited by NevemTeve; 03-09-2015 at 07:45 AM.
 
1 members found this post helpful.
  


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
how to add && operator in while loop? shams Linux - General 7 08-13-2014 12:11 PM
[SOLVED] Problems with NOT ~ bitwise operator - C++ Skyer Programming 10 09-23-2011 05:02 PM
[SOLVED] The & operator on functions Lobinho Programming 6 07-19-2010 07:32 PM
Inconsistent bitwise operator behaviour in C oneandoneis2 Programming 4 02-08-2007 03:20 AM
Bash scripting and && operator Dark_Helmet Programming 16 09-18-2005 09:57 PM

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

All times are GMT -5. The time now is 05:26 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