Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3973952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:30:39+00:00 2026-05-20T04:30:39+00:00

I’ve got the code bellow which it’s supposed to move a bit’s position inside

  • 0

I’ve got the code bellow which it’s supposed to move a bit’s position inside a byte. It works, but my question is about something else?

What am i doing wrong and what am i doing right regarding the allocation of example_byte and new_byte? Is it too much hassle for this simple program? Should have i just not used malloc and let the compiler do the dirty job better?

Here some guy’s opinion about this in the comments section : link.

#include <stdio.h>
#include <malloc.h>

typedef unsigned __int8 byte;

byte move(byte* our, int indexold, int indexnew)
{
byte oldvalue;
byte newvalue;
byte valuetochange;

valuetochange = 0x01 & ((*our)>>indexold);         // get the value of the bit to be moved
printf("value to change : %d\n", valuetochange);
oldvalue = (*our) & (~(1<<(indexold)));            // del the bit from position indexold
oldvalue = oldvalue & (~(1<<(indexnew)));          // del the bit from position indexnew
printf("deleted: %x\n", oldvalue);

newvalue = oldvalue | (valuetochange<<(indexnew)); // write bit in new position (indexnew)

return newvalue;
}

int main()
{
byte* example_byte;
byte* new_byte;

example_byte = (byte*)malloc(sizeof(byte));
new_byte     = (byte*)malloc(sizeof(byte));

*example_byte = 0xc3;  //  hex 0xc3 = binary 1100 0011
printf("\n");

//*****************************************************
// example 1 (move bit from position 1 to position 5)
// example_byte  1100 0011
//                 ^    ^
//               memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1
//               1100 0011 & 1111 1101 = 1100 0001 delete bit from oldindex (1)
//               1100 0001 & 1101 1111 = 1100 0001 delete bit from newindex (5)
// new_byte      1100 0001 | 0010 0000 = 1110 0001
*new_byte     = move(example_byte, 1, 5);

printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte);     // 0xe1 (1110 0001)
printf("\n");

//*****************************************************
// example 2 (move bit from position 6 to position 3)
// example_byte  1100 0011
//                ^   ^
//               memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1
//               1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6)
//               1000 0011 & 1111 0111 = 1000 0011 delete bit ftom newindex (3)
// new_byte      1000 0011 | 0000 1000 = 1000 1011
*new_byte     = move(example_byte, 6, 3);

printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte);     // 0x8b (1000 1011)
printf("\n");

//*****************************************************
// example 3 (move bit from position 2 to position 6)
// example_byte  1100 0011
//                ^    ^
//               memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0
//               1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2)
//               1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6)
// new_byte      1000 0011 | 0000 0000 = 1000 0011
*new_byte     = move(example_byte, 2, 6);

printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte);     // 0x83 (1000 0011)
printf("\n");

//*****************************************************
// example 4 (move bit from position 2 to position 4)
// example_byte  1100 0011
//                  ^  ^
//               memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0
//               1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2)
//               1100 0011 & 1110 1111 = 1100 0011 delete bit from oldindex (4)
// new_byte      1100 0011 | 0000 0000 = 1100 0011
*new_byte     = move(example_byte, 2, 4);

printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x  ", *new_byte);     // 0xc3 (1100 0011)
printf("\n");

free(new_byte);
free(example_byte);
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:30:40+00:00Added an answer on May 20, 2026 at 4:30 am

    If it is a single byte, there is no reason to do memory allocation at all. You can just declare a local variable of type byte and pass it around.

    You need to use malloc() when you need to allocate an array of elements, and you do not know the number of elements at compile time. You also need it when you need to allocate a large structure that needs to persist in memory after the function that created it exits. If you are dealing with a variable of a primitive type, then you should simply declare it as a local variable, which means that it will be allocated automatically on the stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a JSP page retrieving data and when single or double quotes are
I have just tried to save a simple *.rtf file with some websites and
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.