46

I know you can get the first byte by using

int x = number & ((1<<8)-1);

or

int x = number & 0xFF;

But I don't know how to get the nth byte of an integer. For example, 1234 is 00000000 00000000 00000100 11010010 as 32bit integer How can I get all of those bytes? first one would be 210, second would be 4 and the last two would be 0.

Rob
  • 5,223
  • 5
  • 41
  • 62
Kamran224
  • 1,584
  • 7
  • 20
  • 33
  • 1
    You're already using the bit shift operator `<<` in your example. How could you use the shift operator to get different bits out of your number? – Greg Hewgill Oct 16 '11 at 21:24
  • Try the other bit-shift operator. – Marcelo Cantos Oct 16 '11 at 21:26
  • 4
    Bear in mind that the "first byte" — as you've used it here — may not be the first byte in memory. Your example, 1234, may very easily be `11010010` at the lowest address, and, `00000000` at the highest address. – Thanatos Oct 16 '11 at 22:01

5 Answers5

88
int x = (number >> (8*n)) & 0xff;

where n is 0 for the first byte, 1 for the second byte, etc.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • 18
    Please use parentheses! I can never member whether `>>` or `*` has higher precedence. – Greg Hewgill Oct 16 '11 at 21:25
  • 1
    Why doesn't this cause weirdness because of an arithmetic right shift? I know that it doesn't, I just don't understand why. – Brian Peterson Sep 11 '19 at 01:48
  • @BrianPeterson The number gets shifted to the right so that the byte you want is in the rightmost position, and then it gets masked with the & 0xff, which outputs a one in any spot where both numbers have ones. For example n = 0000 0000 1011 0101 0xff = 0000 0000 0000 1111 n & 0xff = 0000 0000 0000 0101 – FlexEast Jun 26 '22 at 14:42
  • in response to @GregHewgill : it makes no sense to use parentheses when NOT needed. it brings confusion because a reviewer will think : oh, i need this parenthese, why ? – sandwood Aug 03 '23 at 12:38
20

For the (n+1)th byte in whatever order they appear in memory (which is also least- to most- significant on little-endian machines like x86):

int x = ((unsigned char *)(&number))[n];

For the (n+1)th byte from least to most significant on big-endian machines:

int x = ((unsigned char *)(&number))[sizeof(int) - 1 - n];

For the (n+1)th byte from least to most significant (any endian):

int x = ((unsigned int)number >> (n << 3)) & 0xff;

Of course, these all assume that n < sizeof(int), and that number is an int.

Dmitri
  • 9,175
  • 2
  • 27
  • 34
  • If we have an array of int, How will the answer change? I mean how can I get the nth byte's value from the beginning of an int array ? – HoseinGhanbari Dec 24 '17 at 04:49
  • @HoseinGhanbari Same way, but find the `(n % sizeof(int))`-th byte of the `(n / sizeof(int))`-th array element. – Dmitri Dec 24 '17 at 06:36
4

int nth = (number >> (n * 8)) & 0xFF;

Carry it into the lowest byte and take it in the "familiar" manner.

akappa
  • 10,220
  • 3
  • 39
  • 56
1

If you are wanting a byte, wouldn't the better solution be:

byte x = (byte)(number >> (8 * n));

This way, you are returning and dealing with a byte instead of an int, so we are using less memory, and we don't have to do the binary and operation & 0xff just to mask the result down to a byte. I also saw that the person asking the question used an int in their example, but that doesn't make it right.

I know this question was asked a long time ago, but I just ran into this problem, and I think that this is a better solution regardless.

David Fletcher
  • 305
  • 1
  • 7
  • 2
    Standard C does not have a type named `byte`, could you be referring to either a `uint8_t` from C99 or `unsigned char`? – txk2048 Jul 14 '20 at 17:55
0
//was trying to do inplace, would have been better if I had swapped higher and lower bytes somehow

uint32_t reverseBytes(uint32_t value) {
uint32_t temp;
size_t size=sizeof(uint32_t);

for(int i=0; i<size/2; i++){

//get byte i
temp = (value >> (8*i)) & 0xff;

//put higher in lower byte
value = ((value & (~(0xff << (8*i)))) | (value & ((0xff << (8*(size-i-1)))))>>(8*(size-2*i-1))) ;

//move lower byte which was stored in temp to higher byte
value=((value & (~(0xff << (8*(size-i-1)))))|(temp << (8*(size-i-1))));              
}
return value;
}
Fitohe
  • 13
  • 1
  • 3