How to insert and shift bit position

Thread Starter

Dadu@

Joined Feb 4, 2022
155
I am trying write program in C language on computer for the following steps:

  1. Declare a variable that holds one byte of data and assign it to 0 .
  2. Data byte = 00000000 ( MSB - LSB)
  3. Give option to the user so that he can set or reset last bit in the byte.
  4. For example, suppose the user sets the last bit
  5. Data byte = 00000001
  6. Shift LSB to MSB position
  7. Data byte = 10000000

My code

C:
#include<stdio.h>
#include<stdint.h>

int main ()
{
    uint8_t Data_Byte = 0;
    uint8_t LSB_BIT;
  
    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );

    Data_Byte =  ( Data_Byte | LSB_BIT );
      
    Data_Byte = Data_Byte << 7;

    printf("%d ",  Data_Byte );
  
   return 0;
}
Output test for set / Reset

Set/Reset LSB Enter 1 /0 : 1
128

Set/Reset LSB Enter 1 /0 : 0
0

I belive code is doing what I wanted to do it

I want to check MSB bit after shifting the position. If the MSB bit is set to high then true statement should be print else false statement should be print

How to test MSB bit in the byte ?
 

dl324

Joined Mar 30, 2015
18,220
You can use bitwise AND to extract the MSB and check to see if it's non-zero. If you plan to do a lot of bit manipulations, it's easier to use bit fields, because it can eliminate the need to shift and will be more readable.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
You can use bitwise AND to extract the MSB and check to see if it's non-zero. If you plan to do a lot of bit manipulations, it's easier to use bit fields, because it can eliminate the need to shift and will be more readable.
Program doesn't works as I am expecting
C:
#include<stdio.h>
#include<stdint.h>

int main ()
{
    uint8_t Data_Byte = 0;
    uint8_t LSB_BIT;
    
    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );
  
    Data_Byte =  ( Data_Byte | LSB_BIT );
        
    Data_Byte = Data_Byte << 7;
  
    printf("%d \n",  Data_Byte );
    
    if (  Data_Byte & 128 == 128)
    {
        printf("TRUE \n");
    }
    else
    {
        printf("FALSE \n");
    }
    
   return 0;
}
Set/Reset LSB Enter 1 /0 : 1
128
FALSE

I was expecting it should be print True
 

MaxHeadRoom

Joined Jul 18, 2013
30,559
Look at the pic logic instructions.
ANDLW
ANDWF,
IORLW,
IORWF,
XORLW
XORWF
Individual bits can be set-reset by one word instruction..
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Data_Byte 0000 0000 & 0000 0001 (SET LSB_BIT) = 0000 0001
Data_Byte 0000 0000 & 0000 0000 (CLEAR LSB_BIT) = 0000 0000

I don't get expected output from code

C:
#include<stdio.h>
#include<stdint.h>

int main ()
{
    uint8_t Data_Byte = 0;
    uint8_t LSB_BIT;
    
    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );
 
    Data_Byte =  ( Data_Byte & LSB_BIT );
    printf("%d \n",  Data_Byte );   
      
   return 0;
}
Set/Reset LSB Enter 1 /0 : 1
0

I was expecting 1 when user set LSB bit but it gives 0
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
The correct result in both cases is 0.
I want to change the LSB bit of a byte either it can be high or it can be low

For example we have byte 0000 0000 and we set LSB bit it to high now byte become 0000 0001.

I think we need to follow this logic
Byte = 0000 0000 & 0000 0001 = 0000 0001

i have implemented the same logic in my code but i am not getting the output i want
 

dl324

Joined Mar 30, 2015
18,220
i have implemented the same logic in my code but i am not getting the output i want
Your logic is faulty. 1 & 0 is always going to be 0.

Code to set and clear bit 0:
Code:
Data_byte = 0;
LSB_BIT = 1;

Data_byte |= LSB_BIT;   // set bit
Data_byte &= ~LSB_BIT;  // clear bit
Since LSB_BIT is actually 8 bits, your code should either check that only bit 0 was set or mask off that bit:
Code:
Data_byte |= (0x1 & LSB_BIT);   // set bit
Data_byte &= ~(0x1 & LSB_BIT);  // clear bit
 

djsfantasi

Joined Apr 11, 2010
9,237
think we need to follow this logic
Byte = 0000 0000 & 0000 0001 = 0000 0001
You are doing a bit wise AND where one value is 0000 0000
The result will always be 0.
To set the bit, you want to do a bit wise OR.
Byte = 0000 0000 | 0000 0001 = 0000 0001
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
I want following operation now

Byte = 0000 0000 | 0000 0001 = 0000 0001 << 1 = 000 00010 = 2
Byte = 000 00010 | 0000 0001 = 0000 0011 << 1 = 000 00110 = 6

I was thinking that last print statement should print 6 in program but it print 2. Where is code going wrong ?

C:
#include<stdio.h>
#include<stdint.h>

int main ()
{
    uint8_t Data_Byte = 0;
    uint8_t LSB_BIT;
 
    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );

    Data_Byte =  ( Data_Byte | LSB_BIT );
    printf("%d \n",  Data_Byte );
 
    Data_Byte = Data_Byte << 1;
    printf("Byte = %d \n",  Data_Byte );
 
//    Byte = 0000 0000 | 0000 0001 = 0000 0001 << 1 = 000 00010 = 2
 
    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );

    Data_Byte =  ( Data_Byte | LSB_BIT );
    printf("%d \n",  Data_Byte );
 
    Data_Byte = Data_Byte << 1;
    printf("Byte = %d \n",  Data_Byte );
 
 
//    Byte = 000 00010 | 0000 0001 = 0000 0011 << 1 = 000 00110 = 6
   
   return 0;
}
Set/Reset LSB Enter 1 /0 : 1
1
Byte = 2
Set/Reset LSB Enter 1 /0 : 1
1
Byte = 2
 

dl324

Joined Mar 30, 2015
18,220
The problem is that scanf expects to do an integer conversion with %d; on my computer, an integer is 32 bits. You need to use %hhd instead:
1646589651164.png
 
Last edited:

Thread Starter

Dadu@

Joined Feb 4, 2022
155
The problem is that scanf expects to do an integer conversion with %d; on my computer, an integer is 32 bits. You need to use %hhd instead:
I am using uint8_t means 8 bit integer. I don't get correct output even after using %hhd

C:
#include<stdio.h>
#include<stdint.h>

int main ()
{
    uint8_t Data_Byte = 0;
    uint8_t LSB_BIT;
   
    printf("size of LSB_BIT :%d  byte \n", sizeof(LSB_BIT));

    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );

    Data_Byte =  ( Data_Byte | LSB_BIT );
    printf(" %hhd\n",  Data_Byte );

    Data_Byte = Data_Byte << 1;
    printf("Byte = %d \n",  Data_Byte );

//    Byte = 0000 0000 | 0000 0001 = 0000 0001 << 1 = 000 00010 = 2

    printf("Set/Reset LSB Enter 1 /0 : ");
    scanf("%d", &LSB_BIT );

    Data_Byte =  ( Data_Byte | LSB_BIT );
    printf(" %hhd\n",  Data_Byte );

    Data_Byte = Data_Byte << 1;
    printf("Byte =  %hhd \n",  Data_Byte );


//    Byte = 000 00010 | 0000 0001 = 0000 0011 << 1 = 000 00110 = 6
 
   return 0;
}
size of LSB_BIT :1 byte
Set/Reset LSB Enter 1 /0 : 1
1
Byte = 2
Set/Reset LSB Enter 1 /0 : 1
1
Byte = 2
 

michael8

Joined Jan 11, 2015
472
I tried and can't. And I can't find the website help either.. Anyway it's C source code. Output is: (and the website squashed
by text... Where is fixed fonts?)

-- and --> &
a b c
0 0 0
0 1 0
1 0 0
1 1 1
-- or --> |
a b c
0 0 0
0 1 1
1 0 1
1 1 1
-- xor --> ^
a b c
0 0 0
0 1 1
1 0 1
1 1 0

At least that is readable, Squashed C code isn't.
 

dl324

Joined Mar 30, 2015
18,220
(and the website squashed
by text... Where is fixed fonts?)
Here's the code from the file. I don't see how it helps the OP.
Code:
#include<stdio.h>
#include<stdint.h>

int doone(int (*fun)(int a, int b) ){

    int a, b, c;

    printf("a\tb\tc\n");
    for (a = 0; a < 2; a++) {
    for (b = 0; b < 2; b++) {
        c = fun(a, b);
        printf("%d\t%d\t%d\n", a, b, c);
    }
    }
}

int fun_and(int a, int b) {

    return a & b;
}

int fun_or(int a, int b) {
    return a | b;
}

int fun_xor(int a, int b) {
    return a ^ b;
}

int main(int argc, char **argv) {

    printf("-- and --> &\n");
    doone(fun_and);

    printf("-- or --> |\n");
    doone(fun_or);

    printf("-- xor --> ^\n");
    doone(fun_xor);

    return 0;
}
 
Top