Author Topic: fillin byte  (Read 897 times)

thrushitha

  • Full Member
  • ***
  • Posts: 26
fillin byte
« on: August 29, 2011, 01:20:38 am »
how to fill an byte partially?
i mean in a byte , it has 8 bits and i want to fill byte from location 5, hat means 1, 2, 3, 4 will not be filled and fillin starts frm 5 and goes on


kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Re: fillin byte
« Reply #1 on: September 07, 2011, 06:45:24 am »
how to fill an byte partially?
i mean in a byte , it has 8 bits and i want to fill byte from location 5, hat means 1, 2, 3, 4 will not be filled and fillin starts frm 5 and goes on

Here you go:

#include "stdafx.h"
#include <iostream>


char FillByte(char byte, int pos, int value)
{
   char pattern = (0x01 << pos);
   if(value == 0)
   {
      byte = (byte & (~(0x01 << pos)) );
   }
   else
   {
      byte = (byte & (~(0x01 << pos)) );
      byte |= (0x01 << pos);
   }
   return byte;
}

int _tmain(int argc, _TCHAR* argv[])
{
   char byte = 0x00;

   byte = FillByte(byte, 4, 1);
   printf("%x\n", byte);
   byte = FillByte(byte, 5, 1);
   printf("%x\n", byte);
   byte = FillByte(byte, 6, 1);
   printf("%x\n", byte);
   byte = FillByte(byte, 7, 1);
   printf("%x\n", byte);

   byte = FillByte(byte, 7, 0);
   printf("%x\n", byte);
   byte = FillByte(byte, 6, 0);
   printf("%x\n", byte);
   byte = FillByte(byte, 5, 0);
   printf("%x\n", byte);
   byte = FillByte(byte, 4, 0);
   printf("%x\n", byte);

   return 0;
}


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1