1 / 14

Bit Manipulation

Bit Manipulation. Get Bit from the particular Position. Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int position) function.

john-mckee
Download Presentation

Bit Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bit Manipulation

  2. Get Bit from the particular Position • Suppose you want to get a bit from the 4th position. • You can call • int get_bit_val(int number_send,int position) function. • In this function, number_send is our data from where we want to get a bit. Second pass the position from where you want to get a bit.

  3. Get Bit function int get_bit_val(int number_send,int position) { int number1=1; number1=number1<<(position-1); if(number_send & number1) { return (1); } else { return (0); } }

  4. How Get bit function works? • First argument we can pass as int. so ascii value is passed there as a number_send. • Suppose user has entered a character N that you pass as a argument to get bit function. • Ascii of N is 78 that is 1001110. • Second argument you pass for the 4th position bit.

  5. 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 1 0 0 0 Contd. • In the first step number1 =1 is initialized. • Number_send Number1 Number1<<(position-1) : Number1<<3 • Number_send Number1

  6. 0 1 0 0 1 1 1 0 0 0 0 0 1 0 0 0 Contd. • if((number_send & number1)) return (1); else return (0); • Number_send Number1 It will generate answer 0 or 1 as per the number_send value. In this case, it will generate 1. &

  7. Set Bit function • Through this function, we can set the bit with our own value on the particular position. • The function • void setbit(int *data, int pos, int val) takes 3 argument one is data, second is position and third is the value which we want to set on the particular position in the data.

  8. Set Bit Function • void setbit(int *data, int pos, int val) • { • int mask=1; • mask = mask<<(pos-1); • if(val==0) • { • mask=~mask; • *data=*data & mask; • } • else • { • *data=*data | mask; • } • }

  9. How set bit function works? • First one variable mask is initialized with one then after it is shifted on the particular position. • Then if value which is passed as third argument, if it is 0 then mask is complemented. So, if we perform data & mask then it sets 0 on that position. • If value which we want to set is 1 then data is ored with 1 so it sets 1 on that position.

  10. 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 1 0 0 0 Example • In the set bit function suppose arguments are for data N, position is 4 and new value what we want to set is 0. Initially it is • Data • Mask • After performing left shift for mask • Data • Mask

  11. 0 0 0 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 1 1 1 Example • if(val==0) { mask=~mask; *data=*data & mask; } In our case value is 0, means we want to set 0 on 4th position. mask ~mask Then last operation is data & mask & =

  12. Insert Bit • int ins_bit_pos(int number_send,int position,int value) { • int num1,num2; • num1=number_send; • num1=num1>>(position-1); • num1=num1<<(position-1); • num2=number_send-num1; • num1=num1<<1; • num1=num1 | num2; • if(value==1) • { • num1=set_bit_val(num1,position,value); • } • return (num1); • }

  13. 0 1 0 0 1 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 • We want to add bit 0 on the 3rd position. • Suppose number_send is N. which is assigned to num1 in the first step. So, • Num1 • Num1>> (position-1) • Num1<<(position-1) • Num2 = number_send – num1 - =

  14. 0 1 0 0 1 1 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 1 0 • num1= num1<<1 • num1= • Num1<<1 • num1 = num1|num2 • Bit 0 is set on the 3rd Position. | =

More Related