byte help

This is a discussion on byte help within the C++ Programming forums, part of the General Programming Boards category; I going to post my problem below but I really am just stuck on how to get to the byte ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    byte help

    I going to post my problem below but I really am just stuck on how to get to the byte in position bp. I know how to set the bits to 1, but just flustered on trying to get to the specified byte.

    Code:
     //Returns the unsigned int obtained when the byte in
    //position bp of x is set to all 1-bits 
    //The bytes are numbered 0 to 3 from right to left.
    unsigned int setByte(unsigned int x, int bp)
    {
    //body
    }
    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps bitshifts can help?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    I was thinking about a bitshift, but I need to return x unchanged except for the byte in which all the bits are set 1, so possible bitshift a copy of x and then and it to x, but I'm really having trouble on accessing the specific byte(indicated by bp), would a loop be neccessary?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    You need to set all the bits in a specific byte?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    I almost never use bitshifts, but this is what I am thinking

    int original = 0xf0f0f0f0;
    int mask = 0x0000000f;
    mask = mask << 3; // Assuming you want to change the third byte.
    int changed = original | mask;

    I have no idea if it will actually work.
    Last edited by 691175002; 01-29-2008 at 05:13 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    If you wanted to set the X byte, then something like this might do the trick:
    Code:
    unsigned int setByte(unsigned int x, int bp)
    {
    	int mask = 0xFF << (8 * bp);
    	x |= mask;
    	return x;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thanks alot Elysia and everyone else that got me there, I really appreciate the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21