473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 bitwise questions

kirtikjr
2 New Member
1. Can I access each bit of value stored in a byte?
Supposing an integer value 65 (int i =65;) is stored in 2 bytes of memory, I can easily find out its memory location by using pointers. We know that within 2 bytes(for 16-bit environment) it will be stored as 1000001, but can I find out this data in each bit of these two bytes?

2. How does the bitwise complement (~) operators work with signed int or char? I can understand with unsigned values. Plz explain how it works with signed int or char.
Dec 4 '08 #1
2 1885
YarrOfDoom
1,247 Recognized Expert Top Contributor
1. You could use the bitwise and operator to find out if a certain bit is 1. Example:
Expand|Select|Wrap|Line Numbers
  1. int test = 41;
  2. int *ptr = &test;
  3. if (*ptr & 8) { // 8=00001000
  4.     cout<< "The 4th bit (counted from the right) is 1.\n";
  5. } else {
  6.     cout<< "The 4th (counted from the right) is 0.\n";
  7. }
Dec 4 '08 #2
newb16
687 Contributor
2- ~ doesn't care about how the bit pattern is treated as a number - it acts on a bit field. If signed numbers are represented as twos complement then ~x == -(x+1) for all x except the smallest negative ( -128 for signed char ).
Dec 4 '08 #3

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

Similar topics

6
5901
by: Bostonasian | last post by:
I am new to bitwise thing in MSSQL. Let's suppose there's a table of favorite foods insert int fav_foods(food_name,bitwiseVal) values('Pasta',1) insert int fav_foods(food_name,bitwiseVal) values('Chicken',2)
2
3466
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
9
7029
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
37
11065
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there anyone that could tell me how to convert these two things? 1)
21
1943
by: Henry Padilla | last post by:
I'm writing a map generator and I'm looking for a way to keep track of the various terrain. I had a thought of using bitboards but I can't find a way to bitwise AND two int's together and get a bool. How do I get the proper response from: IF (0x01 & 0x05) My only recourse so far is to use the full if syntax: IF ((0x01 & 0x05) > 0)
5
5795
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
17
3345
by: mdh | last post by:
If one searches the forum for bitwise operators there are numerous questions and very good answers to the perenial question about what these operators do and why we need them. So, having looked at all these, I approached K&R 2.9 headily!! ...but not for long :-( Quote: The bitwise AND operator & is often used to mask off some set of bits; for example, n=n & 0177;
5
3309
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive OR) ~x Bitwise negation
4
1869
by: John Dann | last post by:
I'm trying to generate a 4-byte unsigned integer in VB2005 that encodes a number of bit-level variables. What I need to do is to be able to compute a value for the uint that is based on specific values for the constituent variables; the unit can then be passed to an external DLL, which requires a function parameter in this form. Unfortunately the only documentation I have for the external DLL is in (I think) C++, which I'm struggling to...
4
4184
by: bretonlais | last post by:
I am creating a scheduling application which will allow users to schedule recurring tasks, e.g., the user can set a task to run (M,T,W) or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and use a bitwise OR and AND to determine which days the user selected (and all the permutations): const None = 0x0; const Sunday = 0x1; const Monday = 0x2; const Tuesday = 0x4;
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.