473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to SetBits 0-4 in a byte using C language

Hi all,
i am new to this forum, but i need a help, i need to increment BIT values of 0-4 without disturbing other bits in a byte, Because others bits 5,6,7 is already set, so i just need to increment the remaining bits whenever and entry is found. for ex when i find a entry, it must increment the bit values from 0-4 without disturbing the rest of the bits. can anybody help me out
Jan 17 '07 #1
6 2212
Hi all,
i am new to this forum, but i need a help, i need to increment BIT values of 0-4 without disturbing other bits in a byte, Because others bits 5,6,7 is already set, so i just need to increment the remaining bits whenever and entry is found. for ex when i find a entry, it must increment the bit values from 0-4 without disturbing the rest of the bits. can anybody help me out

Try by using bitwise OR
Example :
X | 1 will set the 1bit and existing bits will be same only change is 1st bit will set
X | 2 will set second bit
X | 3 will set 1st and 2nd bit ......



Thanks and Regards
Rajesh G
rajeshg@infotechsw.com
Jan 17 '07 #2
Soujiro
35
It depends whether big-indian or small indian.. in my case
for example:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             0010 0000 = (32 in dec ) = (space in ascii) 
  3.  
  4.  
you want to leave the first 4 bit as is.. and want to edit the 4 last bit into something like 0010.. this should become 0010 0010.. therefore because

Expand|Select|Wrap|Line Numbers
  1.  
  2.             0010 0010 = (34 in dec ) = "(quote) in ascii 
  3.  
  4.  
then thats it.. you've changed the last 4 bit without changing the first 4..



NOTE: This is what i've done somehow when i was in high school and now im kinda not into that cause im using high level language.. im just here to reminisce C.. Also be careful with unsigned char and char cause they really differ unsigned char can carry up to 255 but char is only up to 127..
Wait for the powerful ones in the forum they can answer you in a more easy practical way just like the answer above.. :)
Jan 17 '07 #3
hi,
sorry for my delay reply, thanks for your reply, but i need to increment the bits 0-4 without disturbing other 3 bits in a byte whenever i find an entry in a list
Jan 18 '07 #4
Try by using bitwise OR
Example :
X | 1 will set the 1bit and existing bits will be same only change is 1st bit will set
X | 2 will set second bit
X | 3 will set 1st and 2nd bit ......



Thanks and Regards
Rajesh G
rajeshg@infotechsw.com
Thanks Rajesh, i need to increment the bits whenever i find an entry in a list, the setting of bits is fine, how to increment???
Jan 18 '07 #5
horace1
1,510 Expert 1GB
Thanks Rajesh, i need to increment the bits whenever i find an entry in a list, the setting of bits is fine, how to increment???
the following should increment bits 0-3 of n without effecting bits 4-7
Expand|Select|Wrap|Line Numbers
  1.        int temp=(n&0xf);  // get bits 0-3   
  2.        // increment bits 0 to 3 and add in bits 4-7
  3.        n=(n & 0xf0) + ((++temp) & 0xf);
  4.  
Jan 18 '07 #6
the following should increment bits 0-3 of n without effecting bits 4-7
Expand|Select|Wrap|Line Numbers
  1.        int temp=(n&0xf);  // get bits 0-3   
  2.        // increment bits 0 to 3 and add in bits 4-7
  3.        n=(n & 0xf0) + ((++temp) & 0xf);
  4.  
hi thank you for your suggestion, its working fine, i just modified
thank you all for your help
Jan 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
16
by: Samuel Thomas | last post by:
Hello Friends, I understand(could be wrong) that the smallest chunk of memory is called a word. If that is correct, that means if I am using a 32 bit OS a word is 4 bytes. So that's why the size...
9
by: rahul | last post by:
Hi!, I have a java program that receives data via sockets from a C program. The java program has to recieve the data in byte format. I need to send character arrays from the C side after...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
45
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.