Thread: Masking

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    21

    Masking

    I am writing a function for a program that encrypts and decrypts a file. For encryption, I have to read a character from a file and check if bit 15 is a 1. If it is, its printed to one file. If its anything else, its printed to another file. Then I read in the next character and check if bit 14 is a 1 and print it in the appropriate file. I keep doing this until EOF. My question is how exactly do I mask the character to isolate bit 15 and check if it is a 1. I really have no clue on how to actually mask a character from the file. Any help is appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    correction: I actually have to mask the passphrase the user has inputted not the actual character. however, I would still appreciate help on how to mask. For example, I have to turn off all bits in the passphrase except bit 15 (counting from 0). If you could help me understand how to mask that, I would greatly appreciate it.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Querying bit 15 can be done with & 0x8000:

    Code:
    word & 0x8000;
    http://en.wikipedia.org/wiki/Mask_(computing)

    Is this wide characters you have there?

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    The user just inputs a passphrase, for example foobar and I have begin with masking it. First I have to isolate the 15th bit and check if its 1. Then I isolate the 14th bit and check if that is 1. I continue doing this until there are no characters left in the file itself. I figured out how to mask (thanks to you and wikipedia). However, I now have to create a loop that starts masking with the 15th bit and keeps going until EOF. I was thinking I would have to use a for loop for this part but I don't know how I would implement a for loop when it comes to masking. Actually, the masking I did doesn't isolate the 15th bit. This is what I have so far:
    Code:
    #include "crypto.h"
    #include <stdlib.h>
    #include <stdio.h>
    
    int generateKey()
    {
    	char c;
    	int h = 0;
    	int a = 31415;
    	int b = 27183;
    	int power = 2;
    	int i;
    
    	/* calculates the 2 to the 16th and stores it to power*/
    
    	for(i=1; i<16; i++) 
    	{
    		power = power*2;
    	}
    	
    
    	/* while there is no linefeed, does the algorithm and  
    	 * waits for more input */
    	
    	while(c != '\n')
    	{
    		scanf("%c", &c);
    		h = (a*h + (int)c) % power;
    		a = (a*b) % (power-1);
    
    	}
    	return h;
    	
    }
    
    int encrypt(int key)
    {
    	FILE *fp;
    	FILE *fp2;
    	FILE *fp3;
    	int MASK;
    	int retval;
    	char word, tmp;
    
    	if((fp = fopen("input.txt", "r")) == NULL) return -1;
    
    	else
    	{
    		fp2 = fopen("part1.txt", "w");
    		fp3 = fopen("part2.txt", "w");
    
    		
    		
    
    		retval = fscanf(fp, "%c", &word);
    		MASK = generateKey() & 0xffff;
    		while(retval != EOF)
    		{
    			for(i
    Last edited by fiendslyr; 06-24-2010 at 06:48 PM. Reason: adding code

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    a general tutorial/guide on bitwise operations would explain how masking works. you dont absolutely need to know binary, but i think it'd help

    The GNU C Programming Tutorial

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If all you got is 16 bits to check you could do something like this:

    Code:
    int mask = 0x8000;
    
    while(mask){
            if(word & mask)
                    //do something
    
            mask >>= 1;
    }
    Since the expression will evaluate to 1 or 0, it's essentially a true/false check. For each iteration in the loop we make mask smaller by shifting one step until it's zero and your done.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Bitwise operations are also covered in at least one FAQ here, in the FAQ section, as well as in any introductory C book you might happen to have.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Thank you so much Subsonics, that really helped me understand. I wasn't even thinking about right shift. It completely slipped my mind. Thanks to everyone else too. I will probably be back in a little bit when I run into another problem.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you are planning to use right shift - better stick to the unsigned type for the mask
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why would that matter? The mask is a positive integer taking up only two bytes of four.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Subsonics View Post
    Why would that matter? The mask is a positive integer taking up only two bytes of four.
    right shifting of signed integers is compiler specific...

    it means - standard says nothing about how it will work.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by vart View Post
    right shifting of signed integers is compiler specific...

    it means - standard says nothing about how it will work.
    Ok I see, GCC here. You would imagine that it would matter if the signed bit was set of course, but.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Masking input
    By bradszy in forum C++ Programming
    Replies: 15
    Last Post: 02-23-2008, 10:39 AM
  2. understand masking bits
    By musikluvah in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2006, 11:31 PM
  3. drive masking
    By bennyandthejets in forum Windows Programming
    Replies: 5
    Last Post: 09-19-2003, 01:48 PM
  4. DIB masking
    By SAMSAM in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2003, 10:22 AM
  5. Masking a surface
    By malloc(BOB) in forum Windows Programming
    Replies: 0
    Last Post: 09-05-2002, 12:49 PM