0

I created NSData of length 2 bytes (16 bits) and I want to set first 12 bits as binary value of (int)120 and 13th bit as 0 or 1(bit), 14th bit as 0 or 1 (bit) and 15th bit as 0 or 1(bit).

    This is what I want to do:
             0000 0111 1000 --> 12 bits as 120 (int)
             1 <-- 13th bit 
             0 <-- 14th bit
             1 <-- 15th bit
             1 <-- 16th bit

Expected output => 0000 0111 1000 1011 : final binary and convert it to NSData.

How can I do that? Please give me some advice. Thanks all.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
user3125692
  • 85
  • 3
  • 11

3 Answers3

2

0000011110001011 in bit is 0x07 0x8b in byte.

unsigned char a[2] ;
a[0] = 0x07 ;
a[1] = 0x8b ;
NSData * d = [NSData dataWithBytes:a length:2] ;
KudoCC
  • 6,912
  • 1
  • 24
  • 53
2

Recently i wrote this code for my own project.. Check if this can be helpful to you.

//Create a NSMutableData with particular num of data bytes
NSMutableData *dataBytes= [[NSMutableData alloc] initWithLength:numberOfDataBytes];

//get the byte in which you want to change bit.
char x;
[dataBytes getBytes:&x range:NSMakeRange(bytePos,1)];
//change the bit here by shift operation
x |= 1<< (bitNum%8);
//put byte back in NSMutableData
[dataBytes replaceBytesInRange:NSMakeRange(bytePos,1) withBytes:&x length:1];

Let me know if you need more help ..:)

For changing 13th bit based on string equality

if([myString isEqualTo:@"hello"])
{
 char x;
    [dataBytes getBytes:&x range:NSMakeRange(0,1)];
    //change the bit here by shift operation
    x |= 1<< (4%8); // x |=1<<4;
    //put byte back in NSMutableData
    [dataBytes replaceBytesInRange:NSMakeRange(0,1) withBytes:&x length:1];
}
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • Thanks. Could you please show me how to change the bit when check if NSString is equal a value, then change 13th bit to 1 or 0?Example: if NSString = "hello", 13th bit change to 1. how can do that? thanks much – user3125692 Dec 25 '13 at 15:46
  • i've Added required code in my answer.. Don't forget to accept the answer ..:) – Prince Agrawal Dec 25 '13 at 16:04
  • Could you explain why it is x |= 1<< (4%8); ?? thanks – user3125692 Dec 25 '13 at 16:17
  • x is the MSB(Most Significant byte) of your 2 byte NSData. 13th bit is 5th bit in 2nd byte. you have to change it with one. so you are performing X-OR operation between x & 0x00010000. 4%8 gives 4 & 1<<4 is 0x00010000. Let me know if you need more clarity – Prince Agrawal Dec 25 '13 at 16:22
  • One more thing, if I want to set 12 bits first is number (int type). Example: number = 150, it will be 000010010110 (12 bits first). How can i do that? Thanks so much – user3125692 Dec 25 '13 at 17:11
  • Also, I want to change 14th, 15th and 16th bit too. I tried below code to change 14 but it not correct. 14th bit is 3%8, is it right? char x50; [dataBytes getBytes:&x50 range:NSMakeRange(0,1)]; //change the bit here by shift operation x50 |= 1<< (3%8); // x |=1<<4; //put byte back in NSMutableData [dataBytes replaceBytesInRange:NSMakeRange(0,1) withBytes:&x50 length:1]; – user3125692 Dec 25 '13 at 17:29
  • 14th is 5%8... 6th bit – Prince Agrawal Dec 25 '13 at 17:46
  • for 150, int intValue=150; [dataBytes replaceBytesInRange:NSMakeRange(1,1) withBytes:&intValue length:1]; – Prince Agrawal Dec 25 '13 at 17:55
  • I tried your code for 150,after run ,it shows databytes =<0096>, in binary is 10010110, it only has 8bits. I need fill enough 12 bits. How can do that? thanks – user3125692 Dec 26 '13 at 02:52
1

This is the exact code you might need:

uint16_t x= 120; //2 byte unsigned int (0000 0000 0111 1000)
//You need 13th bit to be 1
x<<=1; //Shift bits to left .(x= 0000 0000 1111 0000)
x|=1; //OR with 1 (x= 0000 0000 1111 0001)
//14th bit to be 0.
x<<=1; // (x=0000 0001 1110 0010)
//15th bit to be 1
x<<=1; //x= 0000 0011 1100 0100
x|=1;  //x= 0000 0011 1100 0101
//16th bit to be 1
x<<=1; //x= 0000 0111 1000 1010
x|=1;  //x= 0000 0111 1000 1011

//Now convert x into NSData
/** **** Replace this for Big Endian ***************/
NSMutableData *data = [[NSMutableData alloc] init];
int MSB = x/256;
int LSB = x%256;
[data appendBytes:&MSB length:1];
[data appendBytes:&LSB length:1];

/** **** Replace upto here.. :) ***************/
//replace with : 
//NSMutableData *data = [[NSMutableData alloc] initWithBytes:&x length:sizeof(x)];
NSLog(@"%@",[data description]);

Output: <078b> //x= 0000 0111 1000 1011 //for Big Endian : <8b07> x= 1000 1011 0000 0111

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • your above code is create data 2 byte, it not correct. I want to create a data 2 byte, this data including : 12 bits first has number (ex:120), 4 bits last just doing same as my question. Please help me. thanks – user3125692 Dec 26 '13 at 02:47
  • According to my knowledge, there is no concept of 12 bit. it can be either 8 or 16 bit. So What i have done is, i have taken a 2 byte unsigned integer & kept its first 8 bits 0 & last 8 bits as binary value of 120. After that as you need to change last 4 bit of the number, shift bits of uint & perform OR operation. i've updated my answer with proper comment. Please see it. – Prince Agrawal Dec 26 '13 at 03:46
  • Thanks so much, if I want to use Big Endian, how do that? – user3125692 Dec 26 '13 at 04:31
  • I tried your updated code but when run, it return <78000078>. it seems not correct? NSMutableData *data = [[NSMutableData alloc] initWithBytes:&x length:sizeof(x)]; int MSB = x/256; int LSB = x%256; [data appendBytes:&MSB length:1]; [data appendBytes:&LSB length:1]; – user3125692 Dec 26 '13 at 06:06
  • hey.. you didn't replaced completely… check the answer again – Prince Agrawal Dec 26 '13 at 06:08
  • Could you please help 1 more time: I need create NSdata 1 byte, each bit in this data, I'll check condition to change bit to 1. Ex: 0000 0000 => if a =2, set 3th bit to 1. Thanks – user3125692 Dec 30 '13 at 11:01
  • No need of NSData in that. See answer given by KudoCC. you can use that. Let me know if that does not help – Prince Agrawal Dec 30 '13 at 11:08
  • Hi achievelimitless , can you show me how to read 12 bit first, then read 10 bit next ... in NSData?After reading 12 bit first, I want to convert 12bits to integer. Do same for 10 bit next. How can do that? Thanks – user3125692 Jan 01 '14 at 06:23
  • can you please be somewhat more clear? why don't you post complete requirement so that i can help you out – Prince Agrawal Jan 01 '14 at 09:34