School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,121 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,960 people online right now. Registration is fast and FREE... Join Now!




Masking bit strings

 

Masking bit strings

Beki

2 Aug, 2009 - 01:00 PM
Post #1

New D.I.C Head
*

Joined: 16 Jun, 2009
Posts: 19


My Contributions
I don't really get this..

QUOTE

For example, if you have an eight-bit binary value ‘X’ and you want to guarantee that bits four through seven contain zeros, you could logically AND the value ‘X’ with the binary value 0000 1111. This bitwise logical AND operation would force the H.O. four bits to zero and pass the L.O. four bits of ‘X’ through unchanged.


I get the first part about '0000' AND 'XXXX' because if X is 1 or 0 X AND 0 will always equal 0. But what if the last 4 bits contain a 0, wouldn't 0 AND 1 = 0, which will change it o_o, meaning it will only work if the last 4 bits are all 1's. Sorry if I've explained this badly, but it's hard when you don't understand!

If someone could explain, I'd be very greatful!

User is offlineProfile CardPM
+Quote Post


KYA

RE: Masking Bit Strings

2 Aug, 2009 - 01:12 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Is that a typo? I originally read that as at least 4 bits are zero, but on second glance that's not what the author said.

To guarantee that bits 4-7 are zeros it would need to be the other way around:

1111 0000

Other wise X's bits 4 - 7 would need to be zero.

! AND ! = 1
! AND 0 = 0
0 AND 0 = 0


UNLESS, the author simply means that to check, you could AND it with 0000 1111 which is correct, meaning the result of the AND would tell you what the last 4 bits of X are. If the last 4 turn out to be 0 after the AND, that means that the last 4 of X are zero since ANDing them with 1111 resulted in 0000.
User is online!Profile CardPM
+Quote Post

AdaHacker

RE: Masking Bit Strings

2 Aug, 2009 - 01:19 PM
Post #3

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 395



Thanked: 86 times
My Contributions
QUOTE(Beki @ 2 Aug, 2009 - 03:00 PM) *
But what if the last 4 bits contain a 0, wouldn't 0 AND 1 = 0, which will change it

No, it doesn't change it. If that bit contains a 0, then 0 AND 1 = 0, which is the same as the original bit in X. And if the bit in X was 1, then 1 AND 1 = 1, which is still the same. That's the idea: anything ANDed with 1 equals the original value.
User is offlineProfile CardPM
+Quote Post

Beki

RE: Masking Bit Strings

2 Aug, 2009 - 01:21 PM
Post #4

New D.I.C Head
*

Joined: 16 Jun, 2009
Posts: 19


My Contributions
LOL I just got it, IDK what I was thinking to begin with, probably confused with something else :[ Thankyouu!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:54PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month