Jim Rogers wrote: >>C:\c>temp >>character: ⁿ >>character: = >>character: > >>character: >> >>252 61 62 0 >> >>00111111 >>10111100 >>01111100 >>00000000 >> >>C:\c> >> > > All this can be done in Ada. It would be great if we saw the code and its output. > How good is C++ at exactly representing bit fields? > > Can C++ do this portably: Some explanations with remarks in the code along with the output, would make things easier to understand. > type Byte is mod 2**8; > type Sign_Flag is mod 2; > > type Mouse_Status_Type is record > Left_Button_Pressed : Boolean; > Right_Button_Pressed : Boolean; > X_Movement_Sign : Sign_Flag; > Y_Movement_Sign : Sign_Flag; > X_Movement_Overflow : Boolean; > Y_Movement_Overflow : Boolean; > X_Movement_Magnitude : Byte; > Y_Movement_Magnitude : Byte; > end record; > > for Mouse_Status_Type use record > Left_Button_Pressed at 0 range 0..0; > Right_button_Pressed at 0 range 1..1; > X_Movement_Sign at 0 range 4..4; > Y_Movement_Sign at 0 range 5..5; > X_Movement_Overflow at 0 range 6..6; > Y_Movement_Overflow at 0 range 7..7; > X_Movement_Magnitude at 1 range 0..7; > Y_Movement_Magnitude at 2 range 0..7; > end record; > > The type Mouse_Status_Type defines a data > structure that occupies three bytes. I guess the C++ equivalent is: struct SomeStruct { unsigned Left_Button_Pressed: 1; unsigned Right_button_Pressed: 1; unsigned X_Movement_Sign: 1; unsigned Y_Movement_Sign: 1; unsigned X_Movement_Overflow: 1; unsigned Y_Movement_Overflow: 1; unsigned X_Movement_Magnitude: 8; unsigned Y_Movement_Magnitude: 8; }; It is interesting to see that ADA supports this. > The > first 6 fields of the record occupy a single > bit each, with bits 2 and 3 unused. > > Furthermore, access to the individual fields of this record > are very direct, not requiring arcane bit masks. > > foo : Mouse_Status_Type; > > > if foo.Left_Button_Pressed then > do_something; > end if; The above in a simple C++ program: #include int main() { struct SomeStruct { unsigned Left_Button_Pressed: 1; unsigned Right_button_Pressed: 1; unsigned X_Movement_Sign: 1; unsigned Y_Movement_Sign: 1; unsigned X_Movement_Overflow: 1; unsigned Y_Movement_Overflow: 1; unsigned X_Movement_Magnitude: 8; unsigned Y_Movement_Magnitude: 8; }; SomeStruct obj= {1, 0, true, false, 1, 16, 64}; if(obj.Left_Button_Pressed) std::cout<<"It is pressed!\n"; } C:\c>temp It is pressed! C:\c> > On the contrary, Ada is frequently used for low level bit manipulation. > Ada was designed to be used in safety-critical hard real-time > embedded systems. It is interesting to hear that. > We are all free to have our own interests. I do ask that you look at > the capabilities of a language before you decide they are not in your > interest. Ada does not prevent you from doing unsafe things. It > simply does not provide an unsafe behavior as its default. Perhaps I will check it sometime in the future. > > >>Someone who places much hopes on the language to protect him from his >>mistakes, probably ADA is better than C++ on this. > > > Programming is a human activity. All humans make mistakes. Some of those > mistakes are damned silly. Ada is designed to acknowledge that > programmers are human. Mistakes will be made. Ada attempts to identify > those mistakes as early in the development process as possible. > We all know that it is cheaper to fix mistakes early. Yes, however I have the feeling that it does so by imposing several restrictions that apart from protecting from possible misuses, also prevent from flexible uses. Again, this is just a feeling, I have not studied the language. > And there is no perfect language. Ada is not meant to satisfy the > desires of all programmers. It is meant to support programming as > a human activity with the goal of producing high quality programs > very efficiently. OK. -- Ioannis Vranos http://www23.brinkster.com/noicys